File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -289,6 +289,48 @@ export class OpensearchQueryParser {
289
289
}
290
290
}
291
291
292
+ if ( operator === Operator . ARRAY_LENGTH ) {
293
+ if ( typeof value !== 'object' ) {
294
+ throw new Error (
295
+ 'Array length should be used with an object containing operators and their values!' ,
296
+ )
297
+ }
298
+
299
+ const operatorsMapping = {
300
+ gte : '>=' ,
301
+ lte : '<=' ,
302
+ gt : '>' ,
303
+ lt : '<' ,
304
+ eq : '==' ,
305
+ }
306
+
307
+ const scriptQueries = Object . entries ( value ) . map ( ( [ lengthOperator , arrayLength ] ) => {
308
+ if ( typeof arrayLength !== 'number' ) {
309
+ throw new Error ( `Array length value for operator ${ lengthOperator } should be a number!` )
310
+ }
311
+
312
+ if ( ! Object . prototype . hasOwnProperty . call ( operatorsMapping , lengthOperator ) ) {
313
+ throw new Error ( `Invalid operator "${ lengthOperator } " used in ARRAY_LENGTH` )
314
+ }
315
+
316
+ return {
317
+ script : {
318
+ source : `doc['${ searchKey } '].length ${ operatorsMapping [ lengthOperator ] } params.length` ,
319
+ lang : 'painless' ,
320
+ params : {
321
+ length : arrayLength ,
322
+ } ,
323
+ } ,
324
+ }
325
+ } )
326
+
327
+ return {
328
+ bool : {
329
+ must : scriptQueries ,
330
+ } ,
331
+ }
332
+ }
333
+
292
334
return {
293
335
term : {
294
336
[ searchKey ] : value ,
Original file line number Diff line number Diff line change @@ -88,4 +88,6 @@ export enum Operator {
88
88
CONTAINS = 'contains' ,
89
89
90
90
NOT = 'not' ,
91
+
92
+ ARRAY_LENGTH = 'arrayLength' ,
91
93
}
You can’t perform that action at this time.
0 commit comments