forked from sitepoint-editors/node-elasticsearch-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 1
/
suggest_phrase.js
43 lines (37 loc) · 1.01 KB
/
suggest_phrase.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',
bodySuggester: {
phrase: {
field: 'body'
}
}
};
console.log(`retrieving phrase suggestions for "${body.text}"...`);
suggest('library', body)
.then(results => {
console.log(`suggestions for the phrase are:`);
results.bodySuggester.forEach(phrase => {
console.log(`phrase: ${phrase.text}`);
phrase.options.forEach((option, index) => console.log(`\t suggestion ${++index}: ${option.text}`));
});
})
.catch(console.error);
};
test();
module.exports = {
suggest
};
} ());