Files
opencloud/services/search/pkg/query/bleve/compiler_test.go
T
Dominik Schmidt c3a08d88da fix(search): answer text on numeric and date fields the same on both engines
A text restriction on a numeric field reached the engines as is. bleve found nothing for size:>100 or size:50..500, OpenSearch failed the search with a number format exception. size=50 only matched on OpenSearch.

Both engines now read the value of such a restriction as a number and match it as a whole, with : and with =. Anything that is no number matches nothing, and so does text on a date field. size:>100 is not KQL, the range form is size>100; old iOS clients still send the colon form.

The field type comes from one table of all fields, built from the walk the mapping validation uses. It replaces the separate walk for numeric fields, which left out the parts of a location: location.altitude>1000 now matches, the parts are indexed as numbers next to the geopoint.

The parity suite covers >=, <=, combined ranges, number equality, the colon forms and the location parts.
2026-11-25 10:58:30 +01:00

751 lines
23 KiB
Go

package bleve
import (
"fmt"
"testing"
"time"
"github.com/blevesearch/bleve/v2/search/query"
tAssert "github.com/stretchr/testify/assert"
"github.com/opencloud-eu/opencloud/pkg/ast"
searchquery "github.com/opencloud-eu/opencloud/services/search/pkg/query"
)
var timeMustParse = func(t *testing.T, ts string) time.Time {
tp, err := time.Parse(time.RFC3339Nano, ts)
if err != nil {
t.Fatalf("time.Parse(...) error = %v", err)
}
return tp
}
// TODO(followup): make this a pure compiler test. Field resolution and
// media-type expansion live in query.Normalize, so this test could feed
// canonical ASTs (real field names, media-type already expanded) and call
// compile() directly, dropping the query.Normalize wrapper and the mediatype
// cases.
func boolFieldQuery(field string, value bool) query.Query {
q := query.NewBoolFieldQuery(value)
q.SetField(field)
return q
}
func numberQuery(field string, value float64) query.Query {
inclusive := true
q := query.NewNumericRangeInclusiveQuery(&value, &value, &inclusive, &inclusive)
q.SetField(field)
return q
}
func Test_compile(t *testing.T) {
tests := []struct {
name string
args *ast.Ast
want query.Query
wantErr bool
}{
{
name: `federated`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Value: "federated"},
},
},
want: query.NewConjunctionQuery([]query.Query{
query.NewQueryStringQuery(`Name_words:"federated"`),
}),
wantErr: false,
},
{
// one term matches the folder itself and its descendants
name: `path:/Foo`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Key: "path", Value: "/Foo"},
},
},
want: func() query.Query {
tq := query.NewTermQuery("./Foo")
tq.SetField("Path")
return query.NewConjunctionQuery([]query.Query{tq})
}(),
wantErr: false,
},
{
name: `"John Smith"`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Value: "John Smith"},
},
},
want: query.NewConjunctionQuery([]query.Query{
query.NewQueryStringQuery(`Name_words:"john smith"`),
}),
wantErr: false,
},
{
name: `"John Smith" Jane`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Key: "name", Value: "John Smith"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "name", Value: "Jane"},
},
},
want: query.NewConjunctionQuery([]query.Query{
query.NewQueryStringQuery(`Name_words:"john smith"`),
query.NewQueryStringQuery(`Name_words:"jane"`),
}),
wantErr: false,
},
{
name: `tag:bestseller tag:book`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Key: "tag", Value: "bestseller"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "tag", Value: "book"},
},
},
want: query.NewConjunctionQuery([]query.Query{
query.NewQueryStringQuery(`Tags_lowercase:bestseller`),
query.NewQueryStringQuery(`Tags_lowercase:book`),
}),
wantErr: false,
},
{
name: `name:"moby di*" OR tag:bestseller AND tag:book`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Key: "name", Value: "moby di*"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Key: "tag", Value: "bestseller"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "tag", Value: "book"},
},
},
want: query.NewDisjunctionQuery([]query.Query{
query.NewQueryStringQuery(`Name_lowercase:moby\ di*`),
query.NewConjunctionQuery([]query.Query{
query.NewQueryStringQuery(`Tags_lowercase:bestseller`),
query.NewQueryStringQuery(`Tags_lowercase:book`),
}),
}),
wantErr: false,
},
{
name: `a AND b OR c`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Value: "a"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Value: "b"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Value: "c"},
},
},
want: query.NewDisjunctionQuery([]query.Query{
query.NewConjunctionQuery([]query.Query{
query.NewQueryStringQuery(`Name_words:"a"`),
query.NewQueryStringQuery(`Name_words:"b"`),
}),
query.NewQueryStringQuery(`Name_words:"c"`),
}),
wantErr: false,
},
{
name: `a OR b AND c`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Value: "a"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Value: "b"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Value: "c"},
},
},
want: query.NewDisjunctionQuery([]query.Query{
query.NewQueryStringQuery(`Name_words:"a"`),
query.NewConjunctionQuery([]query.Query{
query.NewQueryStringQuery(`Name_words:"b"`),
query.NewQueryStringQuery(`Name_words:"c"`),
}),
}),
wantErr: false,
},
{
name: `(a OR b OR c) AND d`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.GroupNode{Nodes: []ast.Node{
&ast.StringNode{Value: "a"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Value: "b"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Value: "c"},
}},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Value: "d"},
},
},
want: query.NewConjunctionQuery([]query.Query{
query.NewDisjunctionQuery([]query.Query{
query.NewQueryStringQuery(`Name_words:"a"`),
query.NewQueryStringQuery(`Name_words:"b"`),
query.NewQueryStringQuery(`Name_words:"c"`),
}),
query.NewQueryStringQuery(`Name_words:"d"`),
}),
wantErr: false,
},
{
name: `(name:"moby di*" OR tag:bestseller) AND tag:book`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.GroupNode{Nodes: []ast.Node{
&ast.StringNode{Key: "name", Value: "moby di*"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Key: "tag", Value: "bestseller"},
}},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "tag", Value: "book"},
},
},
want: query.NewConjunctionQuery([]query.Query{
query.NewDisjunctionQuery([]query.Query{
query.NewQueryStringQuery(`Name_lowercase:moby\ di*`),
query.NewQueryStringQuery(`Tags_lowercase:bestseller`),
}),
query.NewQueryStringQuery(`Tags_lowercase:book`),
}),
wantErr: false,
},
{
name: `(name:"moby di*" OR tag:bestseller) AND tag:book AND NOT tag:read`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.GroupNode{Nodes: []ast.Node{
&ast.StringNode{Key: "name", Value: "moby di*"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Key: "tag", Value: "bestseller"},
}},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "tag", Value: "book"},
&ast.OperatorNode{Value: "AND"},
&ast.OperatorNode{Value: "NOT"},
&ast.StringNode{Key: "tag", Value: "read"},
},
},
want: query.NewConjunctionQuery([]query.Query{
query.NewDisjunctionQuery([]query.Query{
query.NewQueryStringQuery(`Name_lowercase:moby\ di*`),
query.NewQueryStringQuery(`Tags_lowercase:bestseller`),
}),
query.NewQueryStringQuery(`Tags_lowercase:book`),
query.NewBooleanQuery(nil, nil, []query.Query{query.NewQueryStringQuery(`Tags_lowercase:read`)}),
}),
wantErr: false,
},
{
name: `author:("John Smith" Jane)`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.GroupNode{
Key: "author",
Nodes: []ast.Node{
&ast.StringNode{Value: "John Smith"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Value: "Jane"},
},
},
},
},
want: query.NewConjunctionQuery([]query.Query{
query.NewQueryStringQuery(`author:John\ Smith`),
query.NewQueryStringQuery(`author:Jane`),
}),
wantErr: false,
},
{
name: `author:("John Smith" Jane) AND tag:bestseller`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.GroupNode{
Key: "author",
Nodes: []ast.Node{
&ast.StringNode{Value: "John Smith"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Value: "Jane"},
},
},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "tag", Value: "bestseller"},
},
},
want: query.NewConjunctionQuery([]query.Query{
query.NewQueryStringQuery(`author:John\ Smith`),
query.NewQueryStringQuery(`author:Jane`),
query.NewQueryStringQuery(`Tags_lowercase:bestseller`),
}),
wantErr: false,
},
{
name: `id:b27d3bf1-b254-459f-92e8-bdba668d6d3f$d0648459-25fb-4ed8-8684-bc62c7dca29c!d0648459-25fb-4ed8-8684-bc62c7dca29c mtime>=2023-09-05T12:40:59.14741+02:00`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{
Key: "id",
Value: "b27d3bf1-b254-459f-92e8-bdba668d6d3f$d0648459-25fb-4ed8-8684-bc62c7dca29c!d0648459-25fb-4ed8-8684-bc62c7dca29c",
},
&ast.OperatorNode{Value: "AND"},
&ast.DateTimeNode{
Key: "Mtime",
Operator: &ast.OperatorNode{Value: ">="},
Value: timeMustParse(t, "2023-09-05T08:42:11.23554+02:00"),
},
},
},
want: query.NewConjunctionQuery([]query.Query{
query.NewQueryStringQuery(`ID:b27d3bf1\-b254\-459f\-92e8\-bdba668d6d3f$d0648459\-25fb\-4ed8\-8684\-bc62c7dca29c\!d0648459\-25fb\-4ed8\-8684\-bc62c7dca29c`),
func() query.Query {
q := query.NewDateRangeInclusiveQuery(timeMustParse(t, "2023-09-05T08:42:11.23554+02:00"), time.Time{}, &[]bool{true}[0], nil)
q.FieldVal = "Mtime"
return q
}(),
}),
wantErr: false,
},
{
name: `StringNode value lowercase`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Value: "John Smith"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "Hidden", Value: "T"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "hidden", Value: "T"},
},
},
want: query.NewConjunctionQuery([]query.Query{
query.NewQueryStringQuery(`Name_words:"john smith"`),
boolFieldQuery("Hidden", true),
boolFieldQuery("Hidden", true),
}),
wantErr: false,
},
{
name: `a numeric field takes a number, anything else matches nothing`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Key: "size", Value: "50"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Key: "size", Value: "50", Exact: true},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Key: "size", Value: ">100"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Key: "size", Value: "50..500"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Key: "mtime", Value: ">2021-01-01"},
},
},
want: query.NewDisjunctionQuery([]query.Query{
numberQuery("Size", 50),
numberQuery("Size", 50),
query.NewMatchNoneQuery(),
query.NewMatchNoneQuery(),
query.NewMatchNoneQuery(),
}),
wantErr: false,
},
{
name: `NOT tag:physik`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.OperatorNode{Value: "NOT"},
&ast.StringNode{Key: "tag", Value: "physik"},
},
},
want: query.NewConjunctionQuery([]query.Query{
query.NewBooleanQuery(nil, nil, []query.Query{query.NewQueryStringQuery(`Tags_lowercase:physik`)}),
}),
wantErr: false,
},
{
name: `ast.DateTimeNode`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.DateTimeNode{
Key: "mtime",
// "=" is not supported by bleve, ignore
Operator: &ast.OperatorNode{Value: "="},
Value: timeMustParse(t, "2023-09-05T08:42:11.23554+02:00"),
},
&ast.OperatorNode{Value: "AND"},
&ast.DateTimeNode{
Key: "mtime",
// ":" is not supported by bleve, ignore
Operator: &ast.OperatorNode{Value: ":"},
Value: timeMustParse(t, "2023-09-05T08:42:11.23554+02:00"),
},
&ast.OperatorNode{Value: "AND"},
&ast.DateTimeNode{
Key: "mtime",
// no operator, skip
Value: timeMustParse(t, "2023-09-05T08:42:11.23554+02:00"),
},
&ast.OperatorNode{Value: "AND"},
&ast.DateTimeNode{
Key: "mtime",
Operator: &ast.OperatorNode{Value: ">"},
Value: timeMustParse(t, "2023-09-05T08:42:11.23554+02:00"),
},
&ast.OperatorNode{Value: "AND"},
&ast.DateTimeNode{
Key: "mtime",
Operator: &ast.OperatorNode{Value: ">="},
Value: timeMustParse(t, "2023-09-05T08:42:11.23554+02:00"),
},
&ast.OperatorNode{Value: "AND"},
&ast.DateTimeNode{
Key: "mtime",
Operator: &ast.OperatorNode{Value: "<"},
Value: timeMustParse(t, "2023-09-05T08:42:11.23554+02:00"),
},
&ast.OperatorNode{Value: "AND"},
&ast.DateTimeNode{
Key: "mtime",
Operator: &ast.OperatorNode{Value: "<="},
Value: timeMustParse(t, "2023-09-05T08:42:11.23554+02:00"),
},
},
},
want: query.NewConjunctionQuery([]query.Query{
func() query.Query {
q := query.NewDateRangeInclusiveQuery(timeMustParse(t, "2023-09-05T08:42:11.23554+02:00"), time.Time{}, &[]bool{false}[0], nil)
q.FieldVal = "Mtime"
return q
}(),
func() query.Query {
q := query.NewDateRangeInclusiveQuery(timeMustParse(t, "2023-09-05T08:42:11.23554+02:00"), time.Time{}, &[]bool{true}[0], nil)
q.FieldVal = "Mtime"
return q
}(),
func() query.Query {
q := query.NewDateRangeInclusiveQuery(time.Time{}, timeMustParse(t, "2023-09-05T08:42:11.23554+02:00"), nil, &[]bool{false}[0])
q.FieldVal = "Mtime"
return q
}(),
func() query.Query {
q := query.NewDateRangeInclusiveQuery(time.Time{}, timeMustParse(t, "2023-09-05T08:42:11.23554+02:00"), nil, &[]bool{true}[0])
q.FieldVal = "Mtime"
return q
}(),
}),
wantErr: false,
},
{
name: `MimeType:document`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Key: "mediatype", Value: "document"},
},
},
want: query.NewDisjunctionQuery([]query.Query{
query.NewQueryStringQuery(`MimeType:application/msword`),
query.NewQueryStringQuery(`MimeType:application/vnd.openxmlformats\-officedocument.wordprocessingml.document`),
query.NewQueryStringQuery(`MimeType:application/vnd.openxmlformats\-officedocument.wordprocessingml.form`),
query.NewQueryStringQuery(`MimeType:application/vnd.oasis.opendocument.text`),
query.NewQueryStringQuery(`MimeType:text/plain`),
query.NewQueryStringQuery(`MimeType:text/markdown`),
query.NewQueryStringQuery(`MimeType:application/rtf`),
query.NewQueryStringQuery(`MimeType:application/vnd.apple.pages`),
}),
wantErr: false,
},
{
name: `MimeType:document AND *tdd*`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Key: "mediatype", Value: "document"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "name", Value: "*tdd*"},
},
},
want: query.NewConjunctionQuery([]query.Query{
query.NewDisjunctionQuery([]query.Query{
query.NewQueryStringQuery(`MimeType:application/msword`),
query.NewQueryStringQuery(`MimeType:application/vnd.openxmlformats\-officedocument.wordprocessingml.document`),
query.NewQueryStringQuery(`MimeType:application/vnd.openxmlformats\-officedocument.wordprocessingml.form`),
query.NewQueryStringQuery(`MimeType:application/vnd.oasis.opendocument.text`),
query.NewQueryStringQuery(`MimeType:text/plain`),
query.NewQueryStringQuery(`MimeType:text/markdown`),
query.NewQueryStringQuery(`MimeType:application/rtf`),
query.NewQueryStringQuery(`MimeType:application/vnd.apple.pages`),
}),
query.NewQueryStringQuery(`Name_lowercase:*tdd*`),
}),
wantErr: false,
},
{
name: `MimeType:document OR MimeType:pdf AND *tdd*`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Key: "mediatype", Value: "document"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Key: "mediatype", Value: "pdf"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "name", Value: "*tdd*"},
},
},
want: query.NewDisjunctionQuery([]query.Query{
query.NewQueryStringQuery(`MimeType:application/msword`),
query.NewQueryStringQuery(`MimeType:application/vnd.openxmlformats\-officedocument.wordprocessingml.document`),
query.NewQueryStringQuery(`MimeType:application/vnd.openxmlformats\-officedocument.wordprocessingml.form`),
query.NewQueryStringQuery(`MimeType:application/vnd.oasis.opendocument.text`),
query.NewQueryStringQuery(`MimeType:text/plain`),
query.NewQueryStringQuery(`MimeType:text/markdown`),
query.NewQueryStringQuery(`MimeType:application/rtf`),
query.NewQueryStringQuery(`MimeType:application/vnd.apple.pages`),
query.NewConjunctionQuery([]query.Query{
query.NewQueryStringQuery(`MimeType:application/pdf`),
query.NewQueryStringQuery(`Name_lowercase:*tdd*`),
}),
}),
wantErr: false,
},
{
name: `(MimeType:document OR MimeType:pdf) AND *tdd*`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.GroupNode{Nodes: []ast.Node{
&ast.StringNode{Key: "mediatype", Value: "document"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Key: "mediatype", Value: "pdf"},
}},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "name", Value: "*tdd*"},
},
},
want: query.NewConjunctionQuery([]query.Query{
query.NewDisjunctionQuery([]query.Query{
query.NewQueryStringQuery(`MimeType:application/msword`),
query.NewQueryStringQuery(`MimeType:application/vnd.openxmlformats\-officedocument.wordprocessingml.document`),
query.NewQueryStringQuery(`MimeType:application/vnd.openxmlformats\-officedocument.wordprocessingml.form`),
query.NewQueryStringQuery(`MimeType:application/vnd.oasis.opendocument.text`),
query.NewQueryStringQuery(`MimeType:text/plain`),
query.NewQueryStringQuery(`MimeType:text/markdown`),
query.NewQueryStringQuery(`MimeType:application/rtf`),
query.NewQueryStringQuery(`MimeType:application/vnd.apple.pages`),
query.NewQueryStringQuery(`MimeType:application/pdf`),
}),
query.NewQueryStringQuery(`Name_lowercase:*tdd*`),
}),
wantErr: false,
},
{
name: `(MimeType:pdf OR MimeType:document) AND *tdd*`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.GroupNode{Nodes: []ast.Node{
&ast.StringNode{Key: "mediatype", Value: "pdf"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Key: "mediatype", Value: "document"},
}},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "name", Value: "*tdd*"},
},
},
want: query.NewConjunctionQuery([]query.Query{
query.NewDisjunctionQuery([]query.Query{
query.NewQueryStringQuery(`MimeType:application/pdf`),
query.NewQueryStringQuery(`MimeType:application/msword`),
query.NewQueryStringQuery(`MimeType:application/vnd.openxmlformats\-officedocument.wordprocessingml.document`),
query.NewQueryStringQuery(`MimeType:application/vnd.openxmlformats\-officedocument.wordprocessingml.form`),
query.NewQueryStringQuery(`MimeType:application/vnd.oasis.opendocument.text`),
query.NewQueryStringQuery(`MimeType:text/plain`),
query.NewQueryStringQuery(`MimeType:text/markdown`),
query.NewQueryStringQuery(`MimeType:application/rtf`),
query.NewQueryStringQuery(`MimeType:application/vnd.apple.pages`),
}),
query.NewQueryStringQuery(`Name_lowercase:*tdd*`),
}),
wantErr: false,
},
{
name: `John Smith`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Value: "John Smith +-=&|><!(){}[]^\"~: "},
},
},
want: query.NewConjunctionQuery([]query.Query{
query.NewQueryStringQuery(`Name_words:"john smith +-=&|><!(){}[]^\"~: "`),
}),
wantErr: false,
},
{
name: `quotes and backslashes in a phrase`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Key: "name", Value: `"Quote" C:\temp\`},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "content", Value: `foo\`},
},
},
want: query.NewConjunctionQuery([]query.Query{
query.NewQueryStringQuery(`Name_words:"\"quote\" c:\\temp\\"`),
query.NewQueryStringQuery(`Content:"foo\\"`),
}),
wantErr: false,
},
{
name: `quotes and backslashes in a wildcard`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Key: "name", Value: `*"Quote"\*`},
},
},
want: query.NewConjunctionQuery([]query.Query{
query.NewQueryStringQuery(`Name_lowercase:*\"quote\"\\*`),
}),
wantErr: false,
},
{
name: `quotes and backslashes in a term`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Key: "id", Value: `a"b\`},
},
},
want: query.NewConjunctionQuery([]query.Query{
query.NewQueryStringQuery(`ID:a\"b\\`),
}),
wantErr: false,
},
{
name: `values a query string can't carry`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Key: "tag", Value: `/`},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "tag", Value: `/a*/`},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "tag", Value: ``},
},
},
want: query.NewConjunctionQuery([]query.Query{
func() query.Query {
q := query.NewMatchQuery(`/`)
q.SetField("Tags_lowercase")
return q
}(),
func() query.Query {
q := query.NewWildcardQuery(`/a*/`)
q.SetField("Tags_lowercase")
return q
}(),
func() query.Query {
q := query.NewMatchQuery(``)
q.SetField("Tags_lowercase")
return q
}(),
}),
wantErr: false,
},
{
name: `missing right operand`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Value: "foo"},
&ast.OperatorNode{Value: "NOT"},
},
},
wantErr: true,
},
{
name: `no operands`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.OperatorNode{Value: "NOT"},
},
},
wantErr: true,
},
}
assert := tAssert.New(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := compile(searchquery.Normalize(tt.args, searchquery.ResolveField))
if (err != nil) != tt.wantErr {
t.Errorf("compile() error = %v, wantErr %v", err, tt.wantErr)
return
}
assert.Equal(tt.want, got)
})
}
}
// whatever a KQL value holds, the compiled query has to be one bleve can parse;
// a quote or a trailing backslash must not break out of a phrase or term
func Test_compileQuotesAndBackslashes(t *testing.T) {
keys := []string{"name", "content", "tag", "title", "mediatype", "path", "id", "driveid", "favorite", "audio.artist"}
values := []string{
``, `"`, `""`, `\`, `\\`, `foo\`, `\foo`, `\"`, `"\`, `a"b`, `"quoted"`, `C:\temp\new`,
`a/b`, `/`, `./a/b/`, `*"x"*`, `*\`, `*\*`, `foo\*`, `a b\`, `a:b`, `(a)`, `+-=&|><!{}[]^~`, ` `,
}
for _, key := range keys {
for _, value := range values {
for _, exact := range []bool{false, true} {
node := &ast.StringNode{Key: key, Value: value, Exact: exact}
t.Run(fmt.Sprintf("%s %t %s", key, exact, value), func(t *testing.T) {
got, err := compile(searchquery.Normalize(&ast.Ast{Nodes: []ast.Node{node}}, searchquery.ResolveField))
tAssert.NoError(t, err)
validatable, ok := got.(query.ValidatableQuery)
tAssert.True(t, ok)
tAssert.NoError(t, validatable.Validate())
})
}
}
}
}
func Test_escape(t *testing.T) {
type args struct {
str string
}
tests := []struct {
name string
args args
want string
}{
{
name: "all escaped",
args: args{
`+-=&|><!(){}[]^"~:\ `,
},
want: `\+\-\=\&\|\>\<\!\(\)\{\}\[\]\^\"\~\:\\\ `,
},
{
name: "no one escaped",
args: args{
`@#$%/*?`,
},
want: `@#$%/*?`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tAssert.Equalf(t, tt.want, bleveEscaper.Replace(tt.args.str), "bleveEscaper(%v)", tt.args.str)
})
}
}