forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_aggs_bucket_significant_text.go
245 lines (218 loc) · 6.97 KB
/
search_aggs_bucket_significant_text.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
// SignificantTextAggregation returns interesting or unusual occurrences
// of free-text terms in a set.
// See: https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-aggregations-bucket-significanttext-aggregation.html
type SignificantTextAggregation struct {
field string
subAggregations map[string]Aggregation
meta map[string]interface{}
sourceFieldNames []string
filterDuplicateText *bool
includeExclude *TermsAggregationIncludeExclude
filter Query
bucketCountThresholds *BucketCountThresholds
significanceHeuristic SignificanceHeuristic
}
func NewSignificantTextAggregation() *SignificantTextAggregation {
return &SignificantTextAggregation{
subAggregations: make(map[string]Aggregation, 0),
}
}
func (a *SignificantTextAggregation) Field(field string) *SignificantTextAggregation {
a.field = field
return a
}
func (a *SignificantTextAggregation) SubAggregation(name string, subAggregation Aggregation) *SignificantTextAggregation {
a.subAggregations[name] = subAggregation
return a
}
// Meta sets the meta data to be included in the aggregation response.
func (a *SignificantTextAggregation) Meta(metaData map[string]interface{}) *SignificantTextAggregation {
a.meta = metaData
return a
}
func (a *SignificantTextAggregation) SourceFieldNames(names ...string) *SignificantTextAggregation {
a.sourceFieldNames = names
return a
}
func (a *SignificantTextAggregation) FilterDuplicateText(filter bool) *SignificantTextAggregation {
a.filterDuplicateText = &filter
return a
}
func (a *SignificantTextAggregation) MinDocCount(minDocCount int64) *SignificantTextAggregation {
if a.bucketCountThresholds == nil {
a.bucketCountThresholds = &BucketCountThresholds{}
}
a.bucketCountThresholds.MinDocCount = &minDocCount
return a
}
func (a *SignificantTextAggregation) ShardMinDocCount(shardMinDocCount int64) *SignificantTextAggregation {
if a.bucketCountThresholds == nil {
a.bucketCountThresholds = &BucketCountThresholds{}
}
a.bucketCountThresholds.ShardMinDocCount = &shardMinDocCount
return a
}
func (a *SignificantTextAggregation) Size(size int) *SignificantTextAggregation {
if a.bucketCountThresholds == nil {
a.bucketCountThresholds = &BucketCountThresholds{}
}
a.bucketCountThresholds.RequiredSize = &size
return a
}
func (a *SignificantTextAggregation) ShardSize(shardSize int) *SignificantTextAggregation {
if a.bucketCountThresholds == nil {
a.bucketCountThresholds = &BucketCountThresholds{}
}
a.bucketCountThresholds.ShardSize = &shardSize
return a
}
func (a *SignificantTextAggregation) BackgroundFilter(filter Query) *SignificantTextAggregation {
a.filter = filter
return a
}
func (a *SignificantTextAggregation) SignificanceHeuristic(heuristic SignificanceHeuristic) *SignificantTextAggregation {
a.significanceHeuristic = heuristic
return a
}
func (a *SignificantTextAggregation) Include(regexp string) *SignificantTextAggregation {
if a.includeExclude == nil {
a.includeExclude = &TermsAggregationIncludeExclude{}
}
a.includeExclude.Include = regexp
return a
}
func (a *SignificantTextAggregation) IncludeValues(values ...interface{}) *SignificantTextAggregation {
if a.includeExclude == nil {
a.includeExclude = &TermsAggregationIncludeExclude{}
}
a.includeExclude.IncludeValues = append(a.includeExclude.IncludeValues, values...)
return a
}
func (a *SignificantTextAggregation) Exclude(regexp string) *SignificantTextAggregation {
if a.includeExclude == nil {
a.includeExclude = &TermsAggregationIncludeExclude{}
}
a.includeExclude.Exclude = regexp
return a
}
func (a *SignificantTextAggregation) ExcludeValues(values ...interface{}) *SignificantTextAggregation {
if a.includeExclude == nil {
a.includeExclude = &TermsAggregationIncludeExclude{}
}
a.includeExclude.ExcludeValues = append(a.includeExclude.ExcludeValues, values...)
return a
}
func (a *SignificantTextAggregation) Partition(p int) *SignificantTextAggregation {
if a.includeExclude == nil {
a.includeExclude = &TermsAggregationIncludeExclude{}
}
a.includeExclude.Partition = p
return a
}
func (a *SignificantTextAggregation) NumPartitions(n int) *SignificantTextAggregation {
if a.includeExclude == nil {
a.includeExclude = &TermsAggregationIncludeExclude{}
}
a.includeExclude.NumPartitions = n
return a
}
func (a *SignificantTextAggregation) Source() (interface{}, error) {
// Example:
// {
// "query" : {
// "match" : {"content" : "Bird flu"}
// },
// "aggregations" : {
// "my_sample" : {
// "sampler": {
// "shard_size" : 100
// },
// "aggregations": {
// "keywords" : {
// "significant_text" : { "field" : "content" }
// }
// }
// }
// }
// }
//
// This method returns only the
// { "significant_text" : { "field" : "content" }
// part.
source := make(map[string]interface{})
opts := make(map[string]interface{})
source["significant_text"] = opts
if a.field != "" {
opts["field"] = a.field
}
if a.bucketCountThresholds != nil {
if a.bucketCountThresholds.RequiredSize != nil {
opts["size"] = (*a.bucketCountThresholds).RequiredSize
}
if a.bucketCountThresholds.ShardSize != nil {
opts["shard_size"] = (*a.bucketCountThresholds).ShardSize
}
if a.bucketCountThresholds.MinDocCount != nil {
opts["min_doc_count"] = (*a.bucketCountThresholds).MinDocCount
}
if a.bucketCountThresholds.ShardMinDocCount != nil {
opts["shard_min_doc_count"] = (*a.bucketCountThresholds).ShardMinDocCount
}
}
if a.filter != nil {
src, err := a.filter.Source()
if err != nil {
return nil, err
}
opts["background_filter"] = src
}
if a.significanceHeuristic != nil {
name := a.significanceHeuristic.Name()
src, err := a.significanceHeuristic.Source()
if err != nil {
return nil, err
}
opts[name] = src
}
// Include/Exclude
if ie := a.includeExclude; ie != nil {
// Include
if ie.Include != "" {
opts["include"] = ie.Include
} else if len(ie.IncludeValues) > 0 {
opts["include"] = ie.IncludeValues
} else if ie.NumPartitions > 0 {
inc := make(map[string]interface{})
inc["partition"] = ie.Partition
inc["num_partitions"] = ie.NumPartitions
opts["include"] = inc
}
// Exclude
if ie.Exclude != "" {
opts["exclude"] = ie.Exclude
} else if len(ie.ExcludeValues) > 0 {
opts["exclude"] = ie.ExcludeValues
}
}
// AggregationBuilder (SubAggregations)
if len(a.subAggregations) > 0 {
aggsMap := make(map[string]interface{})
source["aggregations"] = aggsMap
for name, aggregate := range a.subAggregations {
src, err := aggregate.Source()
if err != nil {
return nil, err
}
aggsMap[name] = src
}
}
// Add Meta data if available
if len(a.meta) > 0 {
source["meta"] = a.meta
}
return source, nil
}