fix(kql): read AND, OR and NOT as operators only where the word ends

This commit is contained in:
Dominik Schmidt committed 2026-12-03 19:22:54 +01:00
1 parent a780f85357
commit 5c4fe27f4c
5 files changed
+766 -662

No files matched your search

+7 -3
View File
@@ -118,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)
+702 -657
View File
File diff suppressed because it is too large. Load diff
+42
View File
@@ -190,6 +190,48 @@ func TestParse_Spec(t *testing.T) {
},
},
},
// 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{
+10 -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? |
|---|---|---|---|---|---|
@@ -369,6 +369,15 @@ Fixtures:
| 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"}},
@@ -30,6 +30,10 @@ func booleanGroup() queryGroup {
{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"}},
},
}
}