Debian Codegraph @

search_aggs_bucket_reverse_nested.go

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