search_queries_geo_distance.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. package elastic
  5. // GeoDistanceQuery filters documents that include only hits that exists
  6. // within a specific distance from a geo point.
  7. //
  8. // For more details, see:
  9. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-geo-distance-query.html
  10. type GeoDistanceQuery struct {
  11. name string
  12. distance string
  13. lat float64
  14. lon float64
  15. geohash string
  16. distanceType string
  17. optimizeBbox string
  18. queryName string
  19. }
  20. // NewGeoDistanceQuery creates and initializes a new GeoDistanceQuery.
  21. func NewGeoDistanceQuery(name string) *GeoDistanceQuery {
  22. return &GeoDistanceQuery{name: name}
  23. }
  24. func (q *GeoDistanceQuery) GeoPoint(point *GeoPoint) *GeoDistanceQuery {
  25. q.lat = point.Lat
  26. q.lon = point.Lon
  27. return q
  28. }
  29. func (q *GeoDistanceQuery) Point(lat, lon float64) *GeoDistanceQuery {
  30. q.lat = lat
  31. q.lon = lon
  32. return q
  33. }
  34. func (q *GeoDistanceQuery) Lat(lat float64) *GeoDistanceQuery {
  35. q.lat = lat
  36. return q
  37. }
  38. func (q *GeoDistanceQuery) Lon(lon float64) *GeoDistanceQuery {
  39. q.lon = lon
  40. return q
  41. }
  42. func (q *GeoDistanceQuery) GeoHash(geohash string) *GeoDistanceQuery {
  43. q.geohash = geohash
  44. return q
  45. }
  46. func (q *GeoDistanceQuery) Distance(distance string) *GeoDistanceQuery {
  47. q.distance = distance
  48. return q
  49. }
  50. func (q *GeoDistanceQuery) DistanceType(distanceType string) *GeoDistanceQuery {
  51. q.distanceType = distanceType
  52. return q
  53. }
  54. func (q *GeoDistanceQuery) OptimizeBbox(optimizeBbox string) *GeoDistanceQuery {
  55. q.optimizeBbox = optimizeBbox
  56. return q
  57. }
  58. func (q *GeoDistanceQuery) QueryName(queryName string) *GeoDistanceQuery {
  59. q.queryName = queryName
  60. return q
  61. }
  62. // Source returns JSON for the function score query.
  63. func (q *GeoDistanceQuery) Source() (interface{}, error) {
  64. // {
  65. // "geo_distance" : {
  66. // "distance" : "200km",
  67. // "pin.location" : {
  68. // "lat" : 40,
  69. // "lon" : -70
  70. // }
  71. // }
  72. // }
  73. source := make(map[string]interface{})
  74. params := make(map[string]interface{})
  75. if q.geohash != "" {
  76. params[q.name] = q.geohash
  77. } else {
  78. location := make(map[string]interface{})
  79. location["lat"] = q.lat
  80. location["lon"] = q.lon
  81. params[q.name] = location
  82. }
  83. if q.distance != "" {
  84. params["distance"] = q.distance
  85. }
  86. if q.distanceType != "" {
  87. params["distance_type"] = q.distanceType
  88. }
  89. if q.optimizeBbox != "" {
  90. params["optimize_bbox"] = q.optimizeBbox
  91. }
  92. if q.queryName != "" {
  93. params["_name"] = q.queryName
  94. }
  95. source["geo_distance"] = params
  96. return source, nil
  97. }