-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.ts
242 lines (213 loc) · 7.62 KB
/
queries.ts
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// ---------------------------------------------------------------------------------------------------------------------
// imports
// ---------------------------------------------------------------------------------------------------------------------
import {
// term
ExistsQuery,
IdsQuery,
TermQuery,
TermsQuery,
PrefixQuery,
FuzzyQuery,
FuzzyOptions,
WildcardQuery,
WildcardOptions,
RegexpQuery,
RegexpQueryOptions,
RangeQuery,
RangeQueryOptions,
// text
MatchAllQuery,
MatchQuery,
MatchPhraseQuery,
MatchPhrasePrefixQuery,
MultiMatchQuery,
// compound
BooleanQuery,
Query,
} from '../types/queries'
// ---------------------------------------------------------------------------------------------------------------------
// term queries
// ---------------------------------------------------------------------------------------------------------------------
/**
* Returns documents that contain an indexed value for a field.
* @see https://www.elastic.co/guide/en/elasticsearch/reference/7.15/query-dsl-exists-query.html
*/
export function _exists (field: string): ExistsQuery {
return { exists: { field } }
}
/**
* Returns documents based on their IDs. This query uses document IDs stored in the _id field.
* @see https://www.elastic.co/guide/en/elasticsearch/reference/7.15/query-dsl-ids-query.html
*/
export function _ids (ids: string | string[]): IdsQuery {
return { ids: { values: ids } }
}
/**
* Returns documents that contain one or more exact terms in a provided field.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/7.15/query-dsl-term-query.html
* @see https://www.elastic.co/guide/en/elasticsearch/reference/7.15/query-dsl-terms-query.html
*/
export function _term (field: string, value: any): TermQuery | TermsQuery {
return Array.isArray(value)
? { terms: { [field]: value } } as TermsQuery
: { term: { [field]: { value } } } as TermQuery
}
/**
* Returns documents that contain a specific prefix in a provided field.
* @see https://www.elastic.co/guide/en/elasticsearch/reference/7.15/query-dsl-prefix-query.html
*/
export function _prefix (field: string, value: any): PrefixQuery {
return { prefix: { [field]: { value } } }
}
/**
* Returns documents that contain terms similar to the search term.
* @see https://www.elastic.co/guide/en/elasticsearch/reference/7.15/query-dsl-fuzzy-query.html
*/
export function _fuzzy (field: string, value: string, options: FuzzyOptions = {}): FuzzyQuery {
return { fuzzy: { [field]: { value, ...options } } }
}
/**
* Returns documents that contain terms matching a wildcard pattern.
* @see https://www.elastic.co/guide/en/elasticsearch/reference/7.15/query-dsl-wildcard-query.html
*/
export function _wildcard (field: string, value: string, options: WildcardOptions = {}): WildcardQuery {
return { wildcard: { [field]: { value, ...options } } }
}
/**
* Returns documents that contain terms matching a regular expression.
* @see https://www.elastic.co/guide/en/elasticsearch/reference/7.15/query-dsl-regexp-query.html
*/
export function _regexp (field: string, value: string, options: RegexpQueryOptions = {}): RegexpQuery {
return { regexp: { [field]: { value, ...options } } }
}
/**
* Returns documents that contain terms within a provided range.
* @see https://www.elastic.co/guide/en/elasticsearch/reference/7.15/query-dsl-range-query.html
*/
export function _range (field: string, options: RangeQueryOptions = {}): RangeQuery {
return { range: { [field]: options } }
}
// ---------------------------------------------------------------------------------------------------------------------
// text queries
// ---------------------------------------------------------------------------------------------------------------------
/**
* The most simple query, which matches all documents.
* @see https://www.elastic.co/guide/en/elasticsearch/reference/7.15/query-dsl-match-all-query.html
*/
export function _matchAll (): MatchAllQuery {
return { match_all: {} }
}
/**
* Returns documents that match a provided text, number, date or boolean value.
* @see https://www.elastic.co/guide/en/elasticsearch/reference/7.15/query-dsl-match-query.html
*/
export function _match (field: string, value: any): MatchQuery {
return { match: { [field]: value } }
}
/**
* The match_phrase query analyzes the text and creates a phrase query out of the analyzed text.
* @see https://www.elastic.co/guide/en/elasticsearch/reference/7.15/query-dsl-match-query-phrase.html
*/
export function _matchPhrase (field: string, value: any): MatchPhraseQuery {
return { match_phrase: { [field]: value } }
}
/**
* Returns documents that contain the words of a provided text, in the same order as provided.
* The last term of the provided text is treated as a prefix, matching any words that begin with that term.
* @see https://www.elastic.co/guide/en/elasticsearch/reference/7.15/query-dsl-match-query-phrase-prefix.html
*/
export function _matchPhrasePrefix (field: string, value: any): MatchPhrasePrefixQuery {
return { match_phrase_prefix: { [field]: value } }
}
/**
* The multi_match query builds on the match query to allow multi-field queries.
* @see https://www.elastic.co/guide/en/elasticsearch/reference/7.15/query-dsl-multi-match-query.html
*/
export function _multiMatch (fields: string | string[], value: any, type = 'phrase_prefix'): MultiMatchQuery {
return {
multi_match: {
fields: Array.isArray(fields) ? fields : [fields],
query: value,
type,
},
}
}
// ---------------------------------------------------------------------------------------------------------------------
// compound queries
// ---------------------------------------------------------------------------------------------------------------------
/**
* A query that matches documents matching boolean combinations of other queries.
* @see https://www.elastic.co/guide/en/elasticsearch/reference/7.15/query-dsl-bool-query.html
*/
export function _bool (type: 'must' | 'filter' | 'should' | 'must_not', clauses: Query[]): BooleanQuery | Query {
if (clauses.length === 1) {
return clauses[0] as Query
}
return {
bool: {
[type]: clauses,
},
} as BooleanQuery
}
/**
* The clause (query) must appear in matching documents (AND)
* @param clauses
*/
export function _must (clauses: Query[]) {
return _bool('must', clauses)
}
/**
* The clause (query) should appear in the matching document (OR)
* @param clauses
*/
export function _should (clauses: Query[]) {
return _bool('should', clauses)
}
/**
* The clause (query) must appear in matching documents (AND)
* The score of the query is ignored.
* @param clauses
*/
export function _filter (clauses: Query[]) {
return _bool('filter', clauses)
}
/**
* The clause (query) must not appear in the matching documents
* The score of the query is ignored.
* @param clauses
*/
export function _mustNot (clauses: Query[]) {
return _bool('must_not', clauses)
}
// ---------------------------------------------------------------------------------------------------------------------
// queries object (import as $)
// ---------------------------------------------------------------------------------------------------------------------
export default {
// term
ids: _ids,
term: _term,
prefix: _prefix,
fuzzy: _fuzzy,
wildcard: _wildcard,
regexp: _regexp,
range: _range,
exists: _exists,
// text
matchAll: _matchAll,
match: _match,
matchPhrase: _matchPhrase,
matchPhrasePrefix: _matchPhrasePrefix,
multiMatch: _multiMatch,
// compound
bool: _bool,
must: _must,
should: _should,
filter: _filter,
mustNot: _mustNot,
// synonyms
all: _matchAll,
and: _must,
or: _should,
}