Skip to content

Commit

Permalink
fix the normalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
2403905 authored and fschade committed Aug 24, 2023
1 parent 1dcbedc commit cf089c8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
54 changes: 45 additions & 9 deletions services/search/pkg/query/kql/dictionary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,44 +49,74 @@ func TestParse(t *testing.T) {
want: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Value: "federated"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Value: "search"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Value: "federat*"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Value: "search"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Value: "search"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Value: "fed*"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "author", Value: "John Smith"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "filetype", Value: "docx"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "filename", Value: "budget.xlsx"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Value: "author"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Value: "John Smith"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Value: "author"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Value: "John Smith"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Value: "author"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Value: "John Smith"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Value: "author"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Value: "John Smith"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Value: "author"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Value: "John Smith"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "author", Value: "Shakespear"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Key: "author", Value: "Paul"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Key: "author", Value: "Shakesp*"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "title", Value: "Advanced Search"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Key: "title", Value: "Advanced Sear*"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Key: "title", Value: "Advan* Search"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Key: "title", Value: "*anced Search"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "author", Value: "John Smith"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Key: "author", Value: "Jane Smith"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Key: "author", Value: "John Smith"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "filetype", Value: "docx"},
&ast.OperatorNode{Value: "AND"},
&ast.GroupNode{
Key: "author",
Nodes: []ast.Node{
&ast.StringNode{Value: "John Smith"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Value: "Jane Smith"},
},
},
&ast.OperatorNode{Value: "OR"},
&ast.GroupNode{
Key: "author",
Nodes: []ast.Node{
Expand All @@ -95,6 +125,7 @@ func TestParse(t *testing.T) {
&ast.StringNode{Value: "Jane Smith"},
},
},
&ast.OperatorNode{Value: "AND"},
&ast.GroupNode{
Nodes: []ast.Node{
&ast.StringNode{Key: "DepartmentId", Value: "*"},
Expand All @@ -106,10 +137,13 @@ func TestParse(t *testing.T) {
&ast.StringNode{Key: "contentclass", Value: "sts_site"},
&ast.OperatorNode{Value: "NOT"},
&ast.BooleanNode{Key: "IsHubSite", Value: false},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "author", Value: "John Smith"},
&ast.OperatorNode{Value: "AND"},
&ast.GroupNode{
Nodes: []ast.Node{
&ast.StringNode{Key: "filetype", Value: "docx"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "title", Value: "Advanced Search"},
},
},
Expand Down Expand Up @@ -137,6 +171,7 @@ func TestParse(t *testing.T) {
&ast.StringNode{Key: "tag", Value: "book"},
&ast.OperatorNode{Value: "NOT"},
&ast.StringNode{Key: "tag", Value: "read"},
&ast.OperatorNode{Value: "AND"},
&ast.GroupNode{
Key: "author",
Nodes: []ast.Node{
Expand All @@ -145,6 +180,15 @@ func TestParse(t *testing.T) {
&ast.StringNode{Value: "Jane"},
},
},
&ast.OperatorNode{Value: "OR"},
&ast.GroupNode{
Key: "author",
Nodes: []ast.Node{
&ast.StringNode{Value: "John Smith"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Value: "Jane"},
},
},
},
},
err: false,
Expand Down Expand Up @@ -175,7 +219,7 @@ func TestParse(t *testing.T) {
{
name: "KeyGroup",
got: []string{
`author:("John Smith" OR Jane)"`,
`author:("John Smith" OR Jane)`,
},
want: &ast.Ast{
Nodes: []ast.Node{
Expand All @@ -187,14 +231,6 @@ func TestParse(t *testing.T) {
&ast.StringNode{Value: "Jane"},
},
},
&ast.GroupNode{
Key: "author",
Nodes: []ast.Node{
&ast.StringNode{Value: "John Smith"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Value: "Jane"},
},
},
},
},
err: false,
Expand Down
10 changes: 9 additions & 1 deletion services/search/pkg/query/kql/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ func normalize(nodes []ast.Node) []ast.Node {
}
currentNode = n
currentKey = &n.Key
case *ast.BooleanNode:
if prevKey == nil {
prevKey = &n.Key
res = append(res, node)
continue
}
currentNode = n
currentKey = &n.Key
case *ast.GroupNode:
n.Nodes = normalize(n.Nodes)
if prevKey == nil {
Expand All @@ -201,9 +209,9 @@ func normalize(nodes []ast.Node) []ast.Node {
} else {
res = append(res, &operatorNodeAnd, currentNode)
}
prevKey = currentKey
currentNode = nil
currentKey = nil
prevKey = nil
continue
}
}
Expand Down

0 comments on commit cf089c8

Please sign in to comment.