mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-12-05 14:39:23 -05:00
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.
111 lines
3.4 KiB
Go
111 lines
3.4 KiB
Go
package query
|
|
|
|
import (
|
|
"math"
|
|
"reflect"
|
|
"strconv"
|
|
"sync"
|
|
|
|
"github.com/opencloud-eu/opencloud/pkg/kql"
|
|
"github.com/opencloud-eu/opencloud/services/search/pkg/mapping"
|
|
"github.com/opencloud-eu/opencloud/services/search/pkg/search"
|
|
)
|
|
|
|
// 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())
|
|
})
|
|
|
|
// pathFields are hierarchical path fields (TypePath), derived from the overrides.
|
|
var pathFields = sync.OnceValue(func() map[string]struct{} {
|
|
out := map[string]struct{}{}
|
|
for field, opts := range (search.Resource{}).SearchFieldOverrides() {
|
|
if opts.Type == mapping.TypePath {
|
|
out[field] = struct{}{}
|
|
}
|
|
}
|
|
return out
|
|
})
|
|
|
|
// fulltextFields are analyzed full-text fields (TypeFulltext), derived from the
|
|
// overrides.
|
|
var fulltextFields = sync.OnceValue(func() map[string]struct{} {
|
|
out := map[string]struct{}{}
|
|
for field, opts := range (search.Resource{}).SearchFieldOverrides() {
|
|
if opts.Type == mapping.TypeFulltext {
|
|
out[field] = struct{}{}
|
|
}
|
|
}
|
|
return out
|
|
})
|
|
|
|
// 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.
|
|
var normalizedValueFields = map[string]struct{}{
|
|
"MimeType": {},
|
|
"Type": {},
|
|
"Hidden": {},
|
|
}
|
|
|
|
// FieldValueIsNormalized reports whether a field's stored values are
|
|
// normalized lowercase.
|
|
func FieldValueIsNormalized(field string) bool {
|
|
_, ok := normalizedValueFields[field]
|
|
return ok
|
|
}
|
|
|
|
// FieldIsCaseInsensitive reports whether a field's default search is case-insensitive.
|
|
func FieldIsCaseInsensitive(field string) bool {
|
|
return siblingFields()[field].Lowercase
|
|
}
|
|
|
|
// FieldIsPath reports whether a field is a hierarchical path field.
|
|
func FieldIsPath(field string) bool {
|
|
_, ok := pathFields()[field]
|
|
return ok
|
|
}
|
|
|
|
// FieldIsFulltext reports whether a field is an analyzed full-text field.
|
|
func FieldIsFulltext(field string) bool {
|
|
_, ok := fulltextFields()[field]
|
|
return ok
|
|
}
|
|
|
|
// FieldIsWordBroken reports whether a field is split into words, so a value
|
|
// without a wildcard matches it as a phrase of those words instead of as a whole.
|
|
func FieldIsWordBroken(field string) bool {
|
|
return siblingFields()[field].Words
|
|
}
|
|
|
|
// FieldIsNumeric reports whether a field is indexed as a number, so a range
|
|
// restriction can target it.
|
|
func FieldIsNumeric(field string) bool {
|
|
return search.FieldType(field) == mapping.TypeNumeric
|
|
}
|
|
|
|
// FieldIsDatetime reports whether a field holds a point in time.
|
|
func FieldIsDatetime(field string) bool {
|
|
return search.FieldType(field) == mapping.TypeDatetime
|
|
}
|
|
|
|
// ParseNumber reads the value of a restriction on a numeric field. ok is false
|
|
// for anything but a finite number, ">100" and "50..500" included.
|
|
func ParseNumber(value string) (float64, bool) {
|
|
v, err := strconv.ParseFloat(value, 64)
|
|
if err != nil || math.IsNaN(v) || math.IsInf(v, 0) {
|
|
return 0, false
|
|
}
|
|
return v, true
|
|
}
|
|
|
|
// GeopointField names the indexed geopoint sibling of a field, erroring when
|
|
// the field is not a geopoint.
|
|
func GeopointField(field string) (string, error) {
|
|
if !search.FieldIsGeopoint(field) {
|
|
return "", &kql.GeoFieldError{Key: field}
|
|
}
|
|
return field + mapping.GeopointSuffix, nil
|
|
}
|