|
| 1 | +// Copyright 2012-2016 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 | +// SamplerAggregation is a filtering aggregation used to limit any |
| 8 | +// sub aggregations' processing to a sample of the top-scoring documents. |
| 9 | +// Optionally, diversity settings can be used to limit the number of matches |
| 10 | +// that share a common value such as an "author". |
| 11 | +// See: https://www.elastic.co/guide/en/elasticsearch/reference/2.x/search-aggregations-bucket-sampler-aggregation.html |
| 12 | +type SamplerAggregation struct { |
| 13 | + field string |
| 14 | + script *Script |
| 15 | + missing interface{} |
| 16 | + subAggregations map[string]Aggregation |
| 17 | + meta map[string]interface{} |
| 18 | + |
| 19 | + shardSize int |
| 20 | + maxDocsPerValue int |
| 21 | + executionHint string |
| 22 | +} |
| 23 | + |
| 24 | +func NewSamplerAggregation() *SamplerAggregation { |
| 25 | + return &SamplerAggregation{ |
| 26 | + shardSize: -1, |
| 27 | + maxDocsPerValue: -1, |
| 28 | + subAggregations: make(map[string]Aggregation), |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func (a *SamplerAggregation) Field(field string) *SamplerAggregation { |
| 33 | + a.field = field |
| 34 | + return a |
| 35 | +} |
| 36 | + |
| 37 | +func (a *SamplerAggregation) Script(script *Script) *SamplerAggregation { |
| 38 | + a.script = script |
| 39 | + return a |
| 40 | +} |
| 41 | + |
| 42 | +// Missing configures the value to use when documents miss a value. |
| 43 | +func (a *SamplerAggregation) Missing(missing interface{}) *SamplerAggregation { |
| 44 | + a.missing = missing |
| 45 | + return a |
| 46 | +} |
| 47 | + |
| 48 | +func (a *SamplerAggregation) SubAggregation(name string, subAggregation Aggregation) *SamplerAggregation { |
| 49 | + a.subAggregations[name] = subAggregation |
| 50 | + return a |
| 51 | +} |
| 52 | + |
| 53 | +// Meta sets the meta data to be included in the aggregation response. |
| 54 | +func (a *SamplerAggregation) Meta(metaData map[string]interface{}) *SamplerAggregation { |
| 55 | + a.meta = metaData |
| 56 | + return a |
| 57 | +} |
| 58 | + |
| 59 | +// ShardSize sets the maximum number of docs returned from each shard. |
| 60 | +func (a *SamplerAggregation) ShardSize(shardSize int) *SamplerAggregation { |
| 61 | + a.shardSize = shardSize |
| 62 | + return a |
| 63 | +} |
| 64 | + |
| 65 | +func (a *SamplerAggregation) MaxDocsPerValue(maxDocsPerValue int) *SamplerAggregation { |
| 66 | + a.maxDocsPerValue = maxDocsPerValue |
| 67 | + return a |
| 68 | +} |
| 69 | + |
| 70 | +func (a *SamplerAggregation) ExecutionHint(hint string) *SamplerAggregation { |
| 71 | + a.executionHint = hint |
| 72 | + return a |
| 73 | +} |
| 74 | + |
| 75 | +func (a *SamplerAggregation) Source() (interface{}, error) { |
| 76 | + // Example: |
| 77 | + // { |
| 78 | + // "aggs" : { |
| 79 | + // "sample" : { |
| 80 | + // "sampler" : { |
| 81 | + // "field" : "user.id", |
| 82 | + // "shard_size" : 200 |
| 83 | + // }, |
| 84 | + // "aggs": { |
| 85 | + // "keywords": { |
| 86 | + // "significant_terms": { |
| 87 | + // "field": "text" |
| 88 | + // } |
| 89 | + // } |
| 90 | + // } |
| 91 | + // } |
| 92 | + // } |
| 93 | + // } |
| 94 | + // |
| 95 | + // This method returns only the { "sampler" : { ... } } part. |
| 96 | + |
| 97 | + source := make(map[string]interface{}) |
| 98 | + opts := make(map[string]interface{}) |
| 99 | + source["sampler"] = opts |
| 100 | + |
| 101 | + // ValuesSourceAggregationBuilder |
| 102 | + if a.field != "" { |
| 103 | + opts["field"] = a.field |
| 104 | + } |
| 105 | + if a.script != nil { |
| 106 | + src, err := a.script.Source() |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + opts["script"] = src |
| 111 | + } |
| 112 | + if a.missing != nil { |
| 113 | + opts["missing"] = a.missing |
| 114 | + } |
| 115 | + |
| 116 | + if a.shardSize >= 0 { |
| 117 | + opts["shard_size"] = a.shardSize |
| 118 | + } |
| 119 | + if a.maxDocsPerValue >= 0 { |
| 120 | + opts["max_docs_per_value"] = a.maxDocsPerValue |
| 121 | + } |
| 122 | + if a.executionHint != "" { |
| 123 | + opts["execution_hint"] = a.executionHint |
| 124 | + } |
| 125 | + |
| 126 | + // AggregationBuilder (SubAggregations) |
| 127 | + if len(a.subAggregations) > 0 { |
| 128 | + aggsMap := make(map[string]interface{}) |
| 129 | + source["aggregations"] = aggsMap |
| 130 | + for name, aggregate := range a.subAggregations { |
| 131 | + src, err := aggregate.Source() |
| 132 | + if err != nil { |
| 133 | + return nil, err |
| 134 | + } |
| 135 | + aggsMap[name] = src |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + // Add Meta data if available |
| 140 | + if len(a.meta) > 0 { |
| 141 | + source["meta"] = a.meta |
| 142 | + } |
| 143 | + |
| 144 | + return source, nil |
| 145 | +} |
0 commit comments