diff --git a/services/search/pkg/parity/query_mediatype_test.go b/services/search/pkg/parity/query_mediatype_test.go index cd43ff89d5..32a79a89fa 100644 --- a/services/search/pkg/parity/query_mediatype_test.go +++ b/services/search/pkg/parity/query_mediatype_test.go @@ -20,6 +20,10 @@ func mediatypeGroup() queryGroup { {id: 4, query: `mediatype:*jpeg`, want: []string{"photo.jpg"}}, {id: 5, query: `mediatype:image`, want: []string{"photo.jpg"}}, {id: 6, query: `mediatype:folder`, want: []string{"albums", "drafts"}}, + {id: 7, query: `mediatype:file`, want: []string{"notes.md", "photo.jpg"}}, + {id: 8, query: `NOT mediatype:file`, want: []string{"albums", "drafts"}}, + {id: 9, query: `mediatype:file OR mediatype:image`, want: []string{"notes.md", "photo.jpg"}}, + {id: 10, query: `NOT mediatype:(image OR folder)`, want: []string{"notes.md"}}, }, } } diff --git a/services/search/pkg/query/bleve/compiler.go b/services/search/pkg/query/bleve/compiler.go index e4be2d50c9..1ca00dbe7e 100644 --- a/services/search/pkg/query/bleve/compiler.go +++ b/services/search/pkg/query/bleve/compiler.go @@ -269,10 +269,7 @@ func walk(offset int, nodes []ast.Node) (bleveQuery.Query, int, error) { func nextNode(offset int, nodes []ast.Node) (bleveQuery.Query, int, error) { if n, ok := nodes[offset].(*ast.GroupNode); ok { - if n.Key != "" { - n = normalizeGroupingProperty(n) - } - + // keys are resolved and group keys propagated by normalize gq, _, err := walk(0, n.Nodes) if err != nil { return nil, 0, err @@ -359,12 +356,3 @@ func numberRange(field string, operator *ast.OperatorNode, value float64) bleveQ return q } - -func normalizeGroupingProperty(group *ast.GroupNode) *ast.GroupNode { - for _, n := range group.Nodes { - if onode, ok := n.(*ast.StringNode); ok { - onode.Key = group.Key - } - } - return group -} diff --git a/services/search/pkg/query/mimetype/mimetype.go b/services/search/pkg/query/mimetype/mimetype.go index ca61e651ac..64b901beb5 100644 --- a/services/search/pkg/query/mimetype/mimetype.go +++ b/services/search/pkg/query/mimetype/mimetype.go @@ -23,9 +23,13 @@ func Expand(key, value string) []ast.Node { value = strings.ToLower(value) switch value { case "file": + // grouped so the negation stays atomic when it composes with other + // terms (mediatype:file OR ...) return []ast.Node{ - &ast.OperatorNode{Value: kql.BoolNOT}, - &ast.StringNode{Key: field, Value: "httpd/unix-directory"}, + &ast.GroupNode{Nodes: []ast.Node{ + &ast.OperatorNode{Value: kql.BoolNOT}, + &ast.StringNode{Key: field, Value: "httpd/unix-directory"}, + }}, } case "folder": return term("httpd/unix-directory") diff --git a/services/search/pkg/query/mimetype/mimetype_test.go b/services/search/pkg/query/mimetype/mimetype_test.go index d48fb44e6a..b21c5befbe 100644 --- a/services/search/pkg/query/mimetype/mimetype_test.go +++ b/services/search/pkg/query/mimetype/mimetype_test.go @@ -56,8 +56,10 @@ var _ = Describe("Expand", func() { It("expands file to not-a-folder", func() { Expect(mimetype.Expand("mediatype", "file")).To(Equal([]ast.Node{ - &ast.OperatorNode{Value: "NOT"}, - &ast.StringNode{Key: "MimeType", Value: "httpd/unix-directory"}, + &ast.GroupNode{Nodes: []ast.Node{ + &ast.OperatorNode{Value: "NOT"}, + &ast.StringNode{Key: "MimeType", Value: "httpd/unix-directory"}, + }}, })) }) diff --git a/services/search/pkg/query/normalize_test.go b/services/search/pkg/query/normalize_test.go index 5260eae383..cb77be7c12 100644 --- a/services/search/pkg/query/normalize_test.go +++ b/services/search/pkg/query/normalize_test.go @@ -73,8 +73,10 @@ var _ = Describe("Normalize", func() { &ast.OperatorNode{Value: "AND"}, &ast.StringNode{Key: "photo.cameraMake", Value: "canon", CaseInsensitive: true}, &ast.OperatorNode{Value: "AND"}, - &ast.OperatorNode{Value: "NOT"}, - &ast.StringNode{Key: "MimeType", Value: "httpd/unix-directory"}, + &ast.GroupNode{Nodes: []ast.Node{ + &ast.OperatorNode{Value: "NOT"}, + &ast.StringNode{Key: "MimeType", Value: "httpd/unix-directory"}, + }}, &ast.OperatorNode{Value: "AND"}, &ast.NumberNode{Key: "Size", Value: 100}, }))