| 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 |
// MissingAggregation is a field data based single bucket aggregation, |
| 8 |
// that creates a bucket of all documents in the current document set context |
| 9 |
// that are missing a field value (effectively, missing a field or having |
| 10 |
// the configured NULL value set). This aggregator will often be used in |
| 11 |
// conjunction with other field data bucket aggregators (such as ranges) |
| 12 |
// to return information for all the documents that could not be placed |
| 13 |
// in any of the other buckets due to missing field data values. |
| 14 |
// See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-missing-aggregation.html |
| 15 |
type MissingAggregation struct { |
| 16 |
field string |
| 17 |
subAggregations map[string]Aggregation |
| 18 |
meta map[string]interface{} |
| 19 |
} |
| 20 |
|
| 21 |
func NewMissingAggregation() *MissingAggregation { |
| 22 |
return &MissingAggregation{ |
| 23 |
subAggregations: make(map[string]Aggregation), |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
func (a *MissingAggregation) Field(field string) *MissingAggregation { |
| 28 |
a.field = field |
| 29 |
return a |
| 30 |
} |
| 31 |
|
| 32 |
func (a *MissingAggregation) SubAggregation(name string, subAggregation Aggregation) *MissingAggregation { |
| 33 |
a.subAggregations[name] = subAggregation |
| 34 |
return a |
| 35 |
} |
| 36 |
|
| 37 |
// Meta sets the meta data to be included in the aggregation response. |
| 38 |
func (a *MissingAggregation) Meta(metaData map[string]interface{}) *MissingAggregation { |
| 39 |
a.meta = metaData |
| 40 |
return a |
| 41 |
} |
| 42 |
|
| 43 |
func (a *MissingAggregation) Source() (interface{}, error) { |
| 44 |
// Example: |
| 45 |
// { |
| 46 |
// "aggs" : { |
| 47 |
// "products_without_a_price" : { |
| 48 |
// "missing" : { "field" : "price" } |
| 49 |
// } |
| 50 |
// } |
| 51 |
// } |
| 52 |
// This method returns only the { "missing" : { ... } } part. |
| 53 |
|
| 54 |
source := make(map[string]interface{}) |
| 55 |
opts := make(map[string]interface{}) |
| 56 |
source["missing"] = opts |
| 57 |
|
| 58 |
if a.field != "" { |
| 59 |
opts["field"] = a.field |
| 60 |
} |
| 61 |
|
| 62 |
// AggregationBuilder (SubAggregations) |
| 63 |
if len(a.subAggregations) > 0 { |
| 64 |
aggsMap := make(map[string]interface{}) |
| 65 |
source["aggregations"] = aggsMap |
| 66 |
for name, aggregate := range a.subAggregations { |
| 67 |
src, err := aggregate.Source() |
| 68 |
if err != nil { |
| 69 |
return nil, err |
| 70 |
} |
| 71 |
aggsMap[name] = src |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
// Add Meta data if available |
| 76 |
if len(a.meta) > 0 { |
| 77 |
source["meta"] = a.meta |
| 78 |
} |
| 79 |
|
| 80 |
return source, nil |
| 81 |
} |