search_aggs_geo_bounds.go 2.5 KB

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