Debian Codegraph @

search_aggs_metrics_value_count.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 // ValueCountAggregation is a single-value metrics aggregation that counts
8 // the number of values that are extracted from the aggregated documents.
9 // These values can be extracted either from specific fields in the documents,
10 // or be generated by a provided script. Typically, this aggregator will be
11 // used in conjunction with other single-value aggregations.
12 // For example, when computing the avg one might be interested in the
13 // number of values the average is computed over.
14 // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-metrics-valuecount-aggregation.html
15 type ValueCountAggregation struct {
16 field string
17 script *Script
18 format string
19 subAggregations map[string]Aggregation
20 meta map[string]interface{}
21 }
22
23 func NewValueCountAggregation() *ValueCountAggregation {
24 return &ValueCountAggregation{
25 subAggregations: make(map[string]Aggregation),
26 }
27 }
28
29 func (a *ValueCountAggregation) Field(field string) *ValueCountAggregation {
30 a.field = field
31 return a
32 }
33
34 func (a *ValueCountAggregation) Script(script *Script) *ValueCountAggregation {
35 a.script = script
36 return a
37 }
38
39 func (a *ValueCountAggregation) Format(format string) *ValueCountAggregation {
40 a.format = format
41 return a
42 }
43
44 func (a *ValueCountAggregation) SubAggregation(name string, subAggregation Aggregation) *ValueCountAggregation {
45 a.subAggregations[name] = subAggregation
46 return a
47 }
48
49 // Meta sets the meta data to be included in the aggregation response.
50 func (a *ValueCountAggregation) Meta(metaData map[string]interface{}) *ValueCountAggregation {
51 a.meta = metaData
52 return a
53 }
54
55 func (a *ValueCountAggregation) Source() (interface{}, error) {
56 // Example:
57 // {
58 // "aggs" : {
59 // "grades_count" : { "value_count" : { "field" : "grade" } }
60 // }
61 // }
62 // This method returns only the { "value_count" : { "field" : "grade" } } part.
63
64 source := make(map[string]interface{})
65 opts := make(map[string]interface{})
66 source["value_count"] = opts
67
68 // ValuesSourceAggregationBuilder
69 if a.field != "" {
70 opts["field"] = a.field
71 }
72 if a.script != nil {
73 src, err := a.script.Source()
74 if err != nil {
75 return nil, err
76 }
77 opts["script"] = src
78 }
79 if a.format != "" {
80 opts["format"] = a.format
81 }
82
83 // AggregationBuilder (SubAggregations)
84 if len(a.subAggregations) > 0 {
85 aggsMap := make(map[string]interface{})
86 source["aggregations"] = aggsMap
87 for name, aggregate := range a.subAggregations {
88 src, err := aggregate.Source()
89 if err != nil {
90 return nil, err
91 }
92 aggsMap[name] = src
93 }
94 }
95
96 // Add Meta data if available
97 if len(a.meta) > 0 {
98 source["meta"] = a.meta
99 }
100
101 return source, nil
102 }