forked from sitepoint-editors/node-elasticsearch-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 1
/
suggest_term.js
43 lines (37 loc) · 1.02 KB
/
suggest_term.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
(function () {
'use strict';
const elasticsearch = require('elasticsearch');
const esClient = new elasticsearch.Client({
host: '127.0.0.1:9200',
log: 'error'
});
const suggest = function search(index, body) {
return esClient.suggest({index: index, body: body});
};
// only for testing purposes
// all calls should be initiated through the module
const test = function test() {
let body = {
text: 'dolo lore fugi',
titleSuggester: {
term: {
field: 'title'
}
}
};
console.log(`retrieving term suggestions for "${body.text}"...`);
suggest('library', body)
.then(results => {
console.log(`suggestions for each term are:`);
results.titleSuggester.forEach((term, index) => {
console.log(`term ${++index}: ${term.text}`);
term.options.forEach((option, index) => console.log(`\t suggestion ${++index}: ${option.text}`));
});
})
.catch(console.error);
};
test();
module.exports = {
suggest
};
} ());