Debian Codegraph @

search_aggs_bucket_diversified_sampler.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 // DiversifiedSamplerAggregation Like the ‘sampler` aggregation this is a filtering aggregation used to limit any
8 // sub aggregations’ processing to a sample of the top-scoring documents. The diversified_sampler aggregation adds
9 // the ability to limit the number of matches that share a common value such as an "author".
10 //
11 // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-aggregations-bucket-diversified-sampler-aggregation.html
12 type DiversifiedSamplerAggregation struct {
13 subAggregations map[string]Aggregation
14 meta map[string]interface{}
15 field string
16 script *Script
17 shardSize int
18 maxDocsPerValue int
19 executionHint string
20 }
21
22 func NewDiversifiedSamplerAggregation() *DiversifiedSamplerAggregation {
23 return &DiversifiedSamplerAggregation{
24 shardSize: -1,
25 maxDocsPerValue: -1,
26 subAggregations: make(map[string]Aggregation),
27 }
28 }
29
30 func (a *DiversifiedSamplerAggregation) SubAggregation(name string, subAggregation Aggregation) *DiversifiedSamplerAggregation {
31 a.subAggregations[name] = subAggregation
32 return a
33 }
34
35 // Meta sets the meta data to be included in the aggregation response.
36 func (a *DiversifiedSamplerAggregation) Meta(metaData map[string]interface{}) *DiversifiedSamplerAggregation {
37 a.meta = metaData
38 return a
39 }
40
41 // Field on which the aggregation is processed.
42 func (a *DiversifiedSamplerAggregation) Field(field string) *DiversifiedSamplerAggregation {
43 a.field = field
44 return a
45 }
46
47 func (a *DiversifiedSamplerAggregation) Script(script *Script) *DiversifiedSamplerAggregation {
48 a.script = script
49 return a
50 }
51
52 // ShardSize sets the maximum number of docs returned from each shard.
53 func (a *DiversifiedSamplerAggregation) ShardSize(shardSize int) *DiversifiedSamplerAggregation {
54 a.shardSize = shardSize
55 return a
56 }
57
58 func (a *DiversifiedSamplerAggregation) MaxDocsPerValue(maxDocsPerValue int) *DiversifiedSamplerAggregation {
59 a.maxDocsPerValue = maxDocsPerValue
60 return a
61 }
62
63 func (a *DiversifiedSamplerAggregation) ExecutionHint(hint string) *DiversifiedSamplerAggregation {
64 a.executionHint = hint
65 return a
66 }
67
68 func (a *DiversifiedSamplerAggregation) Source() (interface{}, error) {
69 // Example:
70 // {
71 // "aggs": {
72 // "my_unbiased_sample": {
73 // "diversified_sampler": {
74 // "shard_size": 200,
75 // "field" : "author"
76 // }
77 // }
78 // }
79 // }
80 //
81 // This method returns only the { "diversified_sampler" : { ... } } part.
82
83 source := make(map[string]interface{})
84 opts := make(map[string]interface{})
85 source["diversified_sampler"] = opts
86
87 if a.field != "" {
88 opts["field"] = a.field
89 }
90 if a.script != nil {
91 src, err := a.script.Source()
92 if err != nil {
93 return nil, err
94 }
95 opts["script"] = src
96 }
97 if a.shardSize >= 0 {
98 opts["shard_size"] = a.shardSize
99 }
100 if a.maxDocsPerValue >= 0 {
101 opts["max_docs_per_value"] = a.maxDocsPerValue
102 }
103 if a.executionHint != "" {
104 opts["execution_hint"] = a.executionHint
105 }
106
107 // AggregationBuilder (SubAggregations)
108 if len(a.subAggregations) > 0 {
109 aggsMap := make(map[string]interface{})
110 source["aggregations"] = aggsMap
111 for name, aggregate := range a.subAggregations {
112 src, err := aggregate.Source()
113 if err != nil {
114 return nil, err
115 }
116 aggsMap[name] = src
117 }
118 }
119
120 // Add Meta data if available
121 if len(a.meta) > 0 {
122 source["meta"] = a.meta
123 }
124
125 return source, nil
126 }