-
Notifications
You must be signed in to change notification settings - Fork 7
/
search.js
153 lines (146 loc) · 2.74 KB
/
search.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const { client, indexName: index } = require("./config");
const { logTitles } = require("./helpers");
/**
* Finding matches sorted by relevance (full-text query)
* run-func search match title "soups with beer and garlic"
* run-func search match title "pizza salad and cheese"
*/
module.exports.match = (field, query) => {
const body = {
query: {
match: {
[field]: {
query,
},
},
},
};
client.search(
{
index,
body,
},
logTitles
);
};
/**
* Matching a phrase (full-text query)
* run-func search phrase title 'pasta with cheese'
* run-func search phrase title 'milk chocolate cake'
*/
module.exports.phrase = (field, query, slop) => {
const body = {
query: {
match_phrase: {
[field]: {
query,
slop
},
},
},
};
client.search(
{
index,
body,
},
logTitles
);
};
/**
* Using special operators within a query string and a size parameter (full-text query)
* run-func search queryString title '+(dessert | cake) -garlic (mango | caramel | cinnamon)'
* run-func search queryString title '+(salad | soup) -broccoli (tomato | apple)'
*/
module.exports.queryString = (field, query) => {
const body = {
query: {
query_string: {
default_field: field,
query,
},
},
};
client.search(
{
index,
body
},
logTitles
);
};
/**
* Searching for exact matches of a value in a field (term-level query)
* run-func search term sodium 0
*/
module.exports.term = (field, value) => {
const body = {
query: {
term: {
[field]: value,
},
},
};
client.search(
{
index,
body,
},
logTitles
);
};
/**
* Searching for a range of values in a field (term-level query)
* gt (greater than)
* gte (greater than or equal to)
* lt (less than)
* lte (less than or equal to)
* run-func search range sodium 0 100
*/
module.exports.range = (field, gte, lte) => {
const body = {
query: {
range: {
[field]: {
gte,
lte,
},
},
},
};
client.search(
{
index,
body,
},
logTitles
);
};
/**
* Combining several queries together (boolean query)
* run-func search boolean
*/
module.exports.boolean = () => {
const body = {
query: {
bool: {
filter: [{ range: { rating: { gte: 4 } } }],
must: [
{ match: { categories: "Quick & Easy" } },
{ match: { title: "beer" } },
],
should: [
{ match: { categories: "Cocktails" } },
],
must_not: { match: { ingredients: "garlic" } }
},
},
};
client.search(
{
index,
body,
},
logTitles
);
};