mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-12-05 05:43:36 -05:00
feat(search): transpile geo KQL predicates for opensearch
Adds geo_distance/geo_bounding_box query builders plus a geo_shape polygon query (relation intersects, the modern replacement for the deprecated geo_polygon, still valid against geo_point). Wires the geo AST nodes through the KQL transpiler onto the <field>_geopoint sibling.
This commit is contained in:
1 parent
e1fb59232d
commit
13b00b9f8a
4 files changed
+201
No files matched your search
@@ -14,6 +14,12 @@ import (
|
||||
"github.com/opencloud-eu/opencloud/services/search/pkg/query"
|
||||
)
|
||||
|
||||
// geoField maps a KQL geo key to its indexed geopoint sibling, e.g.
|
||||
// "location" to "location_geopoint".
|
||||
func geoField(key string) string {
|
||||
return strings.ToLower(key) + mapping.GeopointSuffix
|
||||
}
|
||||
|
||||
func TranspileKQLToOpenSearch(nodes []ast.Node) (osu.Builder, error) {
|
||||
return kqlOpensearchTranspiler{}.Transpile(nodes)
|
||||
}
|
||||
@@ -202,6 +208,19 @@ func (t kqlOpensearchTranspiler) toBuilder(node ast.Node) (osu.Builder, error) {
|
||||
}
|
||||
|
||||
return group, nil
|
||||
case *ast.GeoDistanceNode:
|
||||
return osu.NewGeoDistanceQuery(geoField(node.Key)).
|
||||
Distance(strconv.FormatFloat(node.Radius, 'f', -1, 64)+"m").
|
||||
Point(node.Lat, node.Lon), nil
|
||||
case *ast.GeoBoundingBoxNode:
|
||||
return osu.NewGeoBoundingBoxQuery(geoField(node.Key)).
|
||||
Box(node.MinLat, node.MinLon, node.MaxLat, node.MaxLon), nil
|
||||
case *ast.GeoPolygonNode:
|
||||
q := osu.NewGeoPolygonQuery(geoField(node.Key))
|
||||
for _, p := range node.Points {
|
||||
q.Point(p.Lat, p.Lon)
|
||||
}
|
||||
return q, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("%w: %T", ErrUnsupportedNodeType, node)
|
||||
|
||||
@@ -473,6 +473,27 @@ func TestTranspileKQLToOpenSearch(t *testing.T) {
|
||||
osu.NewTermQuery[string]("d").Value("d"),
|
||||
),
|
||||
},
|
||||
{
|
||||
Name: "geo_distance - geo distance node",
|
||||
Got: &ast.Ast{Nodes: []ast.Node{
|
||||
&ast.GeoDistanceNode{Key: "location", Lat: 48.2, Lon: 16.3, Radius: 5000},
|
||||
}},
|
||||
Want: osu.NewGeoDistanceQuery("location_geopoint").Distance("5000m").Point(48.2, 16.3),
|
||||
},
|
||||
{
|
||||
Name: "geo_bounding_box - geo bbox node",
|
||||
Got: &ast.Ast{Nodes: []ast.Node{
|
||||
&ast.GeoBoundingBoxNode{Key: "location", MinLat: 47.9, MinLon: 16.1, MaxLat: 48.3, MaxLon: 16.5},
|
||||
}},
|
||||
Want: osu.NewGeoBoundingBoxQuery("location_geopoint").Box(47.9, 16.1, 48.3, 16.5),
|
||||
},
|
||||
{
|
||||
Name: "geo_polygon - geo polygon node",
|
||||
Got: &ast.Ast{Nodes: []ast.Node{
|
||||
&ast.GeoPolygonNode{Key: "location", Points: []ast.GeoPoint{{Lat: 48.3, Lon: 16.1}, {Lat: 48.3, Lon: 16.5}, {Lat: 47.9, Lon: 16.5}}},
|
||||
}},
|
||||
Want: osu.NewGeoPolygonQuery("location_geopoint").Point(48.3, 16.1).Point(48.3, 16.5).Point(47.9, 16.5),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
package osu
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
// GeoDistanceQuery builds a geo_distance query over a geo_point field.
|
||||
type GeoDistanceQuery struct {
|
||||
field string
|
||||
distance string
|
||||
lat, lon float64
|
||||
}
|
||||
|
||||
func NewGeoDistanceQuery(field string) *GeoDistanceQuery {
|
||||
return &GeoDistanceQuery{field: field}
|
||||
}
|
||||
|
||||
func (q *GeoDistanceQuery) Distance(v string) *GeoDistanceQuery {
|
||||
q.distance = v
|
||||
return q
|
||||
}
|
||||
|
||||
func (q *GeoDistanceQuery) Point(lat, lon float64) *GeoDistanceQuery {
|
||||
q.lat, q.lon = lat, lon
|
||||
return q
|
||||
}
|
||||
|
||||
func (q *GeoDistanceQuery) Map() (map[string]any, error) {
|
||||
return map[string]any{
|
||||
"geo_distance": map[string]any{
|
||||
"distance": q.distance,
|
||||
q.field: map[string]any{"lat": q.lat, "lon": q.lon},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (q *GeoDistanceQuery) MarshalJSON() ([]byte, error) { return marshalMap(q) }
|
||||
|
||||
// GeoBoundingBoxQuery builds a geo_bounding_box query over a geo_point field.
|
||||
type GeoBoundingBoxQuery struct {
|
||||
field string
|
||||
minLat, minLon, maxLat, maxLon float64
|
||||
}
|
||||
|
||||
func NewGeoBoundingBoxQuery(field string) *GeoBoundingBoxQuery {
|
||||
return &GeoBoundingBoxQuery{field: field}
|
||||
}
|
||||
|
||||
func (q *GeoBoundingBoxQuery) Box(minLat, minLon, maxLat, maxLon float64) *GeoBoundingBoxQuery {
|
||||
q.minLat, q.minLon, q.maxLat, q.maxLon = minLat, minLon, maxLat, maxLon
|
||||
return q
|
||||
}
|
||||
|
||||
func (q *GeoBoundingBoxQuery) Map() (map[string]any, error) {
|
||||
return map[string]any{
|
||||
"geo_bounding_box": map[string]any{
|
||||
q.field: map[string]any{
|
||||
"top_left": map[string]any{"lat": q.maxLat, "lon": q.minLon},
|
||||
"bottom_right": map[string]any{"lat": q.minLat, "lon": q.maxLon},
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (q *GeoBoundingBoxQuery) MarshalJSON() ([]byte, error) { return marshalMap(q) }
|
||||
|
||||
// GeoPolygonQuery builds a point-in-polygon query over a geo_point field. It
|
||||
// emits a geo_shape query (relation intersects), the modern replacement for the
|
||||
// deprecated geo_polygon; geo_shape queries still run against geo_point fields.
|
||||
type GeoPolygonQuery struct {
|
||||
field string
|
||||
points [][2]float64 // GeoJSON [lon, lat] order
|
||||
}
|
||||
|
||||
func NewGeoPolygonQuery(field string) *GeoPolygonQuery {
|
||||
return &GeoPolygonQuery{field: field}
|
||||
}
|
||||
|
||||
func (q *GeoPolygonQuery) Point(lat, lon float64) *GeoPolygonQuery {
|
||||
q.points = append(q.points, [2]float64{lon, lat})
|
||||
return q
|
||||
}
|
||||
|
||||
func (q *GeoPolygonQuery) Map() (map[string]any, error) {
|
||||
ring := make([][2]float64, len(q.points))
|
||||
copy(ring, q.points)
|
||||
// GeoJSON polygon rings must be closed.
|
||||
if n := len(ring); n > 0 && ring[0] != ring[n-1] {
|
||||
ring = append(ring, ring[0])
|
||||
}
|
||||
return map[string]any{
|
||||
"geo_shape": map[string]any{
|
||||
q.field: map[string]any{
|
||||
"shape": map[string]any{
|
||||
"type": "polygon",
|
||||
"coordinates": [][][2]float64{ring},
|
||||
},
|
||||
"relation": "intersects",
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (q *GeoPolygonQuery) MarshalJSON() ([]byte, error) { return marshalMap(q) }
|
||||
|
||||
func marshalMap(b Builder) ([]byte, error) {
|
||||
data, err := b.Map()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return json.Marshal(data)
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package osu_test
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"github.com/opencloud-eu/opencloud/services/search/pkg/opensearch/internal/osu"
|
||||
)
|
||||
|
||||
var _ = Describe("geo query builders", func() {
|
||||
It("builds a geo_distance query", func() {
|
||||
q := osu.NewGeoDistanceQuery("location_geopoint").Distance("5000m").Point(48.2, 16.3)
|
||||
Expect(q.Map()).To(Equal(map[string]any{
|
||||
"geo_distance": map[string]any{
|
||||
"distance": "5000m",
|
||||
"location_geopoint": map[string]any{"lat": 48.2, "lon": 16.3},
|
||||
},
|
||||
}))
|
||||
})
|
||||
|
||||
It("builds a geo_bounding_box query with top-left and bottom-right corners", func() {
|
||||
q := osu.NewGeoBoundingBoxQuery("location_geopoint").Box(47.9, 16.1, 48.3, 16.5)
|
||||
Expect(q.Map()).To(Equal(map[string]any{
|
||||
"geo_bounding_box": map[string]any{
|
||||
"location_geopoint": map[string]any{
|
||||
"top_left": map[string]any{"lat": 48.3, "lon": 16.1},
|
||||
"bottom_right": map[string]any{"lat": 47.9, "lon": 16.5},
|
||||
},
|
||||
},
|
||||
}))
|
||||
})
|
||||
|
||||
It("builds a geo_shape polygon query with a closed GeoJSON ring", func() {
|
||||
q := osu.NewGeoPolygonQuery("location_geopoint").
|
||||
Point(48.3, 16.1).Point(48.3, 16.5).Point(47.9, 16.5)
|
||||
Expect(q.Map()).To(Equal(map[string]any{
|
||||
"geo_shape": map[string]any{
|
||||
"location_geopoint": map[string]any{
|
||||
"shape": map[string]any{
|
||||
"type": "polygon",
|
||||
// GeoJSON [lon, lat], ring closed back to the first point.
|
||||
"coordinates": [][][2]float64{{
|
||||
{16.1, 48.3}, {16.5, 48.3}, {16.5, 47.9}, {16.1, 48.3},
|
||||
}},
|
||||
},
|
||||
"relation": "intersects",
|
||||
},
|
||||
},
|
||||
}))
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user