| 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 |
// CardinalityAggregation is a single-value metrics aggregation that |
| 8 |
// calculates an approximate count of distinct values. |
| 9 |
// Values can be extracted either from specific fields in the document |
| 10 |
// or generated by a script. |
| 11 |
// See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-metrics-cardinality-aggregation.html |
| 12 |
type CardinalityAggregation struct { |
| 13 |
field string |
| 14 |
script *Script |
| 15 |
format string |
| 16 |
subAggregations map[string]Aggregation |
| 17 |
meta map[string]interface{} |
| 18 |
precisionThreshold *int64 |
| 19 |
rehash *bool |
| 20 |
} |
| 21 |
|
| 22 |
func NewCardinalityAggregation() *CardinalityAggregation { |
| 23 |
return &CardinalityAggregation{ |
| 24 |
subAggregations: make(map[string]Aggregation), |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
func (a *CardinalityAggregation) Field(field string) *CardinalityAggregation { |
| 29 |
a.field = field |
| 30 |
return a |
| 31 |
} |
| 32 |
|
| 33 |
func (a *CardinalityAggregation) Script(script *Script) *CardinalityAggregation { |
| 34 |
a.script = script |
| 35 |
return a |
| 36 |
} |
| 37 |
|
| 38 |
func (a *CardinalityAggregation) Format(format string) *CardinalityAggregation { |
| 39 |
a.format = format |
| 40 |
return a |
| 41 |
} |
| 42 |
|
| 43 |
func (a *CardinalityAggregation) SubAggregation(name string, subAggregation Aggregation) *CardinalityAggregation { |
| 44 |
a.subAggregations[name] = subAggregation |
| 45 |
return a |
| 46 |
} |
| 47 |
|
| 48 |
// Meta sets the meta data to be included in the aggregation response. |
| 49 |
func (a *CardinalityAggregation) Meta(metaData map[string]interface{}) *CardinalityAggregation { |
| 50 |
a.meta = metaData |
| 51 |
return a |
| 52 |
} |
| 53 |
|
| 54 |
func (a *CardinalityAggregation) PrecisionThreshold(threshold int64) *CardinalityAggregation { |
| 55 |
a.precisionThreshold = &threshold |
| 56 |
return a |
| 57 |
} |
| 58 |
|
| 59 |
func (a *CardinalityAggregation) Rehash(rehash bool) *CardinalityAggregation { |
| 60 |
a.rehash = &rehash |
| 61 |
return a |
| 62 |
} |
| 63 |
|
| 64 |
func (a *CardinalityAggregation) Source() (interface{}, error) { |
| 65 |
// Example: |
| 66 |
// { |
| 67 |
// "aggs" : { |
| 68 |
// "author_count" : { |
| 69 |
// "cardinality" : { "field" : "author" } |
| 70 |
// } |
| 71 |
// } |
| 72 |
// } |
| 73 |
// This method returns only the "cardinality" : { "field" : "author" } part. |
| 74 |
|
| 75 |
source := make(map[string]interface{}) |
| 76 |
opts := make(map[string]interface{}) |
| 77 |
source["cardinality"] = opts |
| 78 |
|
| 79 |
// ValuesSourceAggregationBuilder |
| 80 |
if a.field != "" { |
| 81 |
opts["field"] = a.field |
| 82 |
} |
| 83 |
if a.script != nil { |
| 84 |
src, err := a.script.Source() |
| 85 |
if err != nil { |
| 86 |
return nil, err |
| 87 |
} |
| 88 |
opts["script"] = src |
| 89 |
} |
| 90 |
|
| 91 |
if a.format != "" { |
| 92 |
opts["format"] = a.format |
| 93 |
} |
| 94 |
if a.precisionThreshold != nil { |
| 95 |
opts["precision_threshold"] = *a.precisionThreshold |
| 96 |
} |
| 97 |
if a.rehash != nil { |
| 98 |
opts["rehash"] = *a.rehash |
| 99 |
} |
| 100 |
|
| 101 |
// AggregationBuilder (SubAggregations) |
| 102 |
if len(a.subAggregations) > 0 { |
| 103 |
aggsMap := make(map[string]interface{}) |
| 104 |
source["aggregations"] = aggsMap |
| 105 |
for name, aggregate := range a.subAggregations { |
| 106 |
src, err := aggregate.Source() |
| 107 |
if err != nil { |
| 108 |
return nil, err |
| 109 |
} |
| 110 |
aggsMap[name] = src |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
// Add Meta data if available |
| 115 |
if len(a.meta) > 0 { |
| 116 |
source["meta"] = a.meta |
| 117 |
} |
| 118 |
|
| 119 |
return source, nil |
| 120 |
} |