Files
opencloud/services/search/pkg/query/normalize.go
T
Dominik Schmidt 5e9e52459b refactor(search): read geopoint fields from the shared field type table
The geo predicates kept their own table of geopoint fields and resolved the
key themselves, and each engine had its own copy of the lookup. They now go
the way of every other field: the normalizer resolves the key of a geo node,
FieldIsGeopoint asks the field type table that FieldIsNumeric and
FieldIsDatetime read, and both engines get the sibling name from
GeopointField. A key in another spelling is covered by GEO-11.
2026-12-04 11:01:26 +01:00

124 lines
3.7 KiB
Go

// Package query provides functions to work with the different search query flavours.
package query
import (
"strconv"
"strings"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/opencloud-eu/reva/v3/pkg/utils"
"reflect"
"github.com/opencloud-eu/opencloud/pkg/ast"
"github.com/opencloud-eu/opencloud/services/search/pkg/query/mimetype"
"github.com/opencloud-eu/opencloud/services/search/pkg/search"
)
// Normalize is the shared KQL lowering pass between parse and compile: it
// resolves keys to real field names (via resolve) and expands media-type
// restrictions, so the backends compile a plain field:value AST.
func Normalize(a *ast.Ast, resolve func(string) string) *ast.Ast {
a.Nodes = normalizeNodes(a.Nodes, resolve, "")
return a
}
// normalizeNodes rewrites nodes in place. defaultKey is what a bare restriction
// inherits: its enclosing group's key, or "" at the top level.
func normalizeNodes(nodes []ast.Node, resolve func(string) string, defaultKey string) []ast.Node {
resolveKey := func(key string) string {
if key == "" && defaultKey != "" {
return defaultKey // bare child inherits the group key
}
return resolve(key)
}
out := make([]ast.Node, 0, len(nodes))
for _, n := range nodes {
n = toPointer(n) // ensure a pointer so in-place key rewrites persist
switch node := n.(type) {
case *ast.StringNode:
node.Key = resolveKey(node.Key)
if FieldIsPath(node.Key) {
if strings.IndexAny(node.Value, "*?") == 0 {
// a leading wildcard stands in for the "./" prefix, anchoring would break it
node.Value = strings.TrimSuffix(node.Value, "/")
} else {
node.Value = utils.MakeRelativePath(node.Value)
}
}
if FieldValueIsNormalized(node.Key) {
node.Value = strings.ToLower(node.Value)
}
if node.Key == "Type" {
node.Value = resourceType(node.Value)
}
if node.Key == "RootID" {
node.Value = search.CompleteRootID(node.Value)
}
if exp := mimetype.Expand(node.Key, node.Value); exp != nil {
out = append(out, normalizeNodes(exp, resolve, defaultKey)...)
continue
}
node.CaseInsensitive = FieldIsCaseInsensitive(node.Key)
out = append(out, node)
case *ast.DateTimeNode:
node.Key = resolveKey(node.Key)
out = append(out, node)
case *ast.BooleanNode:
node.Key = resolveKey(node.Key)
out = append(out, node)
case *ast.NumberNode:
node.Key = resolveKey(node.Key)
out = append(out, node)
case *ast.GeoDistanceNode:
node.Key = resolveKey(node.Key)
out = append(out, node)
case *ast.GeoBoundingBoxNode:
node.Key = resolveKey(node.Key)
out = append(out, node)
case *ast.GeoPolygonNode:
node.Key = resolveKey(node.Key)
out = append(out, node)
case *ast.GroupNode:
groupKey := defaultKey
if node.Key != "" {
node.Key = resolve(node.Key)
groupKey = node.Key
}
node.Nodes = normalizeNodes(node.Nodes, resolve, groupKey)
out = append(out, node)
default:
out = append(out, n)
}
}
return out
}
// toPointer returns n as a pointer; the parser emits some nodes by value and the
// in-place key rewrites would be lost on those.
func toPointer(n ast.Node) ast.Node {
rv := reflect.ValueOf(n)
if rv.Kind() == reflect.Ptr {
return n
}
ptr := reflect.New(rv.Type())
ptr.Elem().Set(rv)
if pn, ok := ptr.Interface().(ast.Node); ok {
return pn
}
return n
}
// resourceType maps the type categories to the stored resource type value;
// unknown values pass through and become dead term queries.
func resourceType(value string) string {
switch strings.ToLower(value) {
case "file":
return strconv.FormatUint(uint64(provider.ResourceType_RESOURCE_TYPE_FILE), 10)
case "folder":
return strconv.FormatUint(uint64(provider.ResourceType_RESOURCE_TYPE_CONTAINER), 10)
default:
return value
}
}