From a8129fde5ae8060f695c39afc58be1469be1dbf7 Mon Sep 17 00:00:00 2001 From: aliheydarabadii Date: Fri, 12 May 2023 18:41:47 +0330 Subject: [PATCH] add support for geo_distance --- query_term_level.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/query_term_level.go b/query_term_level.go index 2604d3e..d2fb7cc 100644 --- a/query_term_level.go +++ b/query_term_level.go @@ -518,3 +518,45 @@ func (q TermsSetQuery) Map() map[string]interface{} { }, } } + +// geoFilterParams represents a query of type "geo_distance", as described in: +// https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-geo-distance-query.html +type GeoFilter struct { + params geoFilterParams + filed string +} + +func GeoFilterFunc(distance string, MiddleCentroid []float64, filed string) *GeoFilter { + return &GeoFilter{ + params: geoFilterParams{ + Distance: distance, + MiddleCentroid: MiddleCentroid, + }, + filed: filed, + } +} + +type geoFilterParams struct { + Distance string `structs:"distance,omitempty"` + MiddleCentroid []float64 `structs:"location,omitempty"` +} + +func (g *GeoFilter) Distance(distance string) *GeoFilter { + g.params.Distance = distance + return g +} +func (g *GeoFilter) MiddleCentroid(middleCentroid []float64) *GeoFilter { + g.params.MiddleCentroid = middleCentroid + return g +} + +func (g *GeoFilter) Map() map[string]interface{} { + m := structs.Map(g.params) + m[g.filed] = m["location"] + delete(m, "location") + response := map[string]interface{}{ + "geo_distance": m} + + return response + +}