Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve nil condition for es query #83

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions pkg/queryexpr/es_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ func (e ESExpr) binaryNodeToEsQuery(n *ast.BinaryNode) (interface{}, error) { //

case "==":
if leftStr, ok := left.(string); ok {
if right == nil {
return e.mustNotExistQuery(leftStr), nil
}
return e.termQuery(leftStr, right), nil
}
result, err := getQueryExprResult(n.String())
Expand All @@ -117,6 +120,9 @@ func (e ESExpr) binaryNodeToEsQuery(n *ast.BinaryNode) (interface{}, error) { //

case "!=":
if leftStr, ok := left.(string); ok {
if right == nil {
return e.mustExistQuery(leftStr), nil
}
return e.mustNotQuery(leftStr, right), nil
}
result, err := getQueryExprResult(n.String())
Expand Down Expand Up @@ -257,6 +263,30 @@ func (ESExpr) mustNotQuery(field string, value interface{}) map[string]interface
}
}

func (ESExpr) mustExistQuery(field string) map[string]interface{} {
return map[string]interface{}{
"bool": map[string]interface{}{
"must": map[string]interface{}{
"exists": map[string]interface{}{
"field": field,
},
},
},
}
}

func (ESExpr) mustNotExistQuery(field string) map[string]interface{} {
return map[string]interface{}{
"bool": map[string]interface{}{
"must_not": map[string]interface{}{
"exists": map[string]interface{}{
"field": field,
},
},
},
}
}

func (ESExpr) rangeQuery(field, operator string, value interface{}) map[string]interface{} {
return map[string]interface{}{
"range": map[string]interface{}{
Expand Down
18 changes: 18 additions & 0 deletions pkg/queryexpr/es_expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@ func TestESExpr_ToQuery(t *testing.T) {
want: `{"query":{"range":{"updated_at":{"gt":"2024-04-05 23:59:59"}}}}`,
wantErr: false,
},
{
name: "equals to nil",
expr: queryexpr.ESExpr(`refreshed_at == nil`),
want: `{"query":{"bool":{"must_not":{"exists":{"field":"refreshed_at"}}}}}`,
wantErr: false,
},
{
name: "not equals to nil",
expr: queryexpr.ESExpr(`refreshed_at != nil`),
want: `{"query":{"bool":{"must":{"exists":{"field":"refreshed_at"}}}}}`,
wantErr: false,
},
{
name: "equals to nil and not equal to nil",
expr: queryexpr.ESExpr(`refreshed_at == nil && updated_at != nil`),
want: `{"query":{"bool":{"must":[{"bool":{"must_not":{"exists":{"field":"refreshed_at"}}}},{"bool":{"must":{"exists":{"field":"updated_at"}}}}]}}}`,
wantErr: false,
},
{
name: "in condition",
expr: queryexpr.ESExpr(`service in ["test1","test2","test3"]`),
Expand Down
Loading