Test
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search_aggs_metrics_geo_bounds.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // GeoBoundsAggregation is a metric aggregation that computes the
  6. // bounding box containing all geo_point values for a field.
  7. // See: https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-aggregations-metrics-geobounds-aggregation.html
  8. type GeoBoundsAggregation struct {
  9. field string
  10. script *Script
  11. wrapLongitude *bool
  12. subAggregations map[string]Aggregation
  13. meta map[string]interface{}
  14. }
  15. func NewGeoBoundsAggregation() *GeoBoundsAggregation {
  16. return &GeoBoundsAggregation{
  17. subAggregations: make(map[string]Aggregation),
  18. }
  19. }
  20. func (a *GeoBoundsAggregation) Field(field string) *GeoBoundsAggregation {
  21. a.field = field
  22. return a
  23. }
  24. func (a *GeoBoundsAggregation) Script(script *Script) *GeoBoundsAggregation {
  25. a.script = script
  26. return a
  27. }
  28. func (a *GeoBoundsAggregation) WrapLongitude(wrapLongitude bool) *GeoBoundsAggregation {
  29. a.wrapLongitude = &wrapLongitude
  30. return a
  31. }
  32. func (a *GeoBoundsAggregation) SubAggregation(name string, subAggregation Aggregation) *GeoBoundsAggregation {
  33. a.subAggregations[name] = subAggregation
  34. return a
  35. }
  36. // Meta sets the meta data to be included in the aggregation response.
  37. func (a *GeoBoundsAggregation) Meta(metaData map[string]interface{}) *GeoBoundsAggregation {
  38. a.meta = metaData
  39. return a
  40. }
  41. func (a *GeoBoundsAggregation) Source() (interface{}, error) {
  42. // Example:
  43. // {
  44. // "query" : {
  45. // "match" : { "business_type" : "shop" }
  46. // },
  47. // "aggs" : {
  48. // "viewport" : {
  49. // "geo_bounds" : {
  50. // "field" : "location"
  51. // "wrap_longitude" : "true"
  52. // }
  53. // }
  54. // }
  55. // }
  56. //
  57. // This method returns only the { "geo_bounds" : { ... } } part.
  58. source := make(map[string]interface{})
  59. opts := make(map[string]interface{})
  60. source["geo_bounds"] = opts
  61. if a.field != "" {
  62. opts["field"] = a.field
  63. }
  64. if a.script != nil {
  65. src, err := a.script.Source()
  66. if err != nil {
  67. return nil, err
  68. }
  69. opts["script"] = src
  70. }
  71. if a.wrapLongitude != nil {
  72. opts["wrap_longitude"] = *a.wrapLongitude
  73. }
  74. // AggregationBuilder (SubAggregations)
  75. if len(a.subAggregations) > 0 {
  76. aggsMap := make(map[string]interface{})
  77. source["aggregations"] = aggsMap
  78. for name, aggregate := range a.subAggregations {
  79. src, err := aggregate.Source()
  80. if err != nil {
  81. return nil, err
  82. }
  83. aggsMap[name] = src
  84. }
  85. }
  86. // Add Meta data if available
  87. if len(a.meta) > 0 {
  88. source["meta"] = a.meta
  89. }
  90. return source, nil
  91. }