search_aggs_global.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. // GlobalAggregation defines a single bucket of all the documents within
  6. // the search execution context. This context is defined by the indices
  7. // and the document types you’re searching on, but is not influenced
  8. // by the search query itself.
  9. // See: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-global-aggregation.html
  10. type GlobalAggregation struct {
  11. subAggregations map[string]Aggregation
  12. }
  13. func NewGlobalAggregation() GlobalAggregation {
  14. a := GlobalAggregation{
  15. subAggregations: make(map[string]Aggregation),
  16. }
  17. return a
  18. }
  19. func (a GlobalAggregation) SubAggregation(name string, subAggregation Aggregation) GlobalAggregation {
  20. a.subAggregations[name] = subAggregation
  21. return a
  22. }
  23. func (a GlobalAggregation) Source() interface{} {
  24. // Example:
  25. // {
  26. // "aggs" : {
  27. // "all_products" : {
  28. // "global" : {},
  29. // "aggs" : {
  30. // "avg_price" : { "avg" : { "field" : "price" } }
  31. // }
  32. // }
  33. // }
  34. // }
  35. // This method returns only the { "global" : {} } part.
  36. source := make(map[string]interface{})
  37. opts := make(map[string]interface{})
  38. source["global"] = opts
  39. // AggregationBuilder (SubAggregations)
  40. if len(a.subAggregations) > 0 {
  41. aggsMap := make(map[string]interface{})
  42. source["aggregations"] = aggsMap
  43. for name, aggregate := range a.subAggregations {
  44. aggsMap[name] = aggregate.Source()
  45. }
  46. }
  47. return source
  48. }