Skip to content

Commit 03de97c

Browse files
authored
Support non empty array matches for emails (#1147)
1 parent a43eca4 commit 03de97c

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

services/libs/opensearch/src/opensearchQueryParser.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,48 @@ export class OpensearchQueryParser {
289289
}
290290
}
291291

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+
292334
return {
293335
term: {
294336
[searchKey]: value,

services/libs/types/src/opensearch.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,6 @@ export enum Operator {
8888
CONTAINS = 'contains',
8989

9090
NOT = 'not',
91+
92+
ARRAY_LENGTH = 'arrayLength',
9193
}

0 commit comments

Comments
 (0)