search_aggs_geo_distance.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2012-2015 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. // GeoDistanceAggregation is a multi-bucket aggregation that works on geo_point fields
  6. // and conceptually works very similar to the range aggregation.
  7. // The user can define a point of origin and a set of distance range buckets.
  8. // The aggregation evaluate the distance of each document value from
  9. // the origin point and determines the buckets it belongs to based on
  10. // the ranges (a document belongs to a bucket if the distance between the
  11. // document and the origin falls within the distance range of the bucket).
  12. // See: http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/search-aggregations-bucket-geodistance-aggregation.html
  13. type GeoDistanceAggregation struct {
  14. field string
  15. unit string
  16. distanceType string
  17. point string
  18. ranges []geoDistAggRange
  19. subAggregations map[string]Aggregation
  20. }
  21. type geoDistAggRange struct {
  22. Key string
  23. From interface{}
  24. To interface{}
  25. }
  26. func NewGeoDistanceAggregation() GeoDistanceAggregation {
  27. a := GeoDistanceAggregation{
  28. subAggregations: make(map[string]Aggregation),
  29. ranges: make([]geoDistAggRange, 0),
  30. }
  31. return a
  32. }
  33. func (a GeoDistanceAggregation) Field(field string) GeoDistanceAggregation {
  34. a.field = field
  35. return a
  36. }
  37. func (a GeoDistanceAggregation) Unit(unit string) GeoDistanceAggregation {
  38. a.unit = unit
  39. return a
  40. }
  41. func (a GeoDistanceAggregation) DistanceType(distanceType string) GeoDistanceAggregation {
  42. a.distanceType = distanceType
  43. return a
  44. }
  45. func (a GeoDistanceAggregation) Point(latLon string) GeoDistanceAggregation {
  46. a.point = latLon
  47. return a
  48. }
  49. func (a GeoDistanceAggregation) SubAggregation(name string, subAggregation Aggregation) GeoDistanceAggregation {
  50. a.subAggregations[name] = subAggregation
  51. return a
  52. }
  53. func (a GeoDistanceAggregation) AddRange(from, to interface{}) GeoDistanceAggregation {
  54. a.ranges = append(a.ranges, geoDistAggRange{From: from, To: to})
  55. return a
  56. }
  57. func (a GeoDistanceAggregation) AddRangeWithKey(key string, from, to interface{}) GeoDistanceAggregation {
  58. a.ranges = append(a.ranges, geoDistAggRange{Key: key, From: from, To: to})
  59. return a
  60. }
  61. func (a GeoDistanceAggregation) AddUnboundedTo(from float64) GeoDistanceAggregation {
  62. a.ranges = append(a.ranges, geoDistAggRange{From: from, To: nil})
  63. return a
  64. }
  65. func (a GeoDistanceAggregation) AddUnboundedToWithKey(key string, from float64) GeoDistanceAggregation {
  66. a.ranges = append(a.ranges, geoDistAggRange{Key: key, From: from, To: nil})
  67. return a
  68. }
  69. func (a GeoDistanceAggregation) AddUnboundedFrom(to float64) GeoDistanceAggregation {
  70. a.ranges = append(a.ranges, geoDistAggRange{From: nil, To: to})
  71. return a
  72. }
  73. func (a GeoDistanceAggregation) AddUnboundedFromWithKey(key string, to float64) GeoDistanceAggregation {
  74. a.ranges = append(a.ranges, geoDistAggRange{Key: key, From: nil, To: to})
  75. return a
  76. }
  77. func (a GeoDistanceAggregation) Between(from, to interface{}) GeoDistanceAggregation {
  78. a.ranges = append(a.ranges, geoDistAggRange{From: from, To: to})
  79. return a
  80. }
  81. func (a GeoDistanceAggregation) BetweenWithKey(key string, from, to interface{}) GeoDistanceAggregation {
  82. a.ranges = append(a.ranges, geoDistAggRange{Key: key, From: from, To: to})
  83. return a
  84. }
  85. func (a GeoDistanceAggregation) Source() interface{} {
  86. // Example:
  87. // {
  88. // "aggs" : {
  89. // "rings_around_amsterdam" : {
  90. // "geo_distance" : {
  91. // "field" : "location",
  92. // "origin" : "52.3760, 4.894",
  93. // "ranges" : [
  94. // { "to" : 100 },
  95. // { "from" : 100, "to" : 300 },
  96. // { "from" : 300 }
  97. // ]
  98. // }
  99. // }
  100. // }
  101. // }
  102. //
  103. // This method returns only the { "range" : { ... } } part.
  104. source := make(map[string]interface{})
  105. opts := make(map[string]interface{})
  106. source["geo_distance"] = opts
  107. if a.field != "" {
  108. opts["field"] = a.field
  109. }
  110. if a.unit != "" {
  111. opts["unit"] = a.unit
  112. }
  113. if a.distanceType != "" {
  114. opts["distance_type"] = a.distanceType
  115. }
  116. if a.point != "" {
  117. opts["origin"] = a.point
  118. }
  119. ranges := make([]interface{}, 0)
  120. for _, ent := range a.ranges {
  121. r := make(map[string]interface{})
  122. if ent.Key != "" {
  123. r["key"] = ent.Key
  124. }
  125. if ent.From != nil {
  126. switch from := ent.From.(type) {
  127. case int, int16, int32, int64, float32, float64:
  128. r["from"] = from
  129. case *int, *int16, *int32, *int64, *float32, *float64:
  130. r["from"] = from
  131. case string:
  132. r["from"] = from
  133. }
  134. }
  135. if ent.To != nil {
  136. switch to := ent.To.(type) {
  137. case int, int16, int32, int64, float32, float64:
  138. r["to"] = to
  139. case *int, *int16, *int32, *int64, *float32, *float64:
  140. r["to"] = to
  141. case string:
  142. r["to"] = to
  143. }
  144. }
  145. ranges = append(ranges, r)
  146. }
  147. opts["ranges"] = ranges
  148. // AggregationBuilder (SubAggregations)
  149. if len(a.subAggregations) > 0 {
  150. aggsMap := make(map[string]interface{})
  151. source["aggregations"] = aggsMap
  152. for name, aggregate := range a.subAggregations {
  153. aggsMap[name] = aggregate.Source()
  154. }
  155. }
  156. return source
  157. }