search_aggs_bucket_reverse_nested.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2012-present 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. // ReverseNestedAggregation defines a special single bucket aggregation
  6. // that enables aggregating on parent docs from nested documents.
  7. // Effectively this aggregation can break out of the nested block
  8. // structure and link to other nested structures or the root document,
  9. // which allows nesting other aggregations that aren’t part of
  10. // the nested object in a nested aggregation.
  11. //
  12. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-reverse-nested-aggregation.html
  13. type ReverseNestedAggregation struct {
  14. path string
  15. subAggregations map[string]Aggregation
  16. meta map[string]interface{}
  17. }
  18. // NewReverseNestedAggregation initializes a new ReverseNestedAggregation
  19. // bucket aggregation.
  20. func NewReverseNestedAggregation() *ReverseNestedAggregation {
  21. return &ReverseNestedAggregation{
  22. subAggregations: make(map[string]Aggregation),
  23. }
  24. }
  25. // Path set the path to use for this nested aggregation. The path must match
  26. // the path to a nested object in the mappings. If it is not specified
  27. // then this aggregation will go back to the root document.
  28. func (a *ReverseNestedAggregation) Path(path string) *ReverseNestedAggregation {
  29. a.path = path
  30. return a
  31. }
  32. func (a *ReverseNestedAggregation) SubAggregation(name string, subAggregation Aggregation) *ReverseNestedAggregation {
  33. a.subAggregations[name] = subAggregation
  34. return a
  35. }
  36. // Meta sets the meta data to be included in the aggregation response.
  37. func (a *ReverseNestedAggregation) Meta(metaData map[string]interface{}) *ReverseNestedAggregation {
  38. a.meta = metaData
  39. return a
  40. }
  41. func (a *ReverseNestedAggregation) Source() (interface{}, error) {
  42. // Example:
  43. // {
  44. // "aggs" : {
  45. // "reverse_nested" : {
  46. // "path": "..."
  47. // }
  48. // }
  49. // }
  50. // This method returns only the { "reverse_nested" : {} } part.
  51. source := make(map[string]interface{})
  52. opts := make(map[string]interface{})
  53. source["reverse_nested"] = opts
  54. if a.path != "" {
  55. opts["path"] = a.path
  56. }
  57. // AggregationBuilder (SubAggregations)
  58. if len(a.subAggregations) > 0 {
  59. aggsMap := make(map[string]interface{})
  60. source["aggregations"] = aggsMap
  61. for name, aggregate := range a.subAggregations {
  62. src, err := aggregate.Source()
  63. if err != nil {
  64. return nil, err
  65. }
  66. aggsMap[name] = src
  67. }
  68. }
  69. // Add Meta data if available
  70. if len(a.meta) > 0 {
  71. source["meta"] = a.meta
  72. }
  73. return source, nil
  74. }