Skip to content

Commit 49a3a91

Browse files
committed
Add: OpenSearch Query MustNotExists to querybuilder
1 parent de26e92 commit 49a3a91

5 files changed

Lines changed: 23 additions & 1 deletion

File tree

pkg/openSearch/osquery/boolQueryBuilder.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (q *BoolQueryBuilder) AddFilterRequest(request *filter.Request) error {
151151
if handler, ok := operatorMapping[field.Operator]; ok {
152152
value := field.Value
153153

154-
if field.Operator == filter.CompareOperatorExists {
154+
if field.Operator == filter.CompareOperatorExists || field.Operator == filter.CompareOperatorMustNotExists {
155155
value = "" // exists operator does not need a value, but for more consistent handling just pass a dummy value
156156
}
157157
if value == nil {
@@ -289,5 +289,9 @@ func defaultCompareOperators() []CompareOperator {
289289
Operator: filter.CompareOperatorExists,
290290
Handler: HandleCompareOperatorExists, MustCondition: true,
291291
},
292+
{
293+
Operator: filter.CompareOperatorMustNotExists,
294+
Handler: HandleCompareOperatorMustNotExists, MustCondition: true,
295+
},
292296
}
293297
}

pkg/openSearch/osquery/boolQueryBuilder_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,15 @@ func TestBoolQueryBuilder_AddFilterRequest(t *testing.T) {
608608
wantDocuments: []ostesting.TestType{doc0, doc1, doc2},
609609
})
610610

611+
// MustNotExists operator
612+
addTest("operator must not Exists", testCase{
613+
filterRequest: singleFilter(filter.RequestField{
614+
Name: "keywordOmitEmptyField",
615+
Operator: filter.CompareOperatorMustNotExists,
616+
}),
617+
wantDocuments: []ostesting.TestType{doc0},
618+
})
619+
611620
// BetweenDates operator
612621
addTest("operator BetweenDates (date time string)", testCase{
613622
filterRequest: singleFilter(filter.RequestField{

pkg/openSearch/osquery/compareHandler.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ func HandleCompareOperatorExists(fieldName string, _ any) (esquery.Mappable, err
158158
return esquery.Exists(fieldName), nil
159159
}
160160

161+
func HandleCompareOperatorMustNotExists(fieldName string, _ any) (esquery.Mappable, error) {
162+
return esquery.Bool().MustNot(esquery.Exists(fieldName)), nil
163+
}
164+
161165
// HandleCompareOperatorBetweenDates constructs an OpenSearch range query for a given date field.
162166
// It accepts a field name and a field value, which must be a slice of exactly 2 elements, representing the start and end of range. Accepted slice types:
163167
// - []time.Time,

pkg/query/filter/type.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ CompareOperator ENUM(
7575
betweenDates
7676
7777
exists
78+
mustNotExists
7879
7980
isEqualToRating
8081
isNotEqualToRating

pkg/query/filter/type_enum.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)