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,55 +8,104 @@ const loggerStream = winstonStream(winston, 'info');
const eclient = new elasticsearch.Client({
host: 'http://localhost:9200',
log : {
level : 'info',
type : 'stream',
log: {
level: 'info',
type: 'stream',
stream: loggerStream,
},
});
function getSuggestions (input) {
return eclient.suggest({
index: 'claims',
body : {
'claim': {
'text' : input,
'completion': {
'field': 'suggest_name',
},
},
},
function getResults(input) {
return eclient.search({
index: "claims",
body: {
"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"
]
}
}
}
}
}
});
}
class LighthouseControllers {
/* eslint-disable no-param-reassign */
// Start syncing blocks...
startSync () {
startSync() {
winston.log('info', '[Importer] Started importer, indexing claims.');
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;
async search(ctx) {
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
*/
async info (ctx) {
async info(ctx) {
ctx.body = 'Info...';
}
@ -64,7 +113,7 @@ class LighthouseControllers {
* Status of the api here
* @param {ctx} Koa Context
*/
async status (ctx) {
async status(ctx) {
ctx.body = getStats();
}

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',