forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_aggs_bucket_significant_terms.go
389 lines (331 loc) · 12.5 KB
/
search_aggs_bucket_significant_terms.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// Copyright 2012-2015 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
// SignificantSignificantTermsAggregation is an aggregation that returns interesting
// or unusual occurrences of terms in a set.
// See: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-significantterms-aggregation.html
type SignificantTermsAggregation struct {
field string
subAggregations map[string]Aggregation
meta map[string]interface{}
minDocCount *int
shardMinDocCount *int
requiredSize *int
shardSize *int
filter Query
executionHint string
significanceHeuristic SignificanceHeuristic
}
func NewSignificantTermsAggregation() *SignificantTermsAggregation {
return &SignificantTermsAggregation{
subAggregations: make(map[string]Aggregation, 0),
}
}
func (a *SignificantTermsAggregation) Field(field string) *SignificantTermsAggregation {
a.field = field
return a
}
func (a *SignificantTermsAggregation) SubAggregation(name string, subAggregation Aggregation) *SignificantTermsAggregation {
a.subAggregations[name] = subAggregation
return a
}
// Meta sets the meta data to be included in the aggregation response.
func (a *SignificantTermsAggregation) Meta(metaData map[string]interface{}) *SignificantTermsAggregation {
a.meta = metaData
return a
}
func (a *SignificantTermsAggregation) MinDocCount(minDocCount int) *SignificantTermsAggregation {
a.minDocCount = &minDocCount
return a
}
func (a *SignificantTermsAggregation) ShardMinDocCount(shardMinDocCount int) *SignificantTermsAggregation {
a.shardMinDocCount = &shardMinDocCount
return a
}
func (a *SignificantTermsAggregation) RequiredSize(requiredSize int) *SignificantTermsAggregation {
a.requiredSize = &requiredSize
return a
}
func (a *SignificantTermsAggregation) ShardSize(shardSize int) *SignificantTermsAggregation {
a.shardSize = &shardSize
return a
}
func (a *SignificantTermsAggregation) BackgroundFilter(filter Query) *SignificantTermsAggregation {
a.filter = filter
return a
}
func (a *SignificantTermsAggregation) ExecutionHint(hint string) *SignificantTermsAggregation {
a.executionHint = hint
return a
}
func (a *SignificantTermsAggregation) SignificanceHeuristic(heuristic SignificanceHeuristic) *SignificantTermsAggregation {
a.significanceHeuristic = heuristic
return a
}
func (a *SignificantTermsAggregation) Source() (interface{}, error) {
// Example:
// {
// "query" : {
// "terms" : {"force" : [ "British Transport Police" ]}
// },
// "aggregations" : {
// "significantCrimeTypes" : {
// "significant_terms" : { "field" : "crime_type" }
// }
// }
// }
//
// This method returns only the
// { "significant_terms" : { "field" : "crime_type" }
// part.
source := make(map[string]interface{})
opts := make(map[string]interface{})
source["significant_terms"] = opts
if a.field != "" {
opts["field"] = a.field
}
if a.requiredSize != nil {
opts["size"] = *a.requiredSize // not a typo!
}
if a.shardSize != nil {
opts["shard_size"] = *a.shardSize
}
if a.minDocCount != nil {
opts["min_doc_count"] = *a.minDocCount
}
if a.shardMinDocCount != nil {
opts["shard_min_doc_count"] = *a.shardMinDocCount
}
if a.executionHint != "" {
opts["execution_hint"] = a.executionHint
}
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
}
// 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
}
// -- Significance heuristics --
type SignificanceHeuristic interface {
Name() string
Source() (interface{}, error)
}
// -- Chi Square --
// ChiSquareSignificanceHeuristic implements Chi square as described
// in "Information Retrieval", Manning et al., Chapter 13.5.2.
//
// See https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-significantterms-aggregation.html#_chi_square
// for details.
type ChiSquareSignificanceHeuristic struct {
backgroundIsSuperset *bool
includeNegatives *bool
}
// NewChiSquareSignificanceHeuristic initializes a new ChiSquareSignificanceHeuristic.
func NewChiSquareSignificanceHeuristic() *ChiSquareSignificanceHeuristic {
return &ChiSquareSignificanceHeuristic{}
}
// Name returns the name of the heuristic in the REST interface.
func (sh *ChiSquareSignificanceHeuristic) Name() string {
return "chi_square"
}
// BackgroundIsSuperset indicates whether you defined a custom background
// filter that represents a difference set of documents that you want to
// compare to.
func (sh *ChiSquareSignificanceHeuristic) BackgroundIsSuperset(backgroundIsSuperset bool) *ChiSquareSignificanceHeuristic {
sh.backgroundIsSuperset = &backgroundIsSuperset
return sh
}
// IncludeNegatives indicates whether to filter out the terms that appear
// much less in the subset than in the background without the subset.
func (sh *ChiSquareSignificanceHeuristic) IncludeNegatives(includeNegatives bool) *ChiSquareSignificanceHeuristic {
sh.includeNegatives = &includeNegatives
return sh
}
// Source returns the parameters that need to be added to the REST parameters.
func (sh *ChiSquareSignificanceHeuristic) Source() (interface{}, error) {
source := make(map[string]interface{})
if sh.backgroundIsSuperset != nil {
source["background_is_superset"] = *sh.backgroundIsSuperset
}
if sh.includeNegatives != nil {
source["include_negatives"] = *sh.includeNegatives
}
return source, nil
}
// -- GND --
// GNDSignificanceHeuristic implements the "Google Normalized Distance"
// as described in "The Google Similarity Distance", Cilibrasi and Vitanyi,
// 2007.
//
// See https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-significantterms-aggregation.html#_google_normalized_distance
// for details.
type GNDSignificanceHeuristic struct {
backgroundIsSuperset *bool
}
// NewGNDSignificanceHeuristic implements a new GNDSignificanceHeuristic.
func NewGNDSignificanceHeuristic() *GNDSignificanceHeuristic {
return &GNDSignificanceHeuristic{}
}
// Name returns the name of the heuristic in the REST interface.
func (sh *GNDSignificanceHeuristic) Name() string {
return "gnd"
}
// BackgroundIsSuperset indicates whether you defined a custom background
// filter that represents a difference set of documents that you want to
// compare to.
func (sh *GNDSignificanceHeuristic) BackgroundIsSuperset(backgroundIsSuperset bool) *GNDSignificanceHeuristic {
sh.backgroundIsSuperset = &backgroundIsSuperset
return sh
}
// Source returns the parameters that need to be added to the REST parameters.
func (sh *GNDSignificanceHeuristic) Source() (interface{}, error) {
source := make(map[string]interface{})
if sh.backgroundIsSuperset != nil {
source["background_is_superset"] = *sh.backgroundIsSuperset
}
return source, nil
}
// -- JLH Score --
// JLHScoreSignificanceHeuristic implements the JLH score as described in
// https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-significantterms-aggregation.html#_jlh_score.
type JLHScoreSignificanceHeuristic struct{}
// NewJLHScoreSignificanceHeuristic initializes a new JLHScoreSignificanceHeuristic.
func NewJLHScoreSignificanceHeuristic() *JLHScoreSignificanceHeuristic {
return &JLHScoreSignificanceHeuristic{}
}
// Name returns the name of the heuristic in the REST interface.
func (sh *JLHScoreSignificanceHeuristic) Name() string {
return "jlh"
}
// Source returns the parameters that need to be added to the REST parameters.
func (sh *JLHScoreSignificanceHeuristic) Source() (interface{}, error) {
source := make(map[string]interface{})
return source, nil
}
// -- Mutual Information --
// MutualInformationSignificanceHeuristic implements Mutual information
// as described in "Information Retrieval", Manning et al., Chapter 13.5.1.
//
// See https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-significantterms-aggregation.html#_mutual_information
// for details.
type MutualInformationSignificanceHeuristic struct {
backgroundIsSuperset *bool
includeNegatives *bool
}
// NewMutualInformationSignificanceHeuristic initializes a new instance of
// MutualInformationSignificanceHeuristic.
func NewMutualInformationSignificanceHeuristic() *MutualInformationSignificanceHeuristic {
return &MutualInformationSignificanceHeuristic{}
}
// Name returns the name of the heuristic in the REST interface.
func (sh *MutualInformationSignificanceHeuristic) Name() string {
return "mutual_information"
}
// BackgroundIsSuperset indicates whether you defined a custom background
// filter that represents a difference set of documents that you want to
// compare to.
func (sh *MutualInformationSignificanceHeuristic) BackgroundIsSuperset(backgroundIsSuperset bool) *MutualInformationSignificanceHeuristic {
sh.backgroundIsSuperset = &backgroundIsSuperset
return sh
}
// IncludeNegatives indicates whether to filter out the terms that appear
// much less in the subset than in the background without the subset.
func (sh *MutualInformationSignificanceHeuristic) IncludeNegatives(includeNegatives bool) *MutualInformationSignificanceHeuristic {
sh.includeNegatives = &includeNegatives
return sh
}
// Source returns the parameters that need to be added to the REST parameters.
func (sh *MutualInformationSignificanceHeuristic) Source() (interface{}, error) {
source := make(map[string]interface{})
if sh.backgroundIsSuperset != nil {
source["background_is_superset"] = *sh.backgroundIsSuperset
}
if sh.includeNegatives != nil {
source["include_negatives"] = *sh.includeNegatives
}
return source, nil
}
// -- Percentage Score --
// PercentageScoreSignificanceHeuristic implements the algorithm described
// in https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-significantterms-aggregation.html#_percentage.
type PercentageScoreSignificanceHeuristic struct{}
// NewPercentageScoreSignificanceHeuristic initializes a new instance of
// PercentageScoreSignificanceHeuristic.
func NewPercentageScoreSignificanceHeuristic() *PercentageScoreSignificanceHeuristic {
return &PercentageScoreSignificanceHeuristic{}
}
// Name returns the name of the heuristic in the REST interface.
func (sh *PercentageScoreSignificanceHeuristic) Name() string {
return "percentage"
}
// Source returns the parameters that need to be added to the REST parameters.
func (sh *PercentageScoreSignificanceHeuristic) Source() (interface{}, error) {
source := make(map[string]interface{})
return source, nil
}
// -- Script --
// ScriptSignificanceHeuristic implements a scripted significance heuristic.
// See https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-significantterms-aggregation.html#_scripted
// for details.
type ScriptSignificanceHeuristic struct {
script *Script
}
// NewScriptSignificanceHeuristic initializes a new instance of
// ScriptSignificanceHeuristic.
func NewScriptSignificanceHeuristic() *ScriptSignificanceHeuristic {
return &ScriptSignificanceHeuristic{}
}
// Name returns the name of the heuristic in the REST interface.
func (sh *ScriptSignificanceHeuristic) Name() string {
return "script_heuristic"
}
// Script specifies the script to use to get custom scores. The following
// parameters are available in the script: `_subset_freq`, `_superset_freq`,
// `_subset_size`, and `_superset_size`.
//
// See https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-significantterms-aggregation.html#_scripted
// for details.
func (sh *ScriptSignificanceHeuristic) Script(script *Script) *ScriptSignificanceHeuristic {
sh.script = script
return sh
}
// Source returns the parameters that need to be added to the REST parameters.
func (sh *ScriptSignificanceHeuristic) Source() (interface{}, error) {
source := make(map[string]interface{})
if sh.script != nil {
src, err := sh.script.Source()
if err != nil {
return nil, err
}
source["script"] = src
}
return source, nil
}