refactor(search): resolve KQL keys next to the field type table

The field facts lived in two packages: search had the field type table,
query had the key resolution and a second copy of the field types. Key
resolution and the geopoint check move to search, next to FieldType, and
query reads them from there. query stays the lowering pass for the
engines; search can now check a parsed query against the fields without
an import cycle.
This commit is contained in:
Dominik Schmidt committed 2026-12-04 11:01:26 +01:00
1 parent 5e9e52459b
commit 00f85c5496
7 files changed
+55 -58

No files matched your search

+1 -1
View File
@@ -65,7 +65,7 @@ func (i *serialIndex) Batch(b *bleve.Batch) error {
// Search executes a search request operation within the index.
func (b *Backend) Search(ctx context.Context, sir *search.IndexQuery) (*search.IndexResult, error) {
createdQuery, err := bleveQuery.Compiler{}.Compile(searchQuery.Normalize(sir.Query, searchQuery.ResolveField))
createdQuery, err := bleveQuery.Compiler{}.Compile(searchQuery.Normalize(sir.Query, search.ResolveField))
if err != nil {
return nil, err
}
@@ -6,6 +6,7 @@ import (
"github.com/opencloud-eu/opencloud/pkg/ast"
"github.com/opencloud-eu/opencloud/services/search/pkg/opensearch/internal/osu"
"github.com/opencloud-eu/opencloud/services/search/pkg/query"
"github.com/opencloud-eu/opencloud/services/search/pkg/search"
)
var (
@@ -14,7 +15,7 @@ var (
func KQLToOpenSearchBoolQuery(kqlAst *ast.Ast) (*osu.BoolQuery, error) {
// shared lowering: field resolution, media-type expansion, value lowercasing.
kqlAst = query.Normalize(kqlAst, query.ResolveField)
kqlAst = query.Normalize(kqlAst, search.ResolveField)
builder, err := TranspileKQLToOpenSearch(kqlAst.Nodes)
if err != nil {
@@ -10,6 +10,7 @@ import (
"github.com/opencloud-eu/opencloud/pkg/ast"
searchquery "github.com/opencloud-eu/opencloud/services/search/pkg/query"
"github.com/opencloud-eu/opencloud/services/search/pkg/search"
)
var timeMustParse = func(t *testing.T, ts string) time.Time {
@@ -681,7 +682,7 @@ func Test_compile(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := compile(searchquery.Normalize(tt.args, searchquery.ResolveField))
got, err := compile(searchquery.Normalize(tt.args, search.ResolveField))
if (err != nil) != tt.wantErr {
t.Errorf("compile() error = %v, wantErr %v", err, tt.wantErr)
@@ -706,7 +707,7 @@ func Test_compileQuotesAndBackslashes(t *testing.T) {
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))
got, err := compile(searchquery.Normalize(&ast.Ast{Nodes: []ast.Node{node}}, search.ResolveField))
tAssert.NoError(t, err)
validatable, ok := got.(query.ValidatableQuery)
+2 -12
View File
@@ -6,6 +6,7 @@ import (
"github.com/opencloud-eu/opencloud/pkg/ast"
"github.com/opencloud-eu/opencloud/services/search/pkg/query"
"github.com/opencloud-eu/opencloud/services/search/pkg/search"
)
// This is the single place the shared KQL lowering pass is tested (field
@@ -14,20 +15,9 @@ import (
// it.
func norm(nodes ...ast.Node) []ast.Node {
return query.Normalize(&ast.Ast{Nodes: nodes}, query.ResolveField).Nodes
return query.Normalize(&ast.Ast{Nodes: nodes}, search.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
+3 -42
View File
@@ -4,7 +4,6 @@ import (
"math"
"reflect"
"strconv"
"strings"
"sync"
"github.com/opencloud-eu/opencloud/pkg/kql"
@@ -12,36 +11,12 @@ import (
"github.com/opencloud-eu/opencloud/services/search/pkg/search"
)
// aliases are KQL spellings the derived index can't produce (fields are plural).
var aliases = map[string]string{
"tag": "Tags",
"favorite": "Favorites",
"driveid": "RootID",
}
// fieldIndex maps a lowercased KQL key to its canonical field name ("" is the
// bare-search default).
var fieldIndex = sync.OnceValue(func() map[string]string {
idx := mapping.FieldNameIndex(reflect.TypeFor[search.Resource](), search.Resource{}.SearchFieldOverrides())
for k, v := range aliases {
idx[k] = v
}
idx[""] = idx["name"]
return idx
})
// siblingFields lists which search siblings every field carries, from the
// resource struct and its overrides.
var siblingFields = sync.OnceValue(func() map[string]mapping.Siblings {
return mapping.SearchSiblings(reflect.TypeFor[search.Resource](), search.Resource{}.SearchFieldOverrides())
})
// fieldTypes holds the mapping type of every field, from the resource struct
// and its overrides.
var fieldTypes = sync.OnceValue(func() map[string]string {
return mapping.FieldTypes(reflect.TypeFor[search.Resource](), search.Resource{}.SearchFieldOverrides())
})
// pathFields are hierarchical path fields (TypePath), derived from the overrides.
var pathFields = sync.OnceValue(func() map[string]struct{} {
out := map[string]struct{}{}
@@ -65,14 +40,6 @@ var fulltextFields = sync.OnceValue(func() map[string]struct{} {
return out
})
// ResolveField maps a KQL key to its canonical field name; unknown keys pass through.
func ResolveField(name string) string {
if v, ok := fieldIndex()[strings.ToLower(name)]; ok {
return v
}
return name
}
// normalizedValueFields have their stored values normalized to lowercase at
// index time, so query values fold to match even though the fields themselves
// are case-preserved keywords.
@@ -115,12 +82,12 @@ func FieldIsWordBroken(field string) bool {
// FieldIsNumeric reports whether a field is indexed as a number, so a range
// restriction can target it.
func FieldIsNumeric(field string) bool {
return fieldTypes()[field] == mapping.TypeNumeric
return search.FieldType(field) == mapping.TypeNumeric
}
// FieldIsDatetime reports whether a field holds a point in time.
func FieldIsDatetime(field string) bool {
return fieldTypes()[field] == mapping.TypeDatetime
return search.FieldType(field) == mapping.TypeDatetime
}
// ParseNumber reads the value of a restriction on a numeric field. ok is false
@@ -133,16 +100,10 @@ func ParseNumber(value string) (float64, bool) {
return v, true
}
// FieldIsGeopoint reports whether a field is indexed as a geopoint, so a geo
// predicate can target its geopoint sibling.
func FieldIsGeopoint(field string) bool {
return fieldTypes()[field] == mapping.TypeGeopoint
}
// GeopointField names the indexed geopoint sibling of a field, erroring when
// the field is not a geopoint.
func GeopointField(field string) (string, error) {
if !FieldIsGeopoint(field) {
if !search.FieldIsGeopoint(field) {
return "", &kql.GeoFieldError{Key: field}
}
return field + mapping.GeopointSuffix, nil
+33
View File
@@ -2,6 +2,7 @@ package search
import (
"reflect"
"strings"
"sync"
"github.com/opencloud-eu/opencloud/services/search/pkg/mapping"
@@ -25,3 +26,35 @@ func AggregatableFieldType(field string) string {
}
return FieldType(field)
}
// FieldIsGeopoint reports whether a field is indexed as a geopoint, so a geo
// predicate can target its geopoint sibling.
func FieldIsGeopoint(field string) bool {
return FieldType(field) == mapping.TypeGeopoint
}
// aliases are KQL spellings the derived index can't produce (fields are plural).
var aliases = map[string]string{
"tag": "Tags",
"favorite": "Favorites",
"driveid": "RootID",
}
// fieldIndex maps a lowercased KQL key to its canonical field name ("" is the
// bare-search default).
var fieldIndex = sync.OnceValue(func() map[string]string {
idx := mapping.FieldNameIndex(reflect.TypeFor[Resource](), Resource{}.SearchFieldOverrides())
for k, v := range aliases {
idx[k] = v
}
idx[""] = idx["name"]
return idx
})
// ResolveField maps a KQL key to its canonical field name; unknown keys pass through.
func ResolveField(name string) string {
if v, ok := fieldIndex()[strings.ToLower(name)]; ok {
return v
}
return name
}
+11
View File
@@ -54,3 +54,14 @@ var _ = Describe("AggregatableFieldType", func() {
Entry("unknown", "audio.nonexistent", ""),
)
})
var _ = Describe("ResolveField", func() {
It("resolves keys to canonical field names", func() {
Expect(search.ResolveField("")).To(Equal("Name")) // empty -> free-text default
Expect(search.ResolveField("NAME")).To(Equal("Name")) // canonical, case-insensitive key match
Expect(search.ResolveField("tag")).To(Equal("Tags")) // singular alias
Expect(search.ResolveField("mimetype")).To(Equal("MimeType")) // real field
Expect(search.ResolveField("photo.CAMERAMAKE")).To(Equal("photo.cameraMake")) // facet, case-insensitive key match
Expect(search.ResolveField("unknown.field")).To(Equal("unknown.field")) // unknown key: unchanged, becomes a dead query
})
})