Merge pull request #4210 from opencloud-eu/fix/kql-group-key-needs-operator

fix(kql): tell operators, words and group keys apart
This commit is contained in:
Dominik Schmidt authored and GitHub committed 2026-12-04 11:01:22 +01:00
commit fb2b1a15f8
5 files changed
+1083 -799

No files matched your search

+11 -4
View File
@@ -32,8 +32,11 @@ Node <-
////////////////////////////////////////////////////////
GroupNode <-
k:Key? (OperatorColonNode / OperatorEqualNode)? "(" v:Nodes ")" {
k:Key (OperatorColonNode / OperatorEqualNode) "(" v:Nodes ")" {
return buildGroupNode(k, v, c.text, c.pos)
} /
(OperatorColonNode / OperatorEqualNode)? "(" v:Nodes ")" {
return buildGroupNode(nil, v, c.text, c.pos)
}
////////////////////////////////////////////////////////
@@ -115,20 +118,24 @@ OperatorBooleanNodes <-
OperatorBooleanOrNode
OperatorBooleanAndNode <-
("AND" / "+") {
("AND" KeywordEnd / "+") {
return buildOperatorNode(c.text, c.pos)
}
OperatorBooleanNotNode <-
("NOT" / "-") {
("NOT" KeywordEnd / "-") {
return buildOperatorNode(c.text, c.pos)
}
OperatorBooleanOrNode <-
("OR") {
("OR" KeywordEnd) {
return buildOperatorNode(c.text, c.pos)
}
// a keyword is only an operator when the word ends there, ORANGE is a word
KeywordEnd <-
![^ \t:()"]
OperatorColonNode <-
":" {
return buildOperatorNode(c.text, c.pos)
+926 -792
View File
File diff suppressed because it is too large. Load diff
+117 -1
View File
@@ -128,6 +128,122 @@ func TestParse_Spec(t *testing.T) {
},
},
},
// a word directly in front of a group is a word, only "key:(" and "key=(" name the group
{
name: `cat(dog OR fox)`,
ast: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Value: "cat"},
&ast.OperatorNode{Value: kql.BoolAND},
&ast.GroupNode{Nodes: []ast.Node{
&ast.StringNode{Value: "dog"},
&ast.OperatorNode{Value: kql.BoolOR},
&ast.StringNode{Value: "fox"},
}},
},
},
},
{
name: `name:cat(dog)`,
ast: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Key: "name", Value: "cat"},
&ast.OperatorNode{Value: kql.BoolAND},
&ast.GroupNode{Nodes: []ast.Node{
&ast.StringNode{Value: "dog"},
}},
},
},
},
{
name: `NOT(dog)`,
ast: &ast.Ast{
Nodes: []ast.Node{
&ast.OperatorNode{Value: kql.BoolNOT},
&ast.GroupNode{Nodes: []ast.Node{
&ast.StringNode{Value: "dog"},
}},
},
},
},
{
name: `cat AND(dog)`,
ast: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Value: "cat"},
&ast.OperatorNode{Value: kql.BoolAND},
&ast.GroupNode{Nodes: []ast.Node{
&ast.StringNode{Value: "dog"},
}},
},
},
},
{
name: `cat OR(dog)`,
ast: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Value: "cat"},
&ast.OperatorNode{Value: kql.BoolOR},
&ast.GroupNode{Nodes: []ast.Node{
&ast.StringNode{Value: "dog"},
}},
},
},
},
// a keyword is only an operator when the word ends there
{
name: `NOTHING`,
ast: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Value: "NOTHING"},
},
},
},
{
name: `cat ORANGE`,
ast: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Value: "cat"},
&ast.OperatorNode{Value: kql.BoolAND},
&ast.StringNode{Value: "ORANGE"},
},
},
},
{
name: `ANDROID(dog)`,
ast: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Value: "ANDROID"},
&ast.OperatorNode{Value: kql.BoolAND},
&ast.GroupNode{Nodes: []ast.Node{
&ast.StringNode{Value: "dog"},
}},
},
},
},
{
name: `cat NOT"dog"`,
ast: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Value: "cat"},
&ast.OperatorNode{Value: kql.BoolAND},
&ast.OperatorNode{Value: kql.BoolNOT},
&ast.StringNode{Value: "dog"},
},
},
},
{
name: `name=(cat OR dog)`,
ast: &ast.Ast{
Nodes: []ast.Node{
&ast.GroupNode{Key: "name", Nodes: []ast.Node{
&ast.StringNode{Value: "cat"},
&ast.OperatorNode{Value: kql.BoolOR},
&ast.StringNode{Value: "dog"},
}},
},
},
},
// 2.1.12 Parentheses
// 3.1.12 Parentheses
{
@@ -1032,7 +1148,7 @@ func TestParse_DottedKey(t *testing.T) {
},
},
},
// GroupNode also uses the Key rule via k:Key?.
// GroupNode also uses the Key rule.
{
name: `audio.artist:("Motörhead" OR Beatles)`,
ast: &ast.Ast{
+13 -1
View File
@@ -356,7 +356,7 @@ Fixtures:
- `alpha.txt`, Tags = red
- `beta.txt`, Tags = blue
- `gamma.md`, MimeType = text/markdown
- `gamma.md`, MimeType = text/markdown, Tags = ORANGE, ANDROID, NOTES
| Case | Query | expected | bleve | OpenSearch | same? |
|---|---|---|---|---|---|
@@ -366,6 +366,18 @@ Fixtures:
| BOOLEAN-04 | `name:"*a*" AND tag:("red")` | alpha.txt | alpha.txt | alpha.txt | ✅ |
| BOOLEAN-05 | `(name:"*alpha*" OR name:"*beta*") AND mediatype:text/plain` | alpha.txt, beta.txt | alpha.txt, beta.txt | alpha.txt, beta.txt | ✅ |
| BOOLEAN-06 | `name:"*a*" AND mediatype:text/markdown` | gamma.md | gamma.md | gamma.md | ✅ |
| BOOLEAN-07 | `name:"*a*" AND NOT(name:"*alpha*")` | beta.txt, gamma.md | beta.txt, gamma.md | beta.txt, gamma.md | ✅ |
| BOOLEAN-08 | `name:"*a*" AND(tag:red)` | alpha.txt | alpha.txt | alpha.txt | ✅ |
| BOOLEAN-09 | `name:"*a*" AND (name:"*alpha*" OR(tag:blue))` | alpha.txt, beta.txt | alpha.txt, beta.txt | alpha.txt, beta.txt | ✅ |
| BOOLEAN-10 | `alpha.txt(tag:red)` | alpha.txt | alpha.txt | alpha.txt | ✅ |
| BOOLEAN-11 | `tag:(red OR blue)` | alpha.txt, beta.txt | alpha.txt, beta.txt | alpha.txt, beta.txt | ✅ |
| BOOLEAN-12 | `tag:(red blue)` | no match | no match | no match | ✅ |
| BOOLEAN-13 | `tag:(red NOT blue)` | alpha.txt | alpha.txt | alpha.txt | ✅ |
| BOOLEAN-14 | `name:(alpha.txt OR beta.txt)` | alpha.txt, beta.txt | alpha.txt, beta.txt | alpha.txt, beta.txt | ✅ |
| BOOLEAN-15 | `name:(tag:red)` | bad request | bad request | bad request | ✅ |
| BOOLEAN-16 | `tag:(ORANGE)` | gamma.md | gamma.md | gamma.md | ✅ |
| BOOLEAN-17 | `tag:(red OR ANDROID)` | alpha.txt, gamma.md | alpha.txt, gamma.md | alpha.txt, gamma.md | ✅ |
| BOOLEAN-18 | `tag:(NOTES)` | gamma.md | gamma.md | gamma.md | ✅ |
### samename
@@ -10,7 +10,7 @@ func booleanGroup() queryGroup {
fixtures: []search.Resource{
fixtureDoc("alpha.txt", withTags("red")),
fixtureDoc("beta.txt", withTags("blue")),
fixtureDoc("gamma.md", withMime("text/markdown")),
fixtureDoc("gamma.md", withMime("text/markdown"), withTags("ORANGE", "ANDROID", "NOTES")),
},
cases: []queryCase{
{id: 1, query: `name:"*alpha*" AND name:"*txt*"`, want: []string{"alpha.txt"}},
@@ -19,6 +19,21 @@ func booleanGroup() queryGroup {
{id: 4, query: `name:"*a*" AND tag:("red")`, want: []string{"alpha.txt"}},
{id: 5, query: `(name:"*alpha*" OR name:"*beta*") AND mediatype:text/plain`, want: []string{"alpha.txt", "beta.txt"}},
{id: 6, query: `name:"*a*" AND mediatype:text/markdown`, want: []string{"gamma.md"}},
// an operator or a word directly in front of a group does not name the group
{id: 7, query: `name:"*a*" AND NOT(name:"*alpha*")`, want: []string{"beta.txt", "gamma.md"}},
{id: 8, query: `name:"*a*" AND(tag:red)`, want: []string{"alpha.txt"}},
{id: 9, query: `name:"*a*" AND (name:"*alpha*" OR(tag:blue))`, want: []string{"alpha.txt", "beta.txt"}},
{id: 10, query: `alpha.txt(tag:red)`, want: []string{"alpha.txt"}},
// a keyed group holds several values of one field
{id: 11, query: `tag:(red OR blue)`, want: []string{"alpha.txt", "beta.txt"}},
{id: 12, query: `tag:(red blue)`},
{id: 13, query: `tag:(red NOT blue)`, want: []string{"alpha.txt"}},
{id: 14, query: `name:(alpha.txt OR beta.txt)`, want: []string{"alpha.txt", "beta.txt"}},
{id: 15, query: `name:(tag:red)`, wantBadRequest: true},
// a word that starts with an operator keyword is a word
{id: 16, query: `tag:(ORANGE)`, want: []string{"gamma.md"}},
{id: 17, query: `tag:(red OR ANDROID)`, want: []string{"alpha.txt", "gamma.md"}},
{id: 18, query: `tag:(NOTES)`, want: []string{"gamma.md"}},
},
}
}