Files
opencloud/services/search/pkg/query/normalize_test.go
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

194 lines
7.3 KiB
Go

package query_test
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/opencloud-eu/opencloud/pkg/ast"
"github.com/opencloud-eu/opencloud/services/search/pkg/query"
)
// This is the single place the shared KQL lowering pass is tested (field
// resolution, media-type expansion, group-key defaulting, pointer conversion).
// The backend query compilers consume its canonical output and must not re-test
// it.
func norm(nodes ...ast.Node) []ast.Node {
return query.Normalize(&ast.Ast{Nodes: nodes}, query.ResolveField).Nodes
}
var _ = Describe("ResolveField", func() {
It("resolves keys to canonical field names", func() {
Expect(query.ResolveField("")).To(Equal("Name")) // empty -> free-text default
Expect(query.ResolveField("NAME")).To(Equal("Name")) // canonical, case-insensitive key match
Expect(query.ResolveField("tag")).To(Equal("Tags")) // singular alias
Expect(query.ResolveField("mimetype")).To(Equal("MimeType")) // real field
Expect(query.ResolveField("photo.CAMERAMAKE")).To(Equal("photo.cameraMake")) // facet, case-insensitive key match
Expect(query.ResolveField("unknown.field")).To(Equal("unknown.field")) // unknown key: unchanged, becomes a dead query
})
})
var _ = Describe("FieldIsCaseInsensitive", func() {
It("reports the CaseInsensitive override fields", func() {
// keyword fields are case-insensitive by default, facets included
for _, f := range []string{"Name", "Title", "Tags", "audio.artist", "photo.cameraMake"} {
Expect(query.FieldIsCaseInsensitive(f)).To(BeTrue(), f)
}
// opted out (ids, favorites, path, mime type) or not a keyword at all
for _, f := range []string{"MimeType", "ID", "RootID", "ParentID", "Favorites", "Content", "Path", "Size", "unknown"} {
Expect(query.FieldIsCaseInsensitive(f)).To(BeFalse(), f)
}
})
})
var _ = Describe("FieldIsWordBroken", func() {
It("reports the word-broken fields, keywords unless opted out", func() {
for _, f := range []string{"Name", "Title", "audio.artist", "photo.cameraMake"} {
Expect(query.FieldIsWordBroken(f)).To(BeTrue(), f)
}
// opted out (labels, ids, mime type), paths and full text are not
for _, f := range []string{"Tags", "Favorites", "MimeType", "ID", "Content", "Path", "unknown"} {
Expect(query.FieldIsWordBroken(f)).To(BeFalse(), f)
}
})
})
var _ = Describe("FieldIsNumeric and FieldIsDatetime", func() {
It("report the field type, facets included", func() {
for _, f := range []string{"Size", "Type", "audio.duration", "photo.iso", "location.altitude"} {
Expect(query.FieldIsNumeric(f)).To(BeTrue(), f)
Expect(query.FieldIsDatetime(f)).To(BeFalse(), f)
}
Expect(query.FieldIsDatetime("Mtime")).To(BeTrue())
for _, f := range []string{"Name", "Tags", "Hidden", "Content", "Path", "unknown"} {
Expect(query.FieldIsNumeric(f)).To(BeFalse(), f)
Expect(query.FieldIsDatetime(f)).To(BeFalse(), f)
}
})
})
var _ = DescribeTable("ParseNumber",
func(value string, want float64, wantOK bool) {
got, ok := query.ParseNumber(value)
Expect(ok).To(Equal(wantOK))
Expect(got).To(Equal(want))
},
Entry("an integer", "50", 50.0, true),
Entry("a decimal", "1.5", 1.5, true),
Entry("a comparison", ">100", 0.0, false),
Entry("a range", "50..500", 0.0, false),
Entry("a wildcard", "5*", 0.0, false),
Entry("a word", "long", 0.0, false),
Entry("not a finite number", "NaN", 0.0, false),
Entry("nothing", "", 0.0, false),
)
var _ = Describe("Normalize", func() {
It("resolves fields and expands mediatype", func() {
got := norm(
&ast.StringNode{Key: "", Value: "free"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "TAG", Value: "x"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "photo.cameramake", Value: "canon"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "mediatype", Value: "file"},
&ast.OperatorNode{Value: "AND"},
ast.NumberNode{Key: "size", Value: 100},
)
Expect(got).To(Equal([]ast.Node{
&ast.StringNode{Key: "Name", Value: "free", CaseInsensitive: true},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "Tags", Value: "x", CaseInsensitive: true},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "photo.cameraMake", Value: "canon", CaseInsensitive: true},
&ast.OperatorNode{Value: "AND"},
&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},
}))
})
It("resolves driveId to RootID and completes the root id", func() {
got := norm(
&ast.StringNode{Key: "driveId", Value: "1$2"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "driveid", Value: "1$2!4"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "RootID", Value: "1$2"},
)
Expect(got).To(Equal([]ast.Node{
&ast.StringNode{Key: "RootID", Value: "1$2!2"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "RootID", Value: "1$2!4"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "RootID", Value: "1$2!2"},
}))
})
// A bare restriction inside a named group inherits the group key; a keyed
// child keeps its own key; a bare restriction in an unnamed group falls
// back to Name.
It("defaults group keys", func() {
got := norm(
&ast.GroupNode{Key: "author", Nodes: []ast.Node{
&ast.StringNode{Value: "b"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Key: "name", Value: "d"},
}},
&ast.OperatorNode{Value: "AND"},
&ast.GroupNode{Nodes: []ast.Node{
&ast.StringNode{Value: "e"},
}},
)
Expect(got).To(Equal([]ast.Node{
&ast.GroupNode{Key: "author", Nodes: []ast.Node{
&ast.StringNode{Key: "author", Value: "b"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Key: "Name", Value: "d", CaseInsensitive: true},
}},
&ast.OperatorNode{Value: "AND"},
&ast.GroupNode{Nodes: []ast.Node{
&ast.StringNode{Key: "Name", Value: "e", CaseInsensitive: true},
}},
}))
})
DescribeTable("normalizes path values to the stored relative form",
func(in, want string) {
got := norm(&ast.StringNode{Key: "path", Value: in})
Expect(got).To(Equal([]ast.Node{&ast.StringNode{Key: "Path", Value: want}}))
},
Entry("relative", "./parent", "./parent"),
Entry("trailing slash", "./parent/", "./parent"),
Entry("absolute", "/parent", "./parent"),
Entry("bare", "parent", "./parent"),
Entry("nested bare", "parent/child.jpg", "./parent/child.jpg"),
Entry("root", "/", "."),
Entry("empty", "", "."),
Entry("dot", ".", "."),
Entry("wildcard", "./par*/", "./par*"),
Entry("bare wildcard", "parent/*", "./parent/*"),
Entry("leading wildcard", "*child*", "*child*"),
Entry("leading wildcard covers the prefix", "*/parent", "*/parent"),
Entry("leading single-char wildcard covers the prefix", "?/parent", "?/parent"),
Entry("leading wildcard trailing slash", "*/parent/", "*/parent"),
)
It("converts value nodes to pointers", func() {
got := norm(
ast.StringNode{Key: "name", Value: "x"},
ast.OperatorNode{Value: "AND"},
ast.DateTimeNode{Key: "mtime"},
)
Expect(got).To(Equal([]ast.Node{
&ast.StringNode{Key: "Name", Value: "x", CaseInsensitive: true},
&ast.OperatorNode{Value: "AND"},
&ast.DateTimeNode{Key: "Mtime"},
}))
})
})