Improvements to search relevancy and seperation of search/autocomplete feature.

-- Removed `getSuggestion`

-- Added `getResults`
-- Added `getAutoComplete`

Restructured to search query to bool query that searches on a claims name, title, author and keywords in descripton -- `it's a disaster` now shows in a search for `disaster` autocomplete also returned correctly for the same example.
This commit is contained in:
Wallermadev 2017-09-20 02:49:42 +01:00
parent d5470ce0e3
commit 58c0fa6aa6
4 changed files with 78 additions and 24 deletions

File diff suppressed because one or more lines are too long

View file

@ -8,6 +8,7 @@ const loggerStream = winstonStream(winston, 'info');
const eclient = new elasticsearch.Client({
host: 'http://localhost:9200',
log: {
level: 'info',
type: 'stream',
@ -15,17 +16,49 @@ const eclient = new elasticsearch.Client({
},
});
function getSuggestions (input) {
return eclient.suggest({
index: 'claims',
function getResults(input) {
return eclient.search({
index: "claims",
body: {
'claim': {
'text' : input,
'completion': {
'field': 'suggest_name',
},
},
},
"query": {
"bool": {
"must": {
"query_string": {
"query": input.trim(),
"fields": [
"name",
"value.stream.metadata.author",
"value.stream.metadata.title",
"value.stream.metadata.description"
]
}
}
}
}
}
});
}
function getAutoComplete(input) {
return eclient.search({
index: "claims",
_source: ["name", "value.stream.metadata.title", "value.stream.metadata.author"],
body: {
"query": {
"bool": {
"must": {
"query_string": {
"query": input.trim(),
"fields": [
"name",
"value.stream.metadata.title",
"value.stream.metadata.author"
]
}
}
}
}
}
});
}
@ -37,21 +70,37 @@ class LighthouseControllers {
sync();
}
/**
* Search api here
* Search API Endpoint.
* @param {ctx} Koa Context
*/
async search(ctx) {
await getSuggestions(ctx.query.s).then(function (result) {
let results = result.claim[0].options;
await getResults(ctx.query.s).then(function (result) {
console.log(result.hits.hits);
let results = result.hits.hits;
let cResults = [];
for (let pResult of results) {
cResults.push(pResult._source);
}
ctx.body = cResults;
});
// ctx.body = 'Search...';
}
/**
* Autocomplete API Endpoint.
* @param {ctx} Koa Context
*/
async autoComplete(ctx) {
await getAutoComplete(ctx.query.s).then(function (result) {
console.log(result.hits.hits);
let results = result.hits.hits;
let cResults = [];
for (let pResult of results) {
cResults.push(pResult._source);
}
ctx.body = cResults;
});
}
/**
* Info about the api here
* @param {ctx} Koa Context

View file

@ -17,6 +17,10 @@ router.get('/', LighthouseControllers.info);
// GET /api/search
router.get('/search', LighthouseControllers.search);
// GET /api/search
router.get('/autocomplete', LighthouseControllers.autoComplete);
// GET /api/ligthouse/status
router.get('/status', LighthouseControllers.status);

View file

@ -13,7 +13,8 @@ import appRoot from 'app-root-path';
const loggerStream = winstonStream(winston, 'info');
const eclient = new elasticsearch.Client({
host: 'http://elastic:changeme@localhost:9200',
host: 'http://localhost:9200',
log : {
level : 'info',
type : 'stream',