forked from sitepoint-editors/node-elasticsearch-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter.js
78 lines (72 loc) · 1.86 KB
/
filter.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
69
70
71
72
73
74
75
76
77
78
(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
}
}
}
],
filter: [
{
range: {
year: {
gte: 2011,
lte: 2015
}
}
}
]
}
}
};
console.log(`retrieving documents with a combined bool query and filter (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
};
} ());