From cf089c825c3e7235220ee94f561a7cdbda271451 Mon Sep 17 00:00:00 2001 From: Roman Perekhod Date: Wed, 23 Aug 2023 15:56:45 +0200 Subject: [PATCH] fix the normalizer --- .../search/pkg/query/kql/dictionary_test.go | 54 +++++++++++++++---- services/search/pkg/query/kql/factory.go | 10 +++- 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/services/search/pkg/query/kql/dictionary_test.go b/services/search/pkg/query/kql/dictionary_test.go index 1b4c0a9669b..4c728d97f4c 100644 --- a/services/search/pkg/query/kql/dictionary_test.go +++ b/services/search/pkg/query/kql/dictionary_test.go @@ -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{ @@ -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: "*"}, @@ -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"}, }, }, @@ -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{ @@ -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, @@ -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{ @@ -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, diff --git a/services/search/pkg/query/kql/factory.go b/services/search/pkg/query/kql/factory.go index 0b2a800c8e5..a49af7426d6 100644 --- a/services/search/pkg/query/kql/factory.go +++ b/services/search/pkg/query/kql/factory.go @@ -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 { @@ -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 } }