Adding Basic Pagination support using size/from params.

This commit is contained in:
Wallermadev 2017-09-20 04:01:06 +01:00
parent c6fab81e61
commit 66fcf4c833
2 changed files with 18 additions and 10 deletions

File diff suppressed because one or more lines are too long

View file

@ -17,6 +17,8 @@ const eclient = new elasticsearch.Client({
}); });
function getResults(input) { function getResults(input) {
if(input.size == undefined) input.size = 10;
if(input.from == undefined) input.from = 0;
return eclient.search({ return eclient.search({
index: "claims", index: "claims",
body: { body: {
@ -24,7 +26,7 @@ function getResults(input) {
"bool": { "bool": {
"must": { "must": {
"query_string": { "query_string": {
"query": input.trim(), "query": input.s.trim(),
"fields": [ "fields": [
"name", "name",
"value.stream.metadata.author", "value.stream.metadata.author",
@ -34,12 +36,16 @@ function getResults(input) {
} }
} }
} }
} },
size: input.size,
from: input.from
} }
}); });
} }
function getAutoComplete(input) { function getAutoComplete(input) {
if(input.size == undefined) input.size = 10;
if(input.from == undefined) input.from = 0;
return eclient.search({ return eclient.search({
index: "claims", index: "claims",
_source: ["name", "value.stream.metadata.title", "value.stream.metadata.author"], _source: ["name", "value.stream.metadata.title", "value.stream.metadata.author"],
@ -48,7 +54,7 @@ function getAutoComplete(input) {
"bool": { "bool": {
"must": { "must": {
"query_string": { "query_string": {
"query": input.trim(), "query": input.s.trim(),
"fields": [ "fields": [
"name", "name",
"value.stream.metadata.title", "value.stream.metadata.title",
@ -57,7 +63,9 @@ function getAutoComplete(input) {
} }
} }
} }
} },
size: input.size,
from: input.from
} }
}); });
} }
@ -74,7 +82,7 @@ class LighthouseControllers {
* @param {ctx} Koa Context * @param {ctx} Koa Context
*/ */
async search(ctx) { async search(ctx) {
await getResults(ctx.query.s).then(function (result) { await getResults(ctx.query).then(function (result) {
let results = result.hits.hits; let results = result.hits.hits;
let cResults = []; let cResults = [];
for (let pResult of results) { for (let pResult of results) {
@ -90,7 +98,7 @@ class LighthouseControllers {
* @param {ctx} Koa Context * @param {ctx} Koa Context
*/ */
async autoComplete(ctx) { async autoComplete(ctx) {
await getAutoComplete(ctx.query.s).then(function (result) { await getAutoComplete(ctx.query).then(function (result) {
let results = result.hits.hits; let results = result.hits.hits;
let cResults = []; let cResults = [];
for (let pResult of results) { for (let pResult of results) {
@ -116,7 +124,7 @@ class LighthouseControllers {
* @param {ctx} Koa Context * @param {ctx} Koa Context
*/ */
async info(ctx) { async info(ctx) {
ctx.body = 'Info...'; ctx.body = "Lighthouse";
} }
/** /**
@ -124,7 +132,7 @@ class LighthouseControllers {
* @param {ctx} Koa Context * @param {ctx} Koa Context
*/ */
async status(ctx) { async status(ctx) {
ctx.body = getStats(); ctx.body = eclient.getStats();
} }
/* eslint-enable no-param-reassign */ /* eslint-enable no-param-reassign */