| 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 |
// MatrixMatrixStatsAggregation ... |
| 8 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.3/search-aggregations-metrics-stats-aggregation.html |
| 9 |
// for details. |
| 10 |
type MatrixStatsAggregation struct { |
| 11 |
fields []string |
| 12 |
missing interface{} |
| 13 |
format string |
| 14 |
valueType interface{} |
| 15 |
mode string |
| 16 |
subAggregations map[string]Aggregation |
| 17 |
meta map[string]interface{} |
| 18 |
} |
| 19 |
|
| 20 |
// NewMatrixStatsAggregation initializes a new MatrixStatsAggregation. |
| 21 |
func NewMatrixStatsAggregation() *MatrixStatsAggregation { |
| 22 |
return &MatrixStatsAggregation{ |
| 23 |
subAggregations: make(map[string]Aggregation), |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
func (a *MatrixStatsAggregation) Fields(fields ...string) *MatrixStatsAggregation { |
| 28 |
a.fields = append(a.fields, fields...) |
| 29 |
return a |
| 30 |
} |
| 31 |
|
| 32 |
// Missing configures the value to use when documents miss a value. |
| 33 |
func (a *MatrixStatsAggregation) Missing(missing interface{}) *MatrixStatsAggregation { |
| 34 |
a.missing = missing |
| 35 |
return a |
| 36 |
} |
| 37 |
|
| 38 |
// Mode specifies how to operate. Valid values are: sum, avg, median, min, or max. |
| 39 |
func (a *MatrixStatsAggregation) Mode(mode string) *MatrixStatsAggregation { |
| 40 |
a.mode = mode |
| 41 |
return a |
| 42 |
} |
| 43 |
|
| 44 |
func (a *MatrixStatsAggregation) Format(format string) *MatrixStatsAggregation { |
| 45 |
a.format = format |
| 46 |
return a |
| 47 |
} |
| 48 |
|
| 49 |
func (a *MatrixStatsAggregation) ValueType(valueType interface{}) *MatrixStatsAggregation { |
| 50 |
a.valueType = valueType |
| 51 |
return a |
| 52 |
} |
| 53 |
|
| 54 |
func (a *MatrixStatsAggregation) SubAggregation(name string, subAggregation Aggregation) *MatrixStatsAggregation { |
| 55 |
a.subAggregations[name] = subAggregation |
| 56 |
return a |
| 57 |
} |
| 58 |
|
| 59 |
// Meta sets the meta data to be included in the aggregation response. |
| 60 |
func (a *MatrixStatsAggregation) Meta(metaData map[string]interface{}) *MatrixStatsAggregation { |
| 61 |
a.meta = metaData |
| 62 |
return a |
| 63 |
} |
| 64 |
|
| 65 |
// Source returns the JSON to serialize into the request, or an error. |
| 66 |
func (a *MatrixStatsAggregation) Source() (interface{}, error) { |
| 67 |
// Example: |
| 68 |
// { |
| 69 |
// "aggs" : { |
| 70 |
// "matrixstats" : { |
| 71 |
// "matrix_stats" : { |
| 72 |
// "fields" : ["poverty", "income"], |
| 73 |
// "missing": {"income": 50000}, |
| 74 |
// "mode": "avg", |
| 75 |
// ... |
| 76 |
// } |
| 77 |
// } |
| 78 |
// } |
| 79 |
// } |
| 80 |
// This method returns only the { "matrix_stats" : { ... } } part. |
| 81 |
|
| 82 |
source := make(map[string]interface{}) |
| 83 |
opts := make(map[string]interface{}) |
| 84 |
source["matrix_stats"] = opts |
| 85 |
|
| 86 |
// MatrixStatsAggregationBuilder |
| 87 |
opts["fields"] = a.fields |
| 88 |
if a.missing != nil { |
| 89 |
opts["missing"] = a.missing |
| 90 |
} |
| 91 |
if a.format != "" { |
| 92 |
opts["format"] = a.format |
| 93 |
} |
| 94 |
if a.valueType != nil { |
| 95 |
opts["value_type"] = a.valueType |
| 96 |
} |
| 97 |
if a.mode != "" { |
| 98 |
opts["mode"] = a.mode |
| 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 |
} |