Skip to content

Commit

Permalink
Merge pull request #7247 from 2403905/enable-kql-fix-unary
Browse files Browse the repository at this point in the history
fix unary in the beginning. add tests
  • Loading branch information
2403905 authored Sep 7, 2023
2 parents 3266efa + 158f91b commit 3ab8226
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
7 changes: 7 additions & 0 deletions changelog/unreleased/kql-fix-unary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Bugfix: Fixed cunary in the beginning

Fixed case when the unary in the beginning lead to panic


https://github.com/owncloud/ocis/pull/7247
git
39 changes: 30 additions & 9 deletions services/search/pkg/query/bleve/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,18 @@ func (c Compiler) Compile(givenAst *ast.Ast) (bleveQuery.Query, error) {
}

func compile(a *ast.Ast) (bleveQuery.Query, error) {
q, _ := walk(0, a.Nodes)
q, _, err := walk(0, a.Nodes)
if err != nil {
return nil, err
}
switch q.(type) {
case *bleveQuery.ConjunctionQuery, *bleveQuery.DisjunctionQuery:
return q, nil
}
return bleve.NewConjunctionQuery(q), nil
}

func walk(offset int, nodes []ast.Node) (bleveQuery.Query, int) {
func walk(offset int, nodes []ast.Node) (bleveQuery.Query, int, error) {
var prev, next bleveQuery.Query
var operator *ast.OperatorNode
var isGroup bool
Expand Down Expand Up @@ -113,7 +116,10 @@ func walk(offset int, nodes []ast.Node) (bleveQuery.Query, int) {
if n.Key != "" {
n = normalizeGroupingProperty(n)
}
q, _ := walk(0, n.Nodes)
q, _, err := walk(0, n.Nodes)
if err != nil {
return nil, 0, err
}
if prev == nil {
prev = q
isGroup = true
Expand All @@ -124,10 +130,19 @@ func walk(offset int, nodes []ast.Node) (bleveQuery.Query, int) {
if n.Value == kql.BoolAND || n.Value == kql.BoolOR {
operator = n
} else if n.Value == kql.BoolNOT {
next, offset = nextNode(i+1, nodes)
var err error
next, offset, err = nextNode(i+1, nodes)
if err != nil {
return nil, 0, err
}
q := bleve.NewBooleanQuery()
q.AddMustNot(next)
next = q
if prev == nil {
// unary in the beginning
prev = q
} else {
next = q
}
}
}
if prev != nil && next != nil && operator != nil {
Expand All @@ -140,13 +155,19 @@ func walk(offset int, nodes []ast.Node) (bleveQuery.Query, int) {
i = offset
}
}
return prev, offset
if prev == nil {
return nil, 0, fmt.Errorf("can not compile the query")
}
return prev, offset, nil
}

func nextNode(offset int, nodes []ast.Node) (bleveQuery.Query, int) {
func nextNode(offset int, nodes []ast.Node) (bleveQuery.Query, int, error) {
if n, ok := nodes[offset].(*ast.GroupNode); ok {
gq, _ := walk(0, n.Nodes)
return gq, offset + 1
gq, _, err := walk(0, n.Nodes)
if err != nil {
return nil, 0, err
}
return gq, offset + 1, nil
}
if n, ok := nodes[offset].(*ast.OperatorNode); ok {
if n.Value == kql.BoolNOT {
Expand Down
13 changes: 13 additions & 0 deletions services/search/pkg/query/bleve/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,19 @@ func Test_compile(t *testing.T) {
}),
wantErr: false,
},
{
name: `NOT tag:physik`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.OperatorNode{Value: "NOT"},
&ast.StringNode{Key: "tag", Value: "physik"},
},
},
want: query.NewConjunctionQuery([]query.Query{
query.NewBooleanQuery(nil, nil, []query.Query{query.NewQueryStringQuery(`Tags:physik`)}),
}),
wantErr: false,
},
{
name: `ast.DateTimeNode`,
args: &ast.Ast{
Expand Down

0 comments on commit 3ab8226

Please sign in to comment.