Debian Codegraph @

search_aggs_bucket_global.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 // GlobalAggregation defines a single bucket of all the documents within
8 // the search execution context. This context is defined by the indices
9 // and the document types you’re searching on, but is not influenced
10 // by the search query itself.
11 // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-global-aggregation.html
12 type GlobalAggregation struct {
13 subAggregations map[string]Aggregation
14 meta map[string]interface{}
15 }
16
17 func NewGlobalAggregation() *GlobalAggregation {
18 return &GlobalAggregation{
19 subAggregations: make(map[string]Aggregation),
20 }
21 }
22
23 func (a *GlobalAggregation) SubAggregation(name string, subAggregation Aggregation) *GlobalAggregation {
24 a.subAggregations[name] = subAggregation
25 return a
26 }
27
28 // Meta sets the meta data to be included in the aggregation response.
29 func (a *GlobalAggregation) Meta(metaData map[string]interface{}) *GlobalAggregation {
30 a.meta = metaData
31 return a
32 }
33
34 func (a *GlobalAggregation) Source() (interface{}, error) {
35 // Example:
36 // {
37 // "aggs" : {
38 // "all_products" : {
39 // "global" : {},
40 // "aggs" : {
41 // "avg_price" : { "avg" : { "field" : "price" } }
42 // }
43 // }
44 // }
45 // }
46 // This method returns only the { "global" : {} } part.
47
48 source := make(map[string]interface{})
49 opts := make(map[string]interface{})
50 source["global"] = opts
51
52 // AggregationBuilder (SubAggregations)
53 if len(a.subAggregations) > 0 {
54 aggsMap := make(map[string]interface{})
55 source["aggregations"] = aggsMap
56 for name, aggregate := range a.subAggregations {
57 src, err := aggregate.Source()
58 if err != nil {
59 return nil, err
60 }
61 aggsMap[name] = src
62 }
63 }
64
65 // Add Meta data if available
66 if len(a.meta) > 0 {
67 source["meta"] = a.meta
68 }
69
70 return source, nil
71 }