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_bucket_geohash_grid.go 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package elastic
  2. type GeoHashGridAggregation struct {
  3. field string
  4. precision interface{}
  5. size int
  6. shardSize int
  7. subAggregations map[string]Aggregation
  8. meta map[string]interface{}
  9. }
  10. func NewGeoHashGridAggregation() *GeoHashGridAggregation {
  11. return &GeoHashGridAggregation{
  12. subAggregations: make(map[string]Aggregation),
  13. size: -1,
  14. shardSize: -1,
  15. }
  16. }
  17. func (a *GeoHashGridAggregation) Field(field string) *GeoHashGridAggregation {
  18. a.field = field
  19. return a
  20. }
  21. // Precision accepts the level as int value between 1 and 12 or Distance Units like "2km", "5mi" as described at
  22. // https://www.elastic.co/guide/en/elasticsearch/reference/7.0/common-options.html#distance-units and
  23. // https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-geohashgrid-aggregation.html
  24. func (a *GeoHashGridAggregation) Precision(precision interface{}) *GeoHashGridAggregation {
  25. a.precision = precision
  26. return a
  27. }
  28. func (a *GeoHashGridAggregation) Size(size int) *GeoHashGridAggregation {
  29. a.size = size
  30. return a
  31. }
  32. func (a *GeoHashGridAggregation) ShardSize(shardSize int) *GeoHashGridAggregation {
  33. a.shardSize = shardSize
  34. return a
  35. }
  36. func (a *GeoHashGridAggregation) SubAggregation(name string, subAggregation Aggregation) *GeoHashGridAggregation {
  37. a.subAggregations[name] = subAggregation
  38. return a
  39. }
  40. func (a *GeoHashGridAggregation) Meta(metaData map[string]interface{}) *GeoHashGridAggregation {
  41. a.meta = metaData
  42. return a
  43. }
  44. func (a *GeoHashGridAggregation) Source() (interface{}, error) {
  45. // Example:
  46. // {
  47. // "aggs": {
  48. // "new_york": {
  49. // "geohash_grid": {
  50. // "field": "location",
  51. // "precision": 5
  52. // }
  53. // }
  54. // }
  55. // }
  56. source := make(map[string]interface{})
  57. opts := make(map[string]interface{})
  58. source["geohash_grid"] = opts
  59. if a.field != "" {
  60. opts["field"] = a.field
  61. }
  62. if a.precision != nil {
  63. opts["precision"] = a.precision
  64. }
  65. if a.size != -1 {
  66. opts["size"] = a.size
  67. }
  68. if a.shardSize != -1 {
  69. opts["shard_size"] = a.shardSize
  70. }
  71. // AggregationBuilder (SubAggregations)
  72. if len(a.subAggregations) > 0 {
  73. aggsMap := make(map[string]interface{})
  74. source["aggregations"] = aggsMap
  75. for name, aggregate := range a.subAggregations {
  76. src, err := aggregate.Source()
  77. if err != nil {
  78. return nil, err
  79. }
  80. aggsMap[name] = src
  81. }
  82. }
  83. if len(a.meta) > 0 {
  84. source["meta"] = a.meta
  85. }
  86. return source, nil
  87. }