forked from sitepoint-editors/node-elasticsearch-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 1
/
search_bool.js
68 lines (62 loc) · 1.65 KB
/
search_bool.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
(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: {
bool: {
must: [
{
query_string: {
query: '(authors.firstname:D* OR authors.lastname:H*) AND (title:excepteur)'
}
}
],
should: [
{
match: {
body: {
query: 'Elit nisi fugiat dolore amet',
type: 'phrase'
}
}
}
],
must_not: [
{
range: {
year: {
lte: 2000,
gte: 1990
}
}
}
]
}
}
};
console.log(`retrieving documents with a combined bool query (displaying ${body.size} items at a time)...`);
search('library', body)
.then(results => {
console.log(`found ${results.hits.total} items in ${results.took}ms`);
if (results.hits.total > 0) console.log(`returned article titles:`);
results.hits.hits.forEach((hit, index) => console.log(`\t${body.from + ++index} - ${hit._source.title} (score: ${hit._score})`));
})
.catch(console.error);
};
test();
module.exports = {
search
};
} ());