mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-11-27 10:34:42 -05:00
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.
This commit is contained in:
1 parent
2ec4822f25
commit
6340f2ecd8
6 files changed
+32
-45
No files matched your search
@@ -14,16 +14,6 @@ import (
|
||||
"github.com/opencloud-eu/opencloud/services/search/pkg/query"
|
||||
)
|
||||
|
||||
// geoQueryField resolves a KQL geo key to its indexed geopoint field, erroring
|
||||
// when the key is not a geopoint field.
|
||||
func geoQueryField(key string) (string, error) {
|
||||
field, ok := query.ResolveGeoField(key)
|
||||
if !ok {
|
||||
return "", &kql.GeoFieldError{Key: key}
|
||||
}
|
||||
return field, nil
|
||||
}
|
||||
|
||||
func TranspileKQLToOpenSearch(nodes []ast.Node) (osu.Builder, error) {
|
||||
return kqlOpensearchTranspiler{}.Transpile(nodes)
|
||||
}
|
||||
@@ -213,7 +203,7 @@ func (t kqlOpensearchTranspiler) toBuilder(node ast.Node) (osu.Builder, error) {
|
||||
|
||||
return group, nil
|
||||
case *ast.GeoDistanceNode:
|
||||
field, err := geoQueryField(node.Key)
|
||||
field, err := query.GeopointField(node.Key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -221,14 +211,14 @@ func (t kqlOpensearchTranspiler) toBuilder(node ast.Node) (osu.Builder, error) {
|
||||
Distance(strconv.FormatFloat(node.Radius, 'f', -1, 64)+"m").
|
||||
Point(node.Lat, node.Lon), nil
|
||||
case *ast.GeoBoundingBoxNode:
|
||||
field, err := geoQueryField(node.Key)
|
||||
field, err := query.GeopointField(node.Key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return osu.NewGeoBoundingBoxQuery(field).
|
||||
Box(node.MinLat, node.MinLon, node.MaxLat, node.MaxLon), nil
|
||||
case *ast.GeoPolygonNode:
|
||||
field, err := geoQueryField(node.Key)
|
||||
field, err := query.GeopointField(node.Key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -517,6 +517,7 @@ Fixtures:
|
||||
| GEO-08 | `name:geo.distance(48.2082, 16.3738, 5km)` | bad request | bad request | bad request | ✅ |
|
||||
| GEO-09 | `location:geo.distance(48.2082, 1...(47.9, 16.1, 48.3, 16.5)` | vienna.txt | vienna.txt | vienna.txt | ✅ |
|
||||
| GEO-10 | `location:geo.distance(48.2082, 16.3738, 5km) AND name:bruck*` | no match | no match | no match | ✅ |
|
||||
| GEO-11 | `Location:geo.distance(48.2082, 16.3738, 5km)` | vienna.txt | vienna.txt | vienna.txt | ✅ |
|
||||
|
||||
### scope
|
||||
|
||||
|
||||
@@ -38,6 +38,8 @@ func geoGroup() queryGroup {
|
||||
// geo predicates compose with the rest of the query language
|
||||
{id: 9, query: `location:geo.distance(48.2082, 16.3738, 50km) AND location:geo.bbox(47.9, 16.1, 48.3, 16.5)`, want: []string{"vienna.txt"}},
|
||||
{id: 10, query: `location:geo.distance(48.2082, 16.3738, 5km) AND name:bruck*`},
|
||||
// the field name resolves like any other key
|
||||
{id: 11, query: `Location:geo.distance(48.2082, 16.3738, 5km)`, want: []string{"vienna.txt"}},
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -68,16 +68,6 @@ func fieldQuery(field, value string) bleveQuery.Query {
|
||||
return q
|
||||
}
|
||||
|
||||
// geoQueryField resolves a KQL geo key to its indexed geopoint field, erroring
|
||||
// when the key is not a geopoint field.
|
||||
func geoQueryField(key string) (string, error) {
|
||||
field, ok := searchQuery.ResolveGeoField(key)
|
||||
if !ok {
|
||||
return "", &kql.GeoFieldError{Key: key}
|
||||
}
|
||||
return field, nil
|
||||
}
|
||||
|
||||
// Compiler represents a KQL query search string to the bleve query formatter.
|
||||
type Compiler struct{}
|
||||
|
||||
@@ -241,7 +231,7 @@ func walk(offset int, nodes []ast.Node) (bleveQuery.Query, int, error) {
|
||||
next = q
|
||||
}
|
||||
case *ast.GeoDistanceNode:
|
||||
field, err := geoQueryField(n.Key)
|
||||
field, err := searchQuery.GeopointField(n.Key)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
@@ -253,7 +243,7 @@ func walk(offset int, nodes []ast.Node) (bleveQuery.Query, int, error) {
|
||||
next = q
|
||||
}
|
||||
case *ast.GeoBoundingBoxNode:
|
||||
field, err := geoQueryField(n.Key)
|
||||
field, err := searchQuery.GeopointField(n.Key)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
@@ -266,7 +256,7 @@ func walk(offset int, nodes []ast.Node) (bleveQuery.Query, int, error) {
|
||||
next = q
|
||||
}
|
||||
case *ast.GeoPolygonNode:
|
||||
field, err := geoQueryField(n.Key)
|
||||
field, err := searchQuery.GeopointField(n.Key)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
@@ -69,6 +69,15 @@ func normalizeNodes(nodes []ast.Node, resolve func(string) string, defaultKey st
|
||||
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 != "" {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"strings"
|
||||
"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"
|
||||
)
|
||||
@@ -132,23 +133,17 @@ func ParseNumber(value string) (float64, bool) {
|
||||
return v, true
|
||||
}
|
||||
|
||||
// geopointFields maps a lowercased KQL key to its indexed geopoint sibling field,
|
||||
// derived from the TypeGeopoint entries in the resource field overrides (e.g.
|
||||
// "location" -> "location_geopoint", "journey.start" -> "journey.start_geopoint").
|
||||
var geopointFields = sync.OnceValue(func() map[string]string {
|
||||
out := map[string]string{}
|
||||
for key, opts := range (search.Resource{}).SearchFieldOverrides() {
|
||||
if opts.Type == mapping.TypeGeopoint {
|
||||
out[strings.ToLower(key)] = key + mapping.GeopointSuffix
|
||||
}
|
||||
}
|
||||
return out
|
||||
})
|
||||
|
||||
// ResolveGeoField maps a KQL key to its indexed geopoint field name. ok is false
|
||||
// when the key is not a geopoint field, so callers can reject geo predicates on
|
||||
// non-geo fields.
|
||||
func ResolveGeoField(name string) (string, bool) {
|
||||
f, ok := geopointFields()[strings.ToLower(name)]
|
||||
return f, ok
|
||||
// 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) {
|
||||
return "", &kql.GeoFieldError{Key: field}
|
||||
}
|
||||
return field + mapping.GeopointSuffix, nil
|
||||
}
|
||||
Reference in new issue
Block a user