From 9c34cea0d32e6cff319c938ae44fd026b7f7e0fa Mon Sep 17 00:00:00 2001 From: Dominik Schmidt Date: Thu, 3 Dec 2026 16:35:54 +0100 Subject: [PATCH] test(search): cover the cell filter parsing and the OpenSearch cell query --- .../search/pkg/aggregation/filter_test.go | 11 +++++ .../opensearch/internal/osu/osu_suite_test.go | 13 ++++++ .../internal/osu/query_geo_cell_test.go | 41 +++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 services/search/pkg/opensearch/internal/osu/osu_suite_test.go create mode 100644 services/search/pkg/opensearch/internal/osu/query_geo_cell_test.go diff --git a/services/search/pkg/aggregation/filter_test.go b/services/search/pkg/aggregation/filter_test.go index 605e64a523..19fa2865d0 100644 --- a/services/search/pkg/aggregation/filter_test.go +++ b/services/search/pkg/aggregation/filter_test.go @@ -35,6 +35,15 @@ var _ = Describe("ParseFilter", func() { Expect(got.Ranges.Numeric).To(HaveLen(2)) }) + It("reads the keys on a geopoint as geohash cells", func() { + got, err := parse(&searchsvc.AggregationFilter{Field: "location", Terms: []string{"u4p", "u33dc"}}) + Expect(err).ToNot(HaveOccurred()) + Expect(got.Terms).To(BeEmpty()) + Expect(got.Cells).To(HaveLen(2)) + Expect(got.Cells[0].Geohash).To(Equal("u4p")) + Expect(got.Cells[1].Geohash).To(Equal("u33dc")) + }) + DescribeTable("rejects what no aggregation on the field could have answered", func(f *searchsvc.AggregationFilter) { _, err := parse(f) @@ -51,6 +60,8 @@ var _ = Describe("ParseFilter", func() { Entry("another spelling of a bool", &searchsvc.AggregationFilter{Field: "audio.hasDrm", Terms: []string{"1"}}), Entry("a key on a date field", &searchsvc.AggregationFilter{Field: "photo.takenDateTime", Terms: []string{"2018-08-11T00:00:00Z"}}), Entry("a key on full text", &searchsvc.AggregationFilter{Field: "Content", Terms: []string{"fox"}}), + Entry("a key that is no geohash on a geopoint", &searchsvc.AggregationFilter{Field: "location", Terms: []string{"u4pru", "a"}}), + Entry("a numeric range on a geopoint", &searchsvc.AggregationFilter{Field: "location", Ranges: []*searchsvc.BucketRange{{From: "1"}}}), Entry("a numeric range on a keyword", &searchsvc.AggregationFilter{Field: "audio.artist", Ranges: []*searchsvc.BucketRange{{From: "1980"}}}), Entry("a date range on a number", &searchsvc.AggregationFilter{Field: "audio.year", Ranges: []*searchsvc.BucketRange{{From: "2018-08-11T00:00:00Z"}}}), ) diff --git a/services/search/pkg/opensearch/internal/osu/osu_suite_test.go b/services/search/pkg/opensearch/internal/osu/osu_suite_test.go new file mode 100644 index 0000000000..ceea359067 --- /dev/null +++ b/services/search/pkg/opensearch/internal/osu/osu_suite_test.go @@ -0,0 +1,13 @@ +package osu_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestOsu(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "OpenSearch Query Builder Suite") +} diff --git a/services/search/pkg/opensearch/internal/osu/query_geo_cell_test.go b/services/search/pkg/opensearch/internal/osu/query_geo_cell_test.go new file mode 100644 index 0000000000..641ddef1b7 --- /dev/null +++ b/services/search/pkg/opensearch/internal/osu/query_geo_cell_test.go @@ -0,0 +1,41 @@ +package osu_test + +import ( + "math" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "github.com/opencloud-eu/opencloud/services/search/pkg/opensearch/internal/osu" +) + +var _ = Describe("GeoCellQuery", func() { + halfLatStep := 180 / math.Exp2(33) + halfLonStep := 360 / math.Exp2(33) + + box := func(top, bottom, left, right float64) map[string]any { + return map[string]any{"geo_bounding_box": map[string]any{"location_geopoint": map[string]any{ + "top_left": map[string]any{"lat": top, "lon": left}, + "bottom_right": map[string]any{"lat": bottom, "lon": right}, + }}} + } + + It("pulls the north and east edges of a cell in by half a step", func() { + q := osu.NewGeoCellQuery("location_geopoint", 57.6123046875, 57.65625, 10.37109375, 10.4150390625) + + Expect(q.Map()).To(Equal(box(57.65625-halfLatStep, 57.6123046875, 10.37109375, 10.4150390625-halfLonStep))) + }) + + It("keeps the edges of the last cell at 90 and 180", func() { + q := osu.NewGeoCellQuery("location_geopoint", 45, 90, 135, 180) + + Expect(q.Map()).To(Equal(box(90, 45, 135, 180))) + }) + + It("marshals to the same query", func() { + q := osu.NewGeoCellQuery("location_geopoint", 45, 90, 135, 180) + + Expect(q.MarshalJSON()).To(MatchJSON(`{"geo_bounding_box": {"location_geopoint": { + "top_left": {"lat": 90, "lon": 135}, "bottom_right": {"lat": 45, "lon": 180}}}}`)) + }) +})