Skip to content

Commit

Permalink
[backend] handle nul dates in filters (#6517)
Browse files Browse the repository at this point in the history
  • Loading branch information
Archidoit committed Apr 15, 2024
1 parent fcc8abb commit 9865d46
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ export const getAvailableOperatorForFilterKey = (
}
const { type: filterType } = filterDefinition;
if (filterType === 'date') {
return ['gt', 'gte', 'lt', 'lte'];
return ['gt', 'gte', 'lt', 'lte', 'nil', 'not_nil'];
}
if (isNumericFilter(filterType)) {
return ['gt', 'gte', 'lt', 'lte'];
Expand Down
34 changes: 33 additions & 1 deletion opencti-platform/opencti-graphql/src/database/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -1746,6 +1746,24 @@ const buildLocalMustFilter = async (validFilter) => {
}
};
}
} else if (filterDefinition?.type === 'date') { // date filters: nil <-> (field doesn't exist) OR (date <= epoch) OR (date >= 5138)
valueFiltering = {
bool: {
should: [{
bool: {
must_not: {
exists: {
field: R.head(arrayKeys)
}
}
}
},
{ range: { [R.head(arrayKeys)]: { lte: '1970-01-01T01:00:00.000Z' } } },
{ range: { [R.head(arrayKeys)]: { gte: '5138-11-16T09:46:40.000Z' } } }
],
minimum_should_match: 1,
}
};
}
valuesFiltering.push(valueFiltering);
}
Expand Down Expand Up @@ -1791,6 +1809,20 @@ const buildLocalMustFilter = async (validFilter) => {
}
};
}
} else if (filterDefinition?.type === 'date') { // date filters: not_nil <-> (field exists) AND (date > epoch) AND (date < 5138)
valueFiltering = {
bool: {
should: [{
exists: {
field: R.head(arrayKeys)
}
},
{ range: { [R.head(arrayKeys)]: { gt: '1970-01-01T01:00:00.000Z' } } },
{ range: { [R.head(arrayKeys)]: { lt: '5138-11-16T09:46:40.000Z' } } }
],
minimum_should_match: 3,
}
};
}
valuesFiltering.push(valueFiltering);
}
Expand Down Expand Up @@ -1874,7 +1906,7 @@ const buildLocalMustFilter = async (validFilter) => {
if (arrayKeys.length > 1) {
throw UnsupportedError('Filter must have only one field', { keys: arrayKeys });
}
valuesFiltering.push({ range: { [R.head(arrayKeys)]: { [operator]: values[i] } } });
valuesFiltering.push({ range: { [R.head(arrayKeys)]: { [operator]: values[i] } } }); // range operators
}
}
}
Expand Down

0 comments on commit 9865d46

Please sign in to comment.