/
query_clauses.ts
253 lines (233 loc) · 5.39 KB
/
query_clauses.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
243
244
245
246
247
248
249
250
251
252
253
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
/**
* Terminology
* ===========
* The terms for the differenct clauses in an Elasticsearch query can be confusing, here are some
* clarifications that might help you understand the Typescript types we use here.
*
* Given the following Query:
* {
* "query": { (1)
* "bool": { (2)
* "must":
* [
* (3) { "term" : { "tag" : "wow" } },
* { "term" : { "tag" : "elasticsearch" } },
* {
* "must" : { "term" : { "user" : "kimchy" } }
* }
* ]
* }
* }
* }
*
* These are referred to as:
* (1). BoolClause / BoolClauseWithAnyCondition
* (2). BoolCondition / AnyBoolCondition
* (3). BoolClauseFilter
*
*/
export interface TermFilter {
term: { [field: string]: string | string[] };
}
export interface RangeFilter {
range: {
[field: string]: { lte: string | number } | { lt: string | number } | { gt: string | number };
};
}
export interface ExistsFilter {
exists: { field: string };
}
type BoolClauseFilter = TermFilter | RangeFilter | ExistsFilter;
type BoolClauseFiltering<T extends BoolClauseFilter> =
| BoolClauseWithAnyCondition<T>
| PinnedQuery<T>
| T;
enum Conditions {
Should = 'should',
Must = 'must',
MustNot = 'must_not',
Filter = 'filter',
}
/**
* Describe a specific BoolClause Condition with a BoolClauseFilter on it, such as:
* ```
* {
* must : [
* T, ...
* ]
* }
* ```
*/
type BoolCondition<C extends Conditions, T extends BoolClauseFilter> = {
[c in C]: Array<BoolClauseFiltering<T>>;
};
/**
* Describe a Bool clause with a specific Condition, such as:
* ```
* {
* // described by BoolClause
* bool: {
* // described by BoolCondition
* must: [
* T, ...
* ]
* }
* }
* ```
*/
interface BoolClause<C extends Conditions, T extends BoolClauseFilter> {
bool: BoolCondition<C, T>;
}
/**
* Describe a Bool clause with mixed Conditions, such as:
* ```
* {
* // described by BoolClause<...>
* bool: {
* // described by BoolCondition<Conditions.Must, ...>
* must : {
* ...
* },
* // described by BoolCondition<Conditions.Should, ...>
* should : {
* ...
* }
* }
* }
* ```
*/
type AnyBoolCondition<T extends BoolClauseFilter> = {
[Condition in Conditions]?: Array<BoolClauseFiltering<T>>;
};
/**
* Describe a Bool Condition with any Condition on it, so it can handle both:
* ```
* {
* bool: {
* must : {
* ...
* }
* }
* }
* ```
*
* and:
*
* ```
* {
* bool: {
* must_not : {
* ...
* }
* }
* }
* ```
*/
export interface BoolClauseWithAnyCondition<T extends BoolClauseFilter> {
bool: AnyBoolCondition<T>;
}
/**
* Describe the various Bool Clause Conditions we support, as specified in the Conditions enum
*/
export type ShouldCondition<T extends BoolClauseFilter> = BoolClause<Conditions.Should, T>;
export type MustCondition<T extends BoolClauseFilter> = BoolClause<Conditions.Must, T>;
export type MustNotCondition<T extends BoolClauseFilter> = BoolClause<Conditions.MustNot, T>;
export type FilterCondition<T extends BoolClauseFilter> = BoolClause<Conditions.Filter, T>;
export interface SortClause {
_script: {
type: string;
order: string;
script: {
lang: string;
source: string;
params?: { [param: string]: string | string[] };
};
};
}
export type SortOptions = string | SortClause | Array<string | SortClause>;
export interface ScriptClause {
source: string;
lang: string;
params: {
[field: string]: string | number | Date;
};
}
export interface UpdateByQuery<T extends BoolClauseFilter> {
query: PinnedQuery<T> | BoolClauseWithAnyCondition<T>;
sort: SortOptions;
seq_no_primary_term: true;
script: ScriptClause;
}
export interface PinnedQuery<T extends BoolClauseFilter> {
pinned: PinnedClause<T>;
}
export interface PinnedClause<T extends BoolClauseFilter> {
ids: string[];
organic: BoolClauseWithAnyCondition<T>;
}
export function matchesClauses<T extends BoolClauseFilter>(
...clauses: Array<BoolClauseWithAnyCondition<T>>
): BoolClauseWithAnyCondition<T> {
return {
bool: Object.assign({}, ...clauses.map((clause) => clause.bool)),
};
}
export function shouldBeOneOf<T extends BoolClauseFilter>(
...should: Array<BoolClauseFiltering<T>>
): ShouldCondition<T> {
return {
bool: {
should,
},
};
}
export function mustBeAllOf<T extends BoolClauseFilter>(
...must: Array<BoolClauseFiltering<T>>
): MustCondition<T> {
return {
bool: {
must,
},
};
}
export function filterDownBy<T extends BoolClauseFilter>(
...filter: Array<BoolClauseFiltering<T>>
): FilterCondition<T> {
return {
bool: {
filter,
},
};
}
export function asPinnedQuery<T extends BoolClauseFilter>(
ids: PinnedClause<T>['ids'],
organic: PinnedClause<T>['organic']
): PinnedQuery<T> {
return {
pinned: {
ids,
organic,
},
};
}
export function asUpdateByQuery<T extends BoolClauseFilter>({
query,
update,
sort,
}: {
query: UpdateByQuery<T>['query'];
update: UpdateByQuery<T>['script'];
sort: UpdateByQuery<T>['sort'];
}): UpdateByQuery<T> {
return {
query,
sort,
seq_no_primary_term: true,
script: update,
};
}