search_aggs_bucket_geohash_grid.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package elastic
  2. type GeoHashGridAggregation struct {
  3. field string
  4. precision int
  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. precision: -1,
  14. size: -1,
  15. shardSize: -1,
  16. }
  17. }
  18. func (a *GeoHashGridAggregation) Field(field string) *GeoHashGridAggregation {
  19. a.field = field
  20. return a
  21. }
  22. func (a *GeoHashGridAggregation) Precision(precision int) *GeoHashGridAggregation {
  23. a.precision = precision
  24. return a
  25. }
  26. func (a *GeoHashGridAggregation) Size(size int) *GeoHashGridAggregation {
  27. a.size = size
  28. return a
  29. }
  30. func (a *GeoHashGridAggregation) ShardSize(shardSize int) *GeoHashGridAggregation {
  31. a.shardSize = shardSize
  32. return a
  33. }
  34. func (a *GeoHashGridAggregation) SubAggregation(name string, subAggregation Aggregation) *GeoHashGridAggregation {
  35. a.subAggregations[name] = subAggregation
  36. return a
  37. }
  38. func (a *GeoHashGridAggregation) Meta(metaData map[string]interface{}) *GeoHashGridAggregation {
  39. a.meta = metaData
  40. return a
  41. }
  42. func (a *GeoHashGridAggregation) Source() (interface{}, error) {
  43. // Example:
  44. // {
  45. // "aggs": {
  46. // "new_york": {
  47. // "geohash_grid": {
  48. // "field": "location",
  49. // "precision": 5
  50. // }
  51. // }
  52. // }
  53. // }
  54. source := make(map[string]interface{})
  55. opts := make(map[string]interface{})
  56. source["geohash_grid"] = opts
  57. if a.field != "" {
  58. opts["field"] = a.field
  59. }
  60. if a.precision != -1 {
  61. opts["precision"] = a.precision
  62. }
  63. if a.size != -1 {
  64. opts["size"] = a.size
  65. }
  66. if a.shardSize != -1 {
  67. opts["shard_size"] = a.shardSize
  68. }
  69. // AggregationBuilder (SubAggregations)
  70. if len(a.subAggregations) > 0 {
  71. aggsMap := make(map[string]interface{})
  72. source["aggregations"] = aggsMap
  73. for name, aggregate := range a.subAggregations {
  74. src, err := aggregate.Source()
  75. if err != nil {
  76. return nil, err
  77. }
  78. aggsMap[name] = src
  79. }
  80. }
  81. if len(a.meta) > 0 {
  82. source["meta"] = a.meta
  83. }
  84. return source, nil
  85. }