Debian Codegraph @

search_aggs_bucket_children.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 // ChildrenAggregation is a special single bucket aggregation that enables
8 // aggregating from buckets on parent document types to buckets on child documents.
9 // It is available from 1.4.0.Beta1 upwards.
10 // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-children-aggregation.html
11 type ChildrenAggregation struct {
12 typ string
13 subAggregations map[string]Aggregation
14 meta map[string]interface{}
15 }
16
17 func NewChildrenAggregation() *ChildrenAggregation {
18 return &ChildrenAggregation{
19 subAggregations: make(map[string]Aggregation),
20 }
21 }
22
23 func (a *ChildrenAggregation) Type(typ string) *ChildrenAggregation {
24 a.typ = typ
25 return a
26 }
27
28 func (a *ChildrenAggregation) SubAggregation(name string, subAggregation Aggregation) *ChildrenAggregation {
29 a.subAggregations[name] = subAggregation
30 return a
31 }
32
33 // Meta sets the meta data to be included in the aggregation response.
34 func (a *ChildrenAggregation) Meta(metaData map[string]interface{}) *ChildrenAggregation {
35 a.meta = metaData
36 return a
37 }
38
39 func (a *ChildrenAggregation) Source() (interface{}, error) {
40 // Example:
41 // {
42 // "aggs" : {
43 // "to-answers" : {
44 // "children": {
45 // "type" : "answer"
46 // }
47 // }
48 // }
49 // }
50 // This method returns only the { "type" : ... } part.
51
52 source := make(map[string]interface{})
53 opts := make(map[string]interface{})
54 source["children"] = opts
55 opts["type"] = a.typ
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
70 // Add Meta data if available
71 if len(a.meta) > 0 {
72 source["meta"] = a.meta
73 }
74
75 return source, nil
76 }