| 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 |
// SignificantSignificantTermsAggregation is an aggregation that returns interesting |
| 8 |
// or unusual occurrences of terms in a set. |
| 9 |
// See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-significantterms-aggregation.html |
| 10 |
type SignificantTermsAggregation struct { |
| 11 |
field string |
| 12 |
subAggregations map[string]Aggregation |
| 13 |
meta map[string]interface{} |
| 14 |
|
| 15 |
minDocCount *int |
| 16 |
shardMinDocCount *int |
| 17 |
requiredSize *int |
| 18 |
shardSize *int |
| 19 |
filter Query |
| 20 |
executionHint string |
| 21 |
significanceHeuristic SignificanceHeuristic |
| 22 |
} |
| 23 |
|
| 24 |
func NewSignificantTermsAggregation() *SignificantTermsAggregation { |
| 25 |
return &SignificantTermsAggregation{ |
| 26 |
subAggregations: make(map[string]Aggregation, 0), |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
func (a *SignificantTermsAggregation) Field(field string) *SignificantTermsAggregation { |
| 31 |
a.field = field |
| 32 |
return a |
| 33 |
} |
| 34 |
|
| 35 |
func (a *SignificantTermsAggregation) SubAggregation(name string, subAggregation Aggregation) *SignificantTermsAggregation { |
| 36 |
a.subAggregations[name] = subAggregation |
| 37 |
return a |
| 38 |
} |
| 39 |
|
| 40 |
// Meta sets the meta data to be included in the aggregation response. |
| 41 |
func (a *SignificantTermsAggregation) Meta(metaData map[string]interface{}) *SignificantTermsAggregation { |
| 42 |
a.meta = metaData |
| 43 |
return a |
| 44 |
} |
| 45 |
|
| 46 |
func (a *SignificantTermsAggregation) MinDocCount(minDocCount int) *SignificantTermsAggregation { |
| 47 |
a.minDocCount = &minDocCount |
| 48 |
return a |
| 49 |
} |
| 50 |
|
| 51 |
func (a *SignificantTermsAggregation) ShardMinDocCount(shardMinDocCount int) *SignificantTermsAggregation { |
| 52 |
a.shardMinDocCount = &shardMinDocCount |
| 53 |
return a |
| 54 |
} |
| 55 |
|
| 56 |
func (a *SignificantTermsAggregation) RequiredSize(requiredSize int) *SignificantTermsAggregation { |
| 57 |
a.requiredSize = &requiredSize |
| 58 |
return a |
| 59 |
} |
| 60 |
|
| 61 |
func (a *SignificantTermsAggregation) ShardSize(shardSize int) *SignificantTermsAggregation { |
| 62 |
a.shardSize = &shardSize |
| 63 |
return a |
| 64 |
} |
| 65 |
|
| 66 |
func (a *SignificantTermsAggregation) BackgroundFilter(filter Query) *SignificantTermsAggregation { |
| 67 |
a.filter = filter |
| 68 |
return a |
| 69 |
} |
| 70 |
|
| 71 |
func (a *SignificantTermsAggregation) ExecutionHint(hint string) *SignificantTermsAggregation { |
| 72 |
a.executionHint = hint |
| 73 |
return a |
| 74 |
} |
| 75 |
|
| 76 |
func (a *SignificantTermsAggregation) SignificanceHeuristic(heuristic SignificanceHeuristic) *SignificantTermsAggregation { |
| 77 |
a.significanceHeuristic = heuristic |
| 78 |
return a |
| 79 |
} |
| 80 |
|
| 81 |
func (a *SignificantTermsAggregation) Source() (interface{}, error) { |
| 82 |
// Example: |
| 83 |
// { |
| 84 |
// "query" : { |
| 85 |
// "terms" : {"force" : [ "British Transport Police" ]} |
| 86 |
// }, |
| 87 |
// "aggregations" : { |
| 88 |
// "significantCrimeTypes" : { |
| 89 |
// "significant_terms" : { "field" : "crime_type" } |
| 90 |
// } |
| 91 |
// } |
| 92 |
// } |
| 93 |
// |
| 94 |
// This method returns only the |
| 95 |
// { "significant_terms" : { "field" : "crime_type" } |
| 96 |
// part. |
| 97 |
|
| 98 |
source := make(map[string]interface{}) |
| 99 |
opts := make(map[string]interface{}) |
| 100 |
source["significant_terms"] = opts |
| 101 |
|
| 102 |
if a.field != "" { |
| 103 |
opts["field"] = a.field |
| 104 |
} |
| 105 |
if a.requiredSize != nil { |
| 106 |
opts["size"] = *a.requiredSize // not a typo! |
| 107 |
} |
| 108 |
if a.shardSize != nil { |
| 109 |
opts["shard_size"] = *a.shardSize |
| 110 |
} |
| 111 |
if a.minDocCount != nil { |
| 112 |
opts["min_doc_count"] = *a.minDocCount |
| 113 |
} |
| 114 |
if a.shardMinDocCount != nil { |
| 115 |
opts["shard_min_doc_count"] = *a.shardMinDocCount |
| 116 |
} |
| 117 |
if a.executionHint != "" { |
| 118 |
opts["execution_hint"] = a.executionHint |
| 119 |
} |
| 120 |
if a.filter != nil { |
| 121 |
src, err := a.filter.Source() |
| 122 |
if err != nil { |
| 123 |
return nil, err |
| 124 |
} |
| 125 |
opts["background_filter"] = src |
| 126 |
} |
| 127 |
if a.significanceHeuristic != nil { |
| 128 |
name := a.significanceHeuristic.Name() |
| 129 |
src, err := a.significanceHeuristic.Source() |
| 130 |
if err != nil { |
| 131 |
return nil, err |
| 132 |
} |
| 133 |
opts[name] = src |
| 134 |
} |
| 135 |
|
| 136 |
// AggregationBuilder (SubAggregations) |
| 137 |
if len(a.subAggregations) > 0 { |
| 138 |
aggsMap := make(map[string]interface{}) |
| 139 |
source["aggregations"] = aggsMap |
| 140 |
for name, aggregate := range a.subAggregations { |
| 141 |
src, err := aggregate.Source() |
| 142 |
if err != nil { |
| 143 |
return nil, err |
| 144 |
} |
| 145 |
aggsMap[name] = src |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
// Add Meta data if available |
| 150 |
if len(a.meta) > 0 { |
| 151 |
source["meta"] = a.meta |
| 152 |
} |
| 153 |
|
| 154 |
return source, nil |
| 155 |
} |
| 156 |
|
| 157 |
// -- Significance heuristics -- |
| 158 |
|
| 159 |
type SignificanceHeuristic interface { |
| 160 |
Name() string |
| 161 |
Source() (interface{}, error) |
| 162 |
} |
| 163 |
|
| 164 |
// -- Chi Square -- |
| 165 |
|
| 166 |
// ChiSquareSignificanceHeuristic implements Chi square as described |
| 167 |
// in "Information Retrieval", Manning et al., Chapter 13.5.2. |
| 168 |
// |
| 169 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-significantterms-aggregation.html#_chi_square |
| 170 |
// for details. |
| 171 |
type ChiSquareSignificanceHeuristic struct { |
| 172 |
backgroundIsSuperset *bool |
| 173 |
includeNegatives *bool |
| 174 |
} |
| 175 |
|
| 176 |
// NewChiSquareSignificanceHeuristic initializes a new ChiSquareSignificanceHeuristic. |
| 177 |
func NewChiSquareSignificanceHeuristic() *ChiSquareSignificanceHeuristic { |
| 178 |
return &ChiSquareSignificanceHeuristic{} |
| 179 |
} |
| 180 |
|
| 181 |
// Name returns the name of the heuristic in the REST interface. |
| 182 |
func (sh *ChiSquareSignificanceHeuristic) Name() string { |
| 183 |
return "chi_square" |
| 184 |
} |
| 185 |
|
| 186 |
// BackgroundIsSuperset indicates whether you defined a custom background |
| 187 |
// filter that represents a difference set of documents that you want to |
| 188 |
// compare to. |
| 189 |
func (sh *ChiSquareSignificanceHeuristic) BackgroundIsSuperset(backgroundIsSuperset bool) *ChiSquareSignificanceHeuristic { |
| 190 |
sh.backgroundIsSuperset = &backgroundIsSuperset |
| 191 |
return sh |
| 192 |
} |
| 193 |
|
| 194 |
// IncludeNegatives indicates whether to filter out the terms that appear |
| 195 |
// much less in the subset than in the background without the subset. |
| 196 |
func (sh *ChiSquareSignificanceHeuristic) IncludeNegatives(includeNegatives bool) *ChiSquareSignificanceHeuristic { |
| 197 |
sh.includeNegatives = &includeNegatives |
| 198 |
return sh |
| 199 |
} |
| 200 |
|
| 201 |
// Source returns the parameters that need to be added to the REST parameters. |
| 202 |
func (sh *ChiSquareSignificanceHeuristic) Source() (interface{}, error) { |
| 203 |
source := make(map[string]interface{}) |
| 204 |
if sh.backgroundIsSuperset != nil { |
| 205 |
source["background_is_superset"] = *sh.backgroundIsSuperset |
| 206 |
} |
| 207 |
if sh.includeNegatives != nil { |
| 208 |
source["include_negatives"] = *sh.includeNegatives |
| 209 |
} |
| 210 |
return source, nil |
| 211 |
} |
| 212 |
|
| 213 |
// -- GND -- |
| 214 |
|
| 215 |
// GNDSignificanceHeuristic implements the "Google Normalized Distance" |
| 216 |
// as described in "The Google Similarity Distance", Cilibrasi and Vitanyi, |
| 217 |
// 2007. |
| 218 |
// |
| 219 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-significantterms-aggregation.html#_google_normalized_distance |
| 220 |
// for details. |
| 221 |
type GNDSignificanceHeuristic struct { |
| 222 |
backgroundIsSuperset *bool |
| 223 |
} |
| 224 |
|
| 225 |
// NewGNDSignificanceHeuristic implements a new GNDSignificanceHeuristic. |
| 226 |
func NewGNDSignificanceHeuristic() *GNDSignificanceHeuristic { |
| 227 |
return &GNDSignificanceHeuristic{} |
| 228 |
} |
| 229 |
|
| 230 |
// Name returns the name of the heuristic in the REST interface. |
| 231 |
func (sh *GNDSignificanceHeuristic) Name() string { |
| 232 |
return "gnd" |
| 233 |
} |
| 234 |
|
| 235 |
// BackgroundIsSuperset indicates whether you defined a custom background |
| 236 |
// filter that represents a difference set of documents that you want to |
| 237 |
// compare to. |
| 238 |
func (sh *GNDSignificanceHeuristic) BackgroundIsSuperset(backgroundIsSuperset bool) *GNDSignificanceHeuristic { |
| 239 |
sh.backgroundIsSuperset = &backgroundIsSuperset |
| 240 |
return sh |
| 241 |
} |
| 242 |
|
| 243 |
// Source returns the parameters that need to be added to the REST parameters. |
| 244 |
func (sh *GNDSignificanceHeuristic) Source() (interface{}, error) { |
| 245 |
source := make(map[string]interface{}) |
| 246 |
if sh.backgroundIsSuperset != nil { |
| 247 |
source["background_is_superset"] = *sh.backgroundIsSuperset |
| 248 |
} |
| 249 |
return source, nil |
| 250 |
} |
| 251 |
|
| 252 |
// -- JLH Score -- |
| 253 |
|
| 254 |
// JLHScoreSignificanceHeuristic implements the JLH score as described in |
| 255 |
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-significantterms-aggregation.html#_jlh_score. |
| 256 |
type JLHScoreSignificanceHeuristic struct{} |
| 257 |
|
| 258 |
// NewJLHScoreSignificanceHeuristic initializes a new JLHScoreSignificanceHeuristic. |
| 259 |
func NewJLHScoreSignificanceHeuristic() *JLHScoreSignificanceHeuristic { |
| 260 |
return &JLHScoreSignificanceHeuristic{} |
| 261 |
} |
| 262 |
|
| 263 |
// Name returns the name of the heuristic in the REST interface. |
| 264 |
func (sh *JLHScoreSignificanceHeuristic) Name() string { |
| 265 |
return "jlh" |
| 266 |
} |
| 267 |
|
| 268 |
// Source returns the parameters that need to be added to the REST parameters. |
| 269 |
func (sh *JLHScoreSignificanceHeuristic) Source() (interface{}, error) { |
| 270 |
source := make(map[string]interface{}) |
| 271 |
return source, nil |
| 272 |
} |
| 273 |
|
| 274 |
// -- Mutual Information -- |
| 275 |
|
| 276 |
// MutualInformationSignificanceHeuristic implements Mutual information |
| 277 |
// as described in "Information Retrieval", Manning et al., Chapter 13.5.1. |
| 278 |
// |
| 279 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-significantterms-aggregation.html#_mutual_information |
| 280 |
// for details. |
| 281 |
type MutualInformationSignificanceHeuristic struct { |
| 282 |
backgroundIsSuperset *bool |
| 283 |
includeNegatives *bool |
| 284 |
} |
| 285 |
|
| 286 |
// NewMutualInformationSignificanceHeuristic initializes a new instance of |
| 287 |
// MutualInformationSignificanceHeuristic. |
| 288 |
func NewMutualInformationSignificanceHeuristic() *MutualInformationSignificanceHeuristic { |
| 289 |
return &MutualInformationSignificanceHeuristic{} |
| 290 |
} |
| 291 |
|
| 292 |
// Name returns the name of the heuristic in the REST interface. |
| 293 |
func (sh *MutualInformationSignificanceHeuristic) Name() string { |
| 294 |
return "mutual_information" |
| 295 |
} |
| 296 |
|
| 297 |
// BackgroundIsSuperset indicates whether you defined a custom background |
| 298 |
// filter that represents a difference set of documents that you want to |
| 299 |
// compare to. |
| 300 |
func (sh *MutualInformationSignificanceHeuristic) BackgroundIsSuperset(backgroundIsSuperset bool) *MutualInformationSignificanceHeuristic { |
| 301 |
sh.backgroundIsSuperset = &backgroundIsSuperset |
| 302 |
return sh |
| 303 |
} |
| 304 |
|
| 305 |
// IncludeNegatives indicates whether to filter out the terms that appear |
| 306 |
// much less in the subset than in the background without the subset. |
| 307 |
func (sh *MutualInformationSignificanceHeuristic) IncludeNegatives(includeNegatives bool) *MutualInformationSignificanceHeuristic { |
| 308 |
sh.includeNegatives = &includeNegatives |
| 309 |
return sh |
| 310 |
} |
| 311 |
|
| 312 |
// Source returns the parameters that need to be added to the REST parameters. |
| 313 |
func (sh *MutualInformationSignificanceHeuristic) Source() (interface{}, error) { |
| 314 |
source := make(map[string]interface{}) |
| 315 |
if sh.backgroundIsSuperset != nil { |
| 316 |
source["background_is_superset"] = *sh.backgroundIsSuperset |
| 317 |
} |
| 318 |
if sh.includeNegatives != nil { |
| 319 |
source["include_negatives"] = *sh.includeNegatives |
| 320 |
} |
| 321 |
return source, nil |
| 322 |
} |
| 323 |
|
| 324 |
// -- Percentage Score -- |
| 325 |
|
| 326 |
// PercentageScoreSignificanceHeuristic implements the algorithm described |
| 327 |
// in https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-significantterms-aggregation.html#_percentage. |
| 328 |
type PercentageScoreSignificanceHeuristic struct{} |
| 329 |
|
| 330 |
// NewPercentageScoreSignificanceHeuristic initializes a new instance of |
| 331 |
// PercentageScoreSignificanceHeuristic. |
| 332 |
func NewPercentageScoreSignificanceHeuristic() *PercentageScoreSignificanceHeuristic { |
| 333 |
return &PercentageScoreSignificanceHeuristic{} |
| 334 |
} |
| 335 |
|
| 336 |
// Name returns the name of the heuristic in the REST interface. |
| 337 |
func (sh *PercentageScoreSignificanceHeuristic) Name() string { |
| 338 |
return "percentage" |
| 339 |
} |
| 340 |
|
| 341 |
// Source returns the parameters that need to be added to the REST parameters. |
| 342 |
func (sh *PercentageScoreSignificanceHeuristic) Source() (interface{}, error) { |
| 343 |
source := make(map[string]interface{}) |
| 344 |
return source, nil |
| 345 |
} |
| 346 |
|
| 347 |
// -- Script -- |
| 348 |
|
| 349 |
// ScriptSignificanceHeuristic implements a scripted significance heuristic. |
| 350 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-significantterms-aggregation.html#_scripted |
| 351 |
// for details. |
| 352 |
type ScriptSignificanceHeuristic struct { |
| 353 |
script *Script |
| 354 |
} |
| 355 |
|
| 356 |
// NewScriptSignificanceHeuristic initializes a new instance of |
| 357 |
// ScriptSignificanceHeuristic. |
| 358 |
func NewScriptSignificanceHeuristic() *ScriptSignificanceHeuristic { |
| 359 |
return &ScriptSignificanceHeuristic{} |
| 360 |
} |
| 361 |
|
| 362 |
// Name returns the name of the heuristic in the REST interface. |
| 363 |
func (sh *ScriptSignificanceHeuristic) Name() string { |
| 364 |
return "script_heuristic" |
| 365 |
} |
| 366 |
|
| 367 |
// Script specifies the script to use to get custom scores. The following |
| 368 |
// parameters are available in the script: `_subset_freq`, `_superset_freq`, |
| 369 |
// `_subset_size`, and `_superset_size`. |
| 370 |
// |
| 371 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-significantterms-aggregation.html#_scripted |
| 372 |
// for details. |
| 373 |
func (sh *ScriptSignificanceHeuristic) Script(script *Script) *ScriptSignificanceHeuristic { |
| 374 |
sh.script = script |
| 375 |
return sh |
| 376 |
} |
| 377 |
|
| 378 |
// Source returns the parameters that need to be added to the REST parameters. |
| 379 |
func (sh *ScriptSignificanceHeuristic) Source() (interface{}, error) { |
| 380 |
source := make(map[string]interface{}) |
| 381 |
if sh.script != nil { |
| 382 |
src, err := sh.script.Source() |
| 383 |
if err != nil { |
| 384 |
return nil, err |
| 385 |
} |
| 386 |
source["script"] = src |
| 387 |
} |
| 388 |
return source, nil |
| 389 |
} |