forked from sitepoint-editors/node-elasticsearch-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 1
/
search_all.js
40 lines (34 loc) · 976 Bytes
/
search_all.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
(function () {
'use strict';
const elasticsearch = require('elasticsearch');
const esClient = new elasticsearch.Client({
host: '127.0.0.1:9200',
log: 'error'
});
const search = function search(index, body) {
return esClient.search({index: index, body: body});
};
// only for testing purposes
// all calls should be initiated through the module
const test = function test() {
let body = {
size: 20,
from: 0,
query: {
match_all: {}
}
};
console.log(`retrieving all documents (displaying ${body.size} at a time)...`);
search('library', body)
.then(results => {
console.log(`found ${results.hits.total} items in ${results.took}ms`);
console.log(`returned article titles:`);
results.hits.hits.forEach((hit, index) => console.log(`\t${body.from + ++index} - ${hit._source.title}`));
})
.catch(console.error);
};
test();
module.exports = {
search
};
} ());