Skip to content

Instantly share code, notes, and snippets.

@sulmanen
Created August 24, 2017 06:33
Show Gist options
  • Save sulmanen/593e8c3873b3e456e0aa6930caafebe6 to your computer and use it in GitHub Desktop.
Save sulmanen/593e8c3873b3e456e0aa6930caafebe6 to your computer and use it in GitHub Desktop.
// elasticsearch.js client documentation is like getting lost in the woods
// node-fetch does not allow GET with a body
const expect = require("chai").expect;
const esUrl = YOUR_URL_HERE; //paste new url here once resource has been created
const https = require("https");
describe("areena search", () => {
it("should be able to assert", () => {
expect(true).to.be.ok;
});
describe("search by program title", () => {
let result;
const query = 'Bike';
const requestBody = JSON.stringify({
query: {
multi_match: {
query: query,
fields: ['title.fi^10', 'description.fi']
}
}
});
// only setting the Content-Length header will send the GET body
const options = {
host: esUrl,
path: '/myindex/_search',
method: 'GET',
headers: {'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(requestBody)}
};
before(done => {
const request = https.request(options, function (response) {
var str = '';
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
result = JSON.parse(str);
done();
});
});
request.write(requestBody);
request.on('error', function(e) {
console.error(e);
});
request.end();
});
it('there should be a result maching the title query', () => {
result.hits.hits.map(hit => console.log(hit._source.title));
expect(result.hits.hits[0]._source.title.fi.includes(query)).to.be.ok;
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment