| 1 |
// Copyright 2012-present Oliver Eilhard. All rights reserved. |
| 2 |
// Use of this source code is governed by a MIT-license. |
| 3 |
// See http://olivere.mit-license.org/license.txt for details. |
| 4 |
|
| 5 |
package elastic |
| 6 |
|
| 7 |
// GeoDistanceQuery filters documents that include only hits that exists |
| 8 |
// within a specific distance from a geo point. |
| 9 |
// |
| 10 |
// For more details, see: |
| 11 |
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-geo-distance-query.html |
| 12 |
type GeoDistanceQuery struct { |
| 13 |
name string |
| 14 |
distance string |
| 15 |
lat float64 |
| 16 |
lon float64 |
| 17 |
geohash string |
| 18 |
distanceType string |
| 19 |
optimizeBbox string |
| 20 |
queryName string |
| 21 |
} |
| 22 |
|
| 23 |
// NewGeoDistanceQuery creates and initializes a new GeoDistanceQuery. |
| 24 |
func NewGeoDistanceQuery(name string) *GeoDistanceQuery { |
| 25 |
return &GeoDistanceQuery{name: name} |
| 26 |
} |
| 27 |
|
| 28 |
func (q *GeoDistanceQuery) GeoPoint(point *GeoPoint) *GeoDistanceQuery { |
| 29 |
q.lat = point.Lat |
| 30 |
q.lon = point.Lon |
| 31 |
return q |
| 32 |
} |
| 33 |
|
| 34 |
func (q *GeoDistanceQuery) Point(lat, lon float64) *GeoDistanceQuery { |
| 35 |
q.lat = lat |
| 36 |
q.lon = lon |
| 37 |
return q |
| 38 |
} |
| 39 |
|
| 40 |
func (q *GeoDistanceQuery) Lat(lat float64) *GeoDistanceQuery { |
| 41 |
q.lat = lat |
| 42 |
return q |
| 43 |
} |
| 44 |
|
| 45 |
func (q *GeoDistanceQuery) Lon(lon float64) *GeoDistanceQuery { |
| 46 |
q.lon = lon |
| 47 |
return q |
| 48 |
} |
| 49 |
|
| 50 |
func (q *GeoDistanceQuery) GeoHash(geohash string) *GeoDistanceQuery { |
| 51 |
q.geohash = geohash |
| 52 |
return q |
| 53 |
} |
| 54 |
|
| 55 |
func (q *GeoDistanceQuery) Distance(distance string) *GeoDistanceQuery { |
| 56 |
q.distance = distance |
| 57 |
return q |
| 58 |
} |
| 59 |
|
| 60 |
func (q *GeoDistanceQuery) DistanceType(distanceType string) *GeoDistanceQuery { |
| 61 |
q.distanceType = distanceType |
| 62 |
return q |
| 63 |
} |
| 64 |
|
| 65 |
func (q *GeoDistanceQuery) OptimizeBbox(optimizeBbox string) *GeoDistanceQuery { |
| 66 |
q.optimizeBbox = optimizeBbox |
| 67 |
return q |
| 68 |
} |
| 69 |
|
| 70 |
func (q *GeoDistanceQuery) QueryName(queryName string) *GeoDistanceQuery { |
| 71 |
q.queryName = queryName |
| 72 |
return q |
| 73 |
} |
| 74 |
|
| 75 |
// Source returns JSON for the function score query. |
| 76 |
func (q *GeoDistanceQuery) Source() (interface{}, error) { |
| 77 |
// { |
| 78 |
// "geo_distance" : { |
| 79 |
// "distance" : "200km", |
| 80 |
// "pin.location" : { |
| 81 |
// "lat" : 40, |
| 82 |
// "lon" : -70 |
| 83 |
// } |
| 84 |
// } |
| 85 |
// } |
| 86 |
|
| 87 |
source := make(map[string]interface{}) |
| 88 |
|
| 89 |
params := make(map[string]interface{}) |
| 90 |
|
| 91 |
if q.geohash != "" { |
| 92 |
params[q.name] = q.geohash |
| 93 |
} else { |
| 94 |
location := make(map[string]interface{}) |
| 95 |
location["lat"] = q.lat |
| 96 |
location["lon"] = q.lon |
| 97 |
params[q.name] = location |
| 98 |
} |
| 99 |
|
| 100 |
if q.distance != "" { |
| 101 |
params["distance"] = q.distance |
| 102 |
} |
| 103 |
if q.distanceType != "" { |
| 104 |
params["distance_type"] = q.distanceType |
| 105 |
} |
| 106 |
if q.optimizeBbox != "" { |
| 107 |
params["optimize_bbox"] = q.optimizeBbox |
| 108 |
} |
| 109 |
if q.queryName != "" { |
| 110 |
params["_name"] = q.queryName |
| 111 |
} |
| 112 |
|
| 113 |
source["geo_distance"] = params |
| 114 |
|
| 115 |
return source, nil |
| 116 |
} |