forked from ukrbublik/react-awesome-query-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelasticSearch.js
340 lines (297 loc) · 9.43 KB
/
elasticSearch.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import {getWidgetForFieldOp} from "../utils/ruleUtils";
import {defaultConjunction} from "../utils/defaultUtils";
/**
* Converts a string representation of top_left and bottom_right cords to
* a ES geo_point required for query
*
* @param {string} geoPointString - comma separated string of lat/lon coods
* @returns {{top_left: {lon: number, lat: number}, bottom_right: {lon: number, lat: number}}} - ES geoPoint formatted object
* @private
*/
function buildEsGeoPoint(geoPointString) {
if (geoPointString == null) {
return null;
}
const coordsNumberArray = geoPointString.split(",").map(Number);
return {
top_left: {
lat: coordsNumberArray[0],
lon: coordsNumberArray[1]
},
bottom_right: {
lat: coordsNumberArray[2],
lon: coordsNumberArray[3]
}
};
}
/**
* Converts a dateTime string from the query builder to a ES range formatted object
*
* @param {string} dateTime - dateTime formatted string
* @param {string} operator - query builder operator type, see constants.js and query builder docs
* @returns {{lt: string}|{lte: string}|{gte: string}|{gte: string, lte: string}|undefined} - ES range query parameter
*
* @private
*/
function buildEsRangeParameters(value, operator) {
// -- if value is greater than 1 then we assume this is a between operator : BUG this is wrong, a selectable list can have multiple values
if (value.length > 1) {
return {
gte: "".concat(value[0]),
lte: "".concat(value[1])
};
} // -- if value is only one we assume this is a date time query for a specific day
const dateTime = value[0]; //TODO: Rethink about this part, what if someone adds a new type of opperator
//todo: move this logic into config
switch (operator) {
case "on_date": //todo: not used
case "not_on_date":
case "equal":
case "select_equals":
case "not_equal":
return {
gte: "".concat(dateTime, "||/d"),
lte: "".concat(dateTime, "||+1d")
};
case "less_or_equal":
return {
lte: "".concat(dateTime)
};
case "greater_or_equal":
return {
gte: "".concat(dateTime)
};
case "less":
return {
lt: "".concat(dateTime)
};
case "greater":
return {
gte: "".concat(dateTime)
};
default:
return undefined;
}
}
/**
* Builds the DSL parameters for a Wildcard query
*
* @param {string} value - The match value
* @returns {{value: string}} - The value = value parameter surrounded with * on each end
* @private
*/
function buildEsWildcardParameters(value) {
return {
value: "*" + value + "*"
};
}
/**
* Takes the match type string from awesome query builder like 'greater_or_equal' and
* returns the ES occurrence required for bool queries
*
* @param {string} combinator - query group type or rule condition
* @returns {string} - ES occurrence type. See constants.js
* @private
*/
function determineOccurrence(combinator) {
//todo: move into config, like mongoConj
switch (combinator) {
case "AND":
return "must";
// -- AND
case "OR":
return "should";
// -- OR
case "NOT":
return "must_not";
// -- NOT AND
default:
return undefined;
}
}
/**
* Determines what field to query off of given the operator type
*
* @param {string} fieldDataType - The type of data
* @param {string} fullFieldName - A '.' separated string containing the property lineage (including self)
* @param {string} queryType - The query type
* @returns {string|*} - will be either the fullFieldName or fullFieldName.keyword
* @private
*/
//todo: not used
function determineQueryField(fieldDataType, fullFieldName, queryType) {
if (fieldDataType === "boolean") {
return fullFieldName;
}
switch (queryType) {
case "term":
case "wildcard":
return "".concat(fullFieldName, ".keyword");
case "geo_bounding_box":
case "range":
case "match":
return fullFieldName;
default:
console.error("Can't determine query field for query type ".concat(queryType));
return null;
}
}
function buildRegexpParameters(value) {
return {
value: value
};
}
function determineField(fieldName, config) {
//todo: ElasticSearchTextField - not used
//return config.fields[fieldName].ElasticSearchTextField || fieldName;
return fieldName;
}
function buildParameters(queryType, value, operator, fieldName, config) {
const textField = determineField(fieldName, config);
switch (queryType) {
case "filter":
//todo: elasticSearchScript - not used
return {
script: config.operators[operator].elasticSearchScript(fieldName, value)
};
case "exists":
return { field: fieldName };
case "match":
return { [textField]: value[0] };
case "term":
return { [fieldName]: value[0] };
//todo: not used
// need to add geo type into RAQB or remove this code
case "geo_bounding_box":
return { [fieldName]: buildEsGeoPoint(value[0]) };
case "range":
return { [fieldName]: buildEsRangeParameters(value, operator) };
case "wildcard":
return { [fieldName]: buildEsWildcardParameters(value[0]) };
case "regexp":
return { [fieldName]: buildRegexpParameters(value[0]) };
default:
return undefined;
}
}
/**
* Handles the building of the group portion of the DSL
*
* @param {string} fieldName - The name of the field you are building a rule for
* @param {string} fieldDataType - The type of data this field holds
* @param {string} value - The value of this rule
* @param {string} operator - The condition on how the value is matched
* @returns {object} - The ES rule
* @private
*/
function buildEsRule(fieldName, value, operator, config, valueSrc) {
if (!fieldName || !operator || value == undefined)
return undefined; // rule is not fully entered
let op = operator;
let opConfig = config.operators[op];
if (!opConfig)
return undefined; // unknown operator
let { elasticSearchQueryType } = opConfig;
// not
let not = false;
if (!elasticSearchQueryType && opConfig.reversedOp) {
not = true;
op = opConfig.reversedOp;
opConfig = config.operators[op];
({ elasticSearchQueryType } = opConfig);
}
// handle if value 0 has multiple values like a select in a array
const widget = getWidgetForFieldOp(config, fieldName, op, valueSrc);
const widgetConfig = config.widgets[widget];
const { elasticSearchFormatValue } = widgetConfig;
/** In most cases the queryType will be static however in some casese (like between) the query type will change
* based on the data type. i.e. a between time will be different than between number, date, letters etc... */
let queryType;
if (typeof elasticSearchQueryType === "function") {
queryType = elasticSearchQueryType(widget);
} else {
queryType = elasticSearchQueryType;
}
if (!queryType) {
// Not supported
return undefined;
}
/** If a widget has a rule on how to format that data then use that otherwise use default way of determineing search parameters
* */
let parameters;
if (typeof elasticSearchFormatValue === "function") {
parameters = elasticSearchFormatValue(queryType, value, op, fieldName, config);
} else {
parameters = buildParameters(queryType, value, op, fieldName, config);
}
if (not) {
return {
bool: {
must_not: {
[queryType]: {...parameters}
}
}
};
} else {
return {
[queryType]: {...parameters}
};
}
}
/**
* Handles the building of the group portion of the DSL
*
* @param {object} children - The contents of the group
* @param {string} conjunction - The way the contents of the group are joined together i.e. AND OR
* @param {Function} recursiveFxn - The recursive fxn to build the contents of the groups children
* @private
* @returns {object} - The ES group
*/
function buildEsGroup(children, conjunction, recursiveFxn, config) {
if (!children || !children.size)
return undefined;
const childrenArray = children.valueSeq().toArray();
const occurrence = determineOccurrence(conjunction);
const result = childrenArray.map((c) => recursiveFxn(c, config)).filter(v => v !== undefined);
if (!result.length)
return undefined;
const resultFlat = result.flat(Infinity);
return {
bool: {
[occurrence]: resultFlat
}
};
}
export function elasticSearchFormat(tree, config) {
// -- format the es dsl here
if (!tree) return undefined;
const type = tree.get("type");
const properties = tree.get("properties") || new Map();
if (type === "rule" && properties.get("field")) {
// -- field is null when a new blank rule is added
const operator = properties.get("operator");
const field = properties.get("field");
const value = properties.get("value").toJS();
const _valueType = properties.get("valueType")?.get(0);
const valueSrc = properties.get("valueSrc")?.get(0);
if (valueSrc === "func") {
// -- elastic search doesn't support functions (that is post processing)
return;
}
if (value && Array.isArray(value[0])) {
//TODO : Handle case where the value has multiple values such as in the case of a list
return value[0].map((val) =>
buildEsRule(field, [val], operator, config, valueSrc)
);
} else {
return buildEsRule(field, value, operator, config, valueSrc);
}
}
if (type === "group" || type === "rule_group") {
let conjunction = properties.get("conjunction");
if (!conjunction)
conjunction = defaultConjunction(config);
const children = tree.get("children1");
return buildEsGroup(children, conjunction, elasticSearchFormat, config);
}
}