Merge pull request #13 from wallermadev/master

Search adaptions and corrections. (Working Search/Autocomplete)
This commit is contained in:
Fillerino 2017-09-20 08:06:07 +02:00 committed by GitHub
commit b99e1f46cb
4 changed files with 98 additions and 26 deletions

File diff suppressed because one or more lines are too long

View file

@ -8,67 +8,134 @@ 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) {
if(input.size == undefined) input.size = 10;
if(input.from == undefined) input.from = 0;
return eclient.search({
index: "claims",
body: {
"query": {
"bool": {
"must": {
"query_string": {
"query": input.s.trim(),
"fields": [
"name",
"value.stream.metadata.author",
"value.stream.metadata.title",
"value.stream.metadata.description"
]
}
}
}
},
},
size: input.size,
from: input.from
}
});
}
function getAutoComplete(input) {
if(input.size == undefined) input.size = 10;
if(input.from == undefined) input.from = 0;
return eclient.search({
index: "claims",
_source: ["name", "value.stream.metadata.title", "value.stream.metadata.author"],
body: {
"query": {
"bool": {
"must": {
"query_string": {
"query": input.s.trim(),
"fields": [
"name",
"value.stream.metadata.title",
"value.stream.metadata.author"
]
}
}
}
},
size: input.size,
from: input.from
}
});
}
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).then(function (result) {
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).then(function (result) {
let results = result.hits.hits;
let cResults = [];
for (let pResult of results) {
cResults.push(pResult._source.name);
if(pResult._source.value !== undefined){
cResults.push(pResult._source.value.stream.metadata.title);
cResults.push(pResult._source.value.stream.metadata.author);
}
}
var clean = new Array();
for (var i = 0; i < cResults.length; i++) {
if (cResults[i] && cResults[i].length > 3 && clean.indexOf(cResults[i]) == -1) {
clean.push(cResults[i]);
}
}
ctx.body = clean;
});
}
/**
* Info about the api here
* @param {ctx} Koa Context
*/
async info (ctx) {
ctx.body = 'Info...';
async info(ctx) {
ctx.body = "Lighthouse";
}
/**
* Status of the api here
* @param {ctx} Koa Context
*/
async status (ctx) {
ctx.body = getStats();
async status(ctx) {
ctx.body = eclient.getStats();
}
/* eslint-enable no-param-reassign */
}
export default new LighthouseControllers();
export default new LighthouseControllers();

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