search_aggs_children.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. // ChildrenAggregation is a special single bucket aggregation that enables
  6. // aggregating from buckets on parent document types to buckets on child documents.
  7. // It is available from 1.4.0.Beta1 upwards.
  8. // See: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-children-aggregation.html
  9. type ChildrenAggregation struct {
  10. typ string
  11. subAggregations map[string]Aggregation
  12. }
  13. func NewChildrenAggregation() ChildrenAggregation {
  14. a := ChildrenAggregation{
  15. subAggregations: make(map[string]Aggregation),
  16. }
  17. return a
  18. }
  19. func (a ChildrenAggregation) Type(typ string) ChildrenAggregation {
  20. a.typ = typ
  21. return a
  22. }
  23. func (a ChildrenAggregation) SubAggregation(name string, subAggregation Aggregation) ChildrenAggregation {
  24. a.subAggregations[name] = subAggregation
  25. return a
  26. }
  27. func (a ChildrenAggregation) Source() interface{} {
  28. // Example:
  29. // {
  30. // "aggs" : {
  31. // "to-answers" : {
  32. // "type" : "answer"
  33. // }
  34. // }
  35. // }
  36. // This method returns only the { "type" : ... } part.
  37. source := make(map[string]interface{})
  38. source["type"] = a.typ
  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. }