mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-11-27 10:34:42 -05:00
feat(search): compile geo KQL predicates for bleve
Translates the geo AST nodes to bleve GeoDistance/GeoBoundingBox/ GeoBoundingPolygon queries over the indexed <field>_geopoint sibling.
This commit is contained in:
1 parent
68a601995b
commit
21833c9d8c
2 files changed
+84
No files matched your search
@@ -0,0 +1,48 @@
|
||||
package bleve_test
|
||||
|
||||
import (
|
||||
bleveSearch "github.com/blevesearch/bleve/v2"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
qbleve "github.com/opencloud-eu/opencloud/services/search/pkg/query/bleve"
|
||||
)
|
||||
|
||||
var _ = Describe("Geo KQL predicates", func() {
|
||||
// Nuremberg-ish point indexed by geoFixture (see geo_verify_test.go).
|
||||
const lat, lon = 49.48675890884328, 11.103870357204285
|
||||
|
||||
// hits compiles the KQL query through the full bleve pipeline and searches a
|
||||
// fresh fixture holding the single point above.
|
||||
hits := func(kqlQuery string) int {
|
||||
q, err := qbleve.DefaultCreator.Create(kqlQuery)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
res, err := geoFixture(lon, lat, 1047.7).Search(bleveSearch.NewSearchRequest(q))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
return len(res.Hits)
|
||||
}
|
||||
|
||||
DescribeTable("geo.distance matches within the radius and misses outside",
|
||||
func(kqlQuery string, want int) {
|
||||
Expect(hits(kqlQuery)).To(Equal(want))
|
||||
},
|
||||
Entry("near", "location:geo.distance(49.48675890884328, 11.103870357204285, 10km)", 1),
|
||||
Entry("far (Berlin, ~400km)", "location:geo.distance(52.520008, 13.404954, 10km)", 0),
|
||||
)
|
||||
|
||||
It("geo.bbox matches a box around the point and misses a distant box", func() {
|
||||
Expect(hits("location:geo.bbox(49.0, 11.0, 50.0, 12.0)")).To(Equal(1))
|
||||
Expect(hits("location:geo.bbox(52.0, 13.0, 53.0, 14.0)")).To(Equal(0))
|
||||
})
|
||||
|
||||
It("geo.polygon matches a polygon around the point and misses a distant one", func() {
|
||||
Expect(hits("location:geo.polygon(49.0 11.0, 50.0 11.0, 50.0 12.0, 49.0 12.0)")).To(Equal(1))
|
||||
Expect(hits("location:geo.polygon(52.0 13.0, 53.0 13.0, 53.0 14.0, 52.0 14.0)")).To(Equal(0))
|
||||
})
|
||||
|
||||
It("composes with other predicates via AND", func() {
|
||||
near := "location:geo.distance(49.48675890884328, 11.103870357204285, 10km)"
|
||||
Expect(hits(near + " AND location:geo.bbox(49.0, 11.0, 50.0, 12.0)")).To(Equal(1))
|
||||
Expect(hits(near + " AND location:geo.bbox(52.0, 13.0, 53.0, 14.0)")).To(Equal(0))
|
||||
})
|
||||
})
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/blevesearch/bleve/v2"
|
||||
"github.com/blevesearch/bleve/v2/geo"
|
||||
bleveQuery "github.com/blevesearch/bleve/v2/search/query"
|
||||
|
||||
"github.com/opencloud-eu/opencloud/pkg/ast"
|
||||
@@ -67,6 +68,12 @@ func fieldQuery(field, value string) bleveQuery.Query {
|
||||
return q
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// Compiler represents a KQL query search string to the bleve query formatter.
|
||||
type Compiler struct{}
|
||||
|
||||
@@ -229,6 +236,35 @@ func walk(offset int, nodes []ast.Node) (bleveQuery.Query, int, error) {
|
||||
} else {
|
||||
next = q
|
||||
}
|
||||
case *ast.GeoDistanceNode:
|
||||
q := bleveQuery.NewGeoDistanceQuery(n.Lon, n.Lat, strconv.FormatFloat(n.Radius, 'f', -1, 64)+"m")
|
||||
q.SetField(geoField(n.Key))
|
||||
if prev == nil {
|
||||
prev = q
|
||||
} else {
|
||||
next = q
|
||||
}
|
||||
case *ast.GeoBoundingBoxNode:
|
||||
// bleve takes the top-left and bottom-right corners.
|
||||
q := bleveQuery.NewGeoBoundingBoxQuery(n.MinLon, n.MaxLat, n.MaxLon, n.MinLat)
|
||||
q.SetField(geoField(n.Key))
|
||||
if prev == nil {
|
||||
prev = q
|
||||
} else {
|
||||
next = q
|
||||
}
|
||||
case *ast.GeoPolygonNode:
|
||||
points := make([]geo.Point, 0, len(n.Points))
|
||||
for _, p := range n.Points {
|
||||
points = append(points, geo.Point{Lon: p.Lon, Lat: p.Lat})
|
||||
}
|
||||
q := bleveQuery.NewGeoBoundingPolygonQuery(points)
|
||||
q.SetField(geoField(n.Key))
|
||||
if prev == nil {
|
||||
prev = q
|
||||
} else {
|
||||
next = q
|
||||
}
|
||||
case *ast.OperatorNode:
|
||||
if n.Value == kql.BoolAND || n.Value == kql.BoolOR {
|
||||
operator = n
|
||||
|
||||
Reference in new issue
Block a user