| 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 |
import ( |
| 8 |
"strings" |
| 9 |
) |
| 10 |
|
| 11 |
// ScoreFunction is used in combination with the Function Score Query. |
| 12 |
type ScoreFunction interface { |
| 13 |
Name() string |
| 14 |
GetWeight() *float64 // returns the weight which must be serialized at the level of FunctionScoreQuery |
| 15 |
Source() (interface{}, error) |
| 16 |
} |
| 17 |
|
| 18 |
// -- Exponential Decay -- |
| 19 |
|
| 20 |
// ExponentialDecayFunction builds an exponential decay score function. |
| 21 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-function-score-query.html |
| 22 |
// for details. |
| 23 |
type ExponentialDecayFunction struct { |
| 24 |
fieldName string |
| 25 |
origin interface{} |
| 26 |
scale interface{} |
| 27 |
decay *float64 |
| 28 |
offset interface{} |
| 29 |
multiValueMode string |
| 30 |
weight *float64 |
| 31 |
} |
| 32 |
|
| 33 |
// NewExponentialDecayFunction creates a new ExponentialDecayFunction. |
| 34 |
func NewExponentialDecayFunction() *ExponentialDecayFunction { |
| 35 |
return &ExponentialDecayFunction{} |
| 36 |
} |
| 37 |
|
| 38 |
// Name represents the JSON field name under which the output of Source |
| 39 |
// needs to be serialized by FunctionScoreQuery (see FunctionScoreQuery.Source). |
| 40 |
func (fn *ExponentialDecayFunction) Name() string { |
| 41 |
return "exp" |
| 42 |
} |
| 43 |
|
| 44 |
// FieldName specifies the name of the field to which this decay function is applied to. |
| 45 |
func (fn *ExponentialDecayFunction) FieldName(fieldName string) *ExponentialDecayFunction { |
| 46 |
fn.fieldName = fieldName |
| 47 |
return fn |
| 48 |
} |
| 49 |
|
| 50 |
// Origin defines the "central point" by which the decay function calculates |
| 51 |
// "distance". |
| 52 |
func (fn *ExponentialDecayFunction) Origin(origin interface{}) *ExponentialDecayFunction { |
| 53 |
fn.origin = origin |
| 54 |
return fn |
| 55 |
} |
| 56 |
|
| 57 |
// Scale defines the scale to be used with Decay. |
| 58 |
func (fn *ExponentialDecayFunction) Scale(scale interface{}) *ExponentialDecayFunction { |
| 59 |
fn.scale = scale |
| 60 |
return fn |
| 61 |
} |
| 62 |
|
| 63 |
// Decay defines how documents are scored at the distance given a Scale. |
| 64 |
// If no decay is defined, documents at the distance Scale will be scored 0.5. |
| 65 |
func (fn *ExponentialDecayFunction) Decay(decay float64) *ExponentialDecayFunction { |
| 66 |
fn.decay = &decay |
| 67 |
return fn |
| 68 |
} |
| 69 |
|
| 70 |
// Offset, if defined, computes the decay function only for a distance |
| 71 |
// greater than the defined offset. |
| 72 |
func (fn *ExponentialDecayFunction) Offset(offset interface{}) *ExponentialDecayFunction { |
| 73 |
fn.offset = offset |
| 74 |
return fn |
| 75 |
} |
| 76 |
|
| 77 |
// Weight adjusts the score of the score function. |
| 78 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-function-score-query.html#_using_function_score |
| 79 |
// for details. |
| 80 |
func (fn *ExponentialDecayFunction) Weight(weight float64) *ExponentialDecayFunction { |
| 81 |
fn.weight = &weight |
| 82 |
return fn |
| 83 |
} |
| 84 |
|
| 85 |
// GetWeight returns the adjusted score. It is part of the ScoreFunction interface. |
| 86 |
// Returns nil if weight is not specified. |
| 87 |
func (fn *ExponentialDecayFunction) GetWeight() *float64 { |
| 88 |
return fn.weight |
| 89 |
} |
| 90 |
|
| 91 |
// MultiValueMode specifies how the decay function should be calculated |
| 92 |
// on a field that has multiple values. |
| 93 |
// Valid modes are: min, max, avg, and sum. |
| 94 |
func (fn *ExponentialDecayFunction) MultiValueMode(mode string) *ExponentialDecayFunction { |
| 95 |
fn.multiValueMode = mode |
| 96 |
return fn |
| 97 |
} |
| 98 |
|
| 99 |
// Source returns the serializable JSON data of this score function. |
| 100 |
func (fn *ExponentialDecayFunction) Source() (interface{}, error) { |
| 101 |
source := make(map[string]interface{}) |
| 102 |
params := make(map[string]interface{}) |
| 103 |
source[fn.fieldName] = params |
| 104 |
if fn.origin != nil { |
| 105 |
params["origin"] = fn.origin |
| 106 |
} |
| 107 |
params["scale"] = fn.scale |
| 108 |
if fn.decay != nil && *fn.decay > 0 { |
| 109 |
params["decay"] = *fn.decay |
| 110 |
} |
| 111 |
if fn.offset != nil { |
| 112 |
params["offset"] = fn.offset |
| 113 |
} |
| 114 |
if fn.multiValueMode != "" { |
| 115 |
source["multi_value_mode"] = fn.multiValueMode |
| 116 |
} |
| 117 |
return source, nil |
| 118 |
} |
| 119 |
|
| 120 |
// -- Gauss Decay -- |
| 121 |
|
| 122 |
// GaussDecayFunction builds a gauss decay score function. |
| 123 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-function-score-query.html |
| 124 |
// for details. |
| 125 |
type GaussDecayFunction struct { |
| 126 |
fieldName string |
| 127 |
origin interface{} |
| 128 |
scale interface{} |
| 129 |
decay *float64 |
| 130 |
offset interface{} |
| 131 |
multiValueMode string |
| 132 |
weight *float64 |
| 133 |
} |
| 134 |
|
| 135 |
// NewGaussDecayFunction returns a new GaussDecayFunction. |
| 136 |
func NewGaussDecayFunction() *GaussDecayFunction { |
| 137 |
return &GaussDecayFunction{} |
| 138 |
} |
| 139 |
|
| 140 |
// Name represents the JSON field name under which the output of Source |
| 141 |
// needs to be serialized by FunctionScoreQuery (see FunctionScoreQuery.Source). |
| 142 |
func (fn *GaussDecayFunction) Name() string { |
| 143 |
return "gauss" |
| 144 |
} |
| 145 |
|
| 146 |
// FieldName specifies the name of the field to which this decay function is applied to. |
| 147 |
func (fn *GaussDecayFunction) FieldName(fieldName string) *GaussDecayFunction { |
| 148 |
fn.fieldName = fieldName |
| 149 |
return fn |
| 150 |
} |
| 151 |
|
| 152 |
// Origin defines the "central point" by which the decay function calculates |
| 153 |
// "distance". |
| 154 |
func (fn *GaussDecayFunction) Origin(origin interface{}) *GaussDecayFunction { |
| 155 |
fn.origin = origin |
| 156 |
return fn |
| 157 |
} |
| 158 |
|
| 159 |
// Scale defines the scale to be used with Decay. |
| 160 |
func (fn *GaussDecayFunction) Scale(scale interface{}) *GaussDecayFunction { |
| 161 |
fn.scale = scale |
| 162 |
return fn |
| 163 |
} |
| 164 |
|
| 165 |
// Decay defines how documents are scored at the distance given a Scale. |
| 166 |
// If no decay is defined, documents at the distance Scale will be scored 0.5. |
| 167 |
func (fn *GaussDecayFunction) Decay(decay float64) *GaussDecayFunction { |
| 168 |
fn.decay = &decay |
| 169 |
return fn |
| 170 |
} |
| 171 |
|
| 172 |
// Offset, if defined, computes the decay function only for a distance |
| 173 |
// greater than the defined offset. |
| 174 |
func (fn *GaussDecayFunction) Offset(offset interface{}) *GaussDecayFunction { |
| 175 |
fn.offset = offset |
| 176 |
return fn |
| 177 |
} |
| 178 |
|
| 179 |
// Weight adjusts the score of the score function. |
| 180 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-function-score-query.html#_using_function_score |
| 181 |
// for details. |
| 182 |
func (fn *GaussDecayFunction) Weight(weight float64) *GaussDecayFunction { |
| 183 |
fn.weight = &weight |
| 184 |
return fn |
| 185 |
} |
| 186 |
|
| 187 |
// GetWeight returns the adjusted score. It is part of the ScoreFunction interface. |
| 188 |
// Returns nil if weight is not specified. |
| 189 |
func (fn *GaussDecayFunction) GetWeight() *float64 { |
| 190 |
return fn.weight |
| 191 |
} |
| 192 |
|
| 193 |
// MultiValueMode specifies how the decay function should be calculated |
| 194 |
// on a field that has multiple values. |
| 195 |
// Valid modes are: min, max, avg, and sum. |
| 196 |
func (fn *GaussDecayFunction) MultiValueMode(mode string) *GaussDecayFunction { |
| 197 |
fn.multiValueMode = mode |
| 198 |
return fn |
| 199 |
} |
| 200 |
|
| 201 |
// Source returns the serializable JSON data of this score function. |
| 202 |
func (fn *GaussDecayFunction) Source() (interface{}, error) { |
| 203 |
source := make(map[string]interface{}) |
| 204 |
params := make(map[string]interface{}) |
| 205 |
source[fn.fieldName] = params |
| 206 |
if fn.origin != nil { |
| 207 |
params["origin"] = fn.origin |
| 208 |
} |
| 209 |
params["scale"] = fn.scale |
| 210 |
if fn.decay != nil && *fn.decay > 0 { |
| 211 |
params["decay"] = *fn.decay |
| 212 |
} |
| 213 |
if fn.offset != nil { |
| 214 |
params["offset"] = fn.offset |
| 215 |
} |
| 216 |
if fn.multiValueMode != "" { |
| 217 |
source["multi_value_mode"] = fn.multiValueMode |
| 218 |
} |
| 219 |
// Notice that the weight has to be serialized in FunctionScoreQuery. |
| 220 |
return source, nil |
| 221 |
} |
| 222 |
|
| 223 |
// -- Linear Decay -- |
| 224 |
|
| 225 |
// LinearDecayFunction builds a linear decay score function. |
| 226 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-function-score-query.html |
| 227 |
// for details. |
| 228 |
type LinearDecayFunction struct { |
| 229 |
fieldName string |
| 230 |
origin interface{} |
| 231 |
scale interface{} |
| 232 |
decay *float64 |
| 233 |
offset interface{} |
| 234 |
multiValueMode string |
| 235 |
weight *float64 |
| 236 |
} |
| 237 |
|
| 238 |
// NewLinearDecayFunction initializes and returns a new LinearDecayFunction. |
| 239 |
func NewLinearDecayFunction() *LinearDecayFunction { |
| 240 |
return &LinearDecayFunction{} |
| 241 |
} |
| 242 |
|
| 243 |
// Name represents the JSON field name under which the output of Source |
| 244 |
// needs to be serialized by FunctionScoreQuery (see FunctionScoreQuery.Source). |
| 245 |
func (fn *LinearDecayFunction) Name() string { |
| 246 |
return "linear" |
| 247 |
} |
| 248 |
|
| 249 |
// FieldName specifies the name of the field to which this decay function is applied to. |
| 250 |
func (fn *LinearDecayFunction) FieldName(fieldName string) *LinearDecayFunction { |
| 251 |
fn.fieldName = fieldName |
| 252 |
return fn |
| 253 |
} |
| 254 |
|
| 255 |
// Origin defines the "central point" by which the decay function calculates |
| 256 |
// "distance". |
| 257 |
func (fn *LinearDecayFunction) Origin(origin interface{}) *LinearDecayFunction { |
| 258 |
fn.origin = origin |
| 259 |
return fn |
| 260 |
} |
| 261 |
|
| 262 |
// Scale defines the scale to be used with Decay. |
| 263 |
func (fn *LinearDecayFunction) Scale(scale interface{}) *LinearDecayFunction { |
| 264 |
fn.scale = scale |
| 265 |
return fn |
| 266 |
} |
| 267 |
|
| 268 |
// Decay defines how documents are scored at the distance given a Scale. |
| 269 |
// If no decay is defined, documents at the distance Scale will be scored 0.5. |
| 270 |
func (fn *LinearDecayFunction) Decay(decay float64) *LinearDecayFunction { |
| 271 |
fn.decay = &decay |
| 272 |
return fn |
| 273 |
} |
| 274 |
|
| 275 |
// Offset, if defined, computes the decay function only for a distance |
| 276 |
// greater than the defined offset. |
| 277 |
func (fn *LinearDecayFunction) Offset(offset interface{}) *LinearDecayFunction { |
| 278 |
fn.offset = offset |
| 279 |
return fn |
| 280 |
} |
| 281 |
|
| 282 |
// Weight adjusts the score of the score function. |
| 283 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-function-score-query.html#_using_function_score |
| 284 |
// for details. |
| 285 |
func (fn *LinearDecayFunction) Weight(weight float64) *LinearDecayFunction { |
| 286 |
fn.weight = &weight |
| 287 |
return fn |
| 288 |
} |
| 289 |
|
| 290 |
// GetWeight returns the adjusted score. It is part of the ScoreFunction interface. |
| 291 |
// Returns nil if weight is not specified. |
| 292 |
func (fn *LinearDecayFunction) GetWeight() *float64 { |
| 293 |
return fn.weight |
| 294 |
} |
| 295 |
|
| 296 |
// MultiValueMode specifies how the decay function should be calculated |
| 297 |
// on a field that has multiple values. |
| 298 |
// Valid modes are: min, max, avg, and sum. |
| 299 |
func (fn *LinearDecayFunction) MultiValueMode(mode string) *LinearDecayFunction { |
| 300 |
fn.multiValueMode = mode |
| 301 |
return fn |
| 302 |
} |
| 303 |
|
| 304 |
// GetMultiValueMode returns how the decay function should be calculated |
| 305 |
// on a field that has multiple values. |
| 306 |
// Valid modes are: min, max, avg, and sum. |
| 307 |
func (fn *LinearDecayFunction) GetMultiValueMode() string { |
| 308 |
return fn.multiValueMode |
| 309 |
} |
| 310 |
|
| 311 |
// Source returns the serializable JSON data of this score function. |
| 312 |
func (fn *LinearDecayFunction) Source() (interface{}, error) { |
| 313 |
source := make(map[string]interface{}) |
| 314 |
params := make(map[string]interface{}) |
| 315 |
source[fn.fieldName] = params |
| 316 |
if fn.origin != nil { |
| 317 |
params["origin"] = fn.origin |
| 318 |
} |
| 319 |
params["scale"] = fn.scale |
| 320 |
if fn.decay != nil && *fn.decay > 0 { |
| 321 |
params["decay"] = *fn.decay |
| 322 |
} |
| 323 |
if fn.offset != nil { |
| 324 |
params["offset"] = fn.offset |
| 325 |
} |
| 326 |
if fn.multiValueMode != "" { |
| 327 |
source["multi_value_mode"] = fn.multiValueMode |
| 328 |
} |
| 329 |
// Notice that the weight has to be serialized in FunctionScoreQuery. |
| 330 |
return source, nil |
| 331 |
} |
| 332 |
|
| 333 |
// -- Script -- |
| 334 |
|
| 335 |
// ScriptFunction builds a script score function. It uses a script to |
| 336 |
// compute or influence the score of documents that match with the inner |
| 337 |
// query or filter. |
| 338 |
// |
| 339 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-function-score-query.html#_script_score |
| 340 |
// for details. |
| 341 |
type ScriptFunction struct { |
| 342 |
script *Script |
| 343 |
weight *float64 |
| 344 |
} |
| 345 |
|
| 346 |
// NewScriptFunction initializes and returns a new ScriptFunction. |
| 347 |
func NewScriptFunction(script *Script) *ScriptFunction { |
| 348 |
return &ScriptFunction{ |
| 349 |
script: script, |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
// Name represents the JSON field name under which the output of Source |
| 354 |
// needs to be serialized by FunctionScoreQuery (see FunctionScoreQuery.Source). |
| 355 |
func (fn *ScriptFunction) Name() string { |
| 356 |
return "script_score" |
| 357 |
} |
| 358 |
|
| 359 |
// Script specifies the script to be executed. |
| 360 |
func (fn *ScriptFunction) Script(script *Script) *ScriptFunction { |
| 361 |
fn.script = script |
| 362 |
return fn |
| 363 |
} |
| 364 |
|
| 365 |
// Weight adjusts the score of the score function. |
| 366 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-function-score-query.html#_using_function_score |
| 367 |
// for details. |
| 368 |
func (fn *ScriptFunction) Weight(weight float64) *ScriptFunction { |
| 369 |
fn.weight = &weight |
| 370 |
return fn |
| 371 |
} |
| 372 |
|
| 373 |
// GetWeight returns the adjusted score. It is part of the ScoreFunction interface. |
| 374 |
// Returns nil if weight is not specified. |
| 375 |
func (fn *ScriptFunction) GetWeight() *float64 { |
| 376 |
return fn.weight |
| 377 |
} |
| 378 |
|
| 379 |
// Source returns the serializable JSON data of this score function. |
| 380 |
func (fn *ScriptFunction) Source() (interface{}, error) { |
| 381 |
source := make(map[string]interface{}) |
| 382 |
if fn.script != nil { |
| 383 |
src, err := fn.script.Source() |
| 384 |
if err != nil { |
| 385 |
return nil, err |
| 386 |
} |
| 387 |
source["script"] = src |
| 388 |
} |
| 389 |
// Notice that the weight has to be serialized in FunctionScoreQuery. |
| 390 |
return source, nil |
| 391 |
} |
| 392 |
|
| 393 |
// -- Field value factor -- |
| 394 |
|
| 395 |
// FieldValueFactorFunction is a function score function that allows you |
| 396 |
// to use a field from a document to influence the score. |
| 397 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-function-score-query.html#_field_value_factor. |
| 398 |
type FieldValueFactorFunction struct { |
| 399 |
field string |
| 400 |
factor *float64 |
| 401 |
missing *float64 |
| 402 |
weight *float64 |
| 403 |
modifier string |
| 404 |
} |
| 405 |
|
| 406 |
// NewFieldValueFactorFunction initializes and returns a new FieldValueFactorFunction. |
| 407 |
func NewFieldValueFactorFunction() *FieldValueFactorFunction { |
| 408 |
return &FieldValueFactorFunction{} |
| 409 |
} |
| 410 |
|
| 411 |
// Name represents the JSON field name under which the output of Source |
| 412 |
// needs to be serialized by FunctionScoreQuery (see FunctionScoreQuery.Source). |
| 413 |
func (fn *FieldValueFactorFunction) Name() string { |
| 414 |
return "field_value_factor" |
| 415 |
} |
| 416 |
|
| 417 |
// Field is the field to be extracted from the document. |
| 418 |
func (fn *FieldValueFactorFunction) Field(field string) *FieldValueFactorFunction { |
| 419 |
fn.field = field |
| 420 |
return fn |
| 421 |
} |
| 422 |
|
| 423 |
// Factor is the (optional) factor to multiply the field with. If you do not |
| 424 |
// specify a factor, the default is 1. |
| 425 |
func (fn *FieldValueFactorFunction) Factor(factor float64) *FieldValueFactorFunction { |
| 426 |
fn.factor = &factor |
| 427 |
return fn |
| 428 |
} |
| 429 |
|
| 430 |
// Modifier to apply to the field value. It can be one of: none, log, log1p, |
| 431 |
// log2p, ln, ln1p, ln2p, square, sqrt, or reciprocal. Defaults to: none. |
| 432 |
func (fn *FieldValueFactorFunction) Modifier(modifier string) *FieldValueFactorFunction { |
| 433 |
fn.modifier = modifier |
| 434 |
return fn |
| 435 |
} |
| 436 |
|
| 437 |
// Weight adjusts the score of the score function. |
| 438 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-function-score-query.html#_using_function_score |
| 439 |
// for details. |
| 440 |
func (fn *FieldValueFactorFunction) Weight(weight float64) *FieldValueFactorFunction { |
| 441 |
fn.weight = &weight |
| 442 |
return fn |
| 443 |
} |
| 444 |
|
| 445 |
// GetWeight returns the adjusted score. It is part of the ScoreFunction interface. |
| 446 |
// Returns nil if weight is not specified. |
| 447 |
func (fn *FieldValueFactorFunction) GetWeight() *float64 { |
| 448 |
return fn.weight |
| 449 |
} |
| 450 |
|
| 451 |
// Missing is used if a document does not have that field. |
| 452 |
func (fn *FieldValueFactorFunction) Missing(missing float64) *FieldValueFactorFunction { |
| 453 |
fn.missing = &missing |
| 454 |
return fn |
| 455 |
} |
| 456 |
|
| 457 |
// Source returns the serializable JSON data of this score function. |
| 458 |
func (fn *FieldValueFactorFunction) Source() (interface{}, error) { |
| 459 |
source := make(map[string]interface{}) |
| 460 |
if fn.field != "" { |
| 461 |
source["field"] = fn.field |
| 462 |
} |
| 463 |
if fn.factor != nil { |
| 464 |
source["factor"] = *fn.factor |
| 465 |
} |
| 466 |
if fn.missing != nil { |
| 467 |
source["missing"] = *fn.missing |
| 468 |
} |
| 469 |
if fn.modifier != "" { |
| 470 |
source["modifier"] = strings.ToLower(fn.modifier) |
| 471 |
} |
| 472 |
// Notice that the weight has to be serialized in FunctionScoreQuery. |
| 473 |
return source, nil |
| 474 |
} |
| 475 |
|
| 476 |
// -- Weight Factor -- |
| 477 |
|
| 478 |
// WeightFactorFunction builds a weight factor function that multiplies |
| 479 |
// the weight to the score. |
| 480 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-function-score-query.html#_weight |
| 481 |
// for details. |
| 482 |
type WeightFactorFunction struct { |
| 483 |
weight float64 |
| 484 |
} |
| 485 |
|
| 486 |
// NewWeightFactorFunction initializes and returns a new WeightFactorFunction. |
| 487 |
func NewWeightFactorFunction(weight float64) *WeightFactorFunction { |
| 488 |
return &WeightFactorFunction{weight: weight} |
| 489 |
} |
| 490 |
|
| 491 |
// Name represents the JSON field name under which the output of Source |
| 492 |
// needs to be serialized by FunctionScoreQuery (see FunctionScoreQuery.Source). |
| 493 |
func (fn *WeightFactorFunction) Name() string { |
| 494 |
return "weight" |
| 495 |
} |
| 496 |
|
| 497 |
// Weight adjusts the score of the score function. |
| 498 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-function-score-query.html#_using_function_score |
| 499 |
// for details. |
| 500 |
func (fn *WeightFactorFunction) Weight(weight float64) *WeightFactorFunction { |
| 501 |
fn.weight = weight |
| 502 |
return fn |
| 503 |
} |
| 504 |
|
| 505 |
// GetWeight returns the adjusted score. It is part of the ScoreFunction interface. |
| 506 |
// Returns nil if weight is not specified. |
| 507 |
func (fn *WeightFactorFunction) GetWeight() *float64 { |
| 508 |
return &fn.weight |
| 509 |
} |
| 510 |
|
| 511 |
// Source returns the serializable JSON data of this score function. |
| 512 |
func (fn *WeightFactorFunction) Source() (interface{}, error) { |
| 513 |
// Notice that the weight has to be serialized in FunctionScoreQuery. |
| 514 |
return fn.weight, nil |
| 515 |
} |
| 516 |
|
| 517 |
// -- Random -- |
| 518 |
|
| 519 |
// RandomFunction builds a random score function. |
| 520 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-function-score-query.html#_random |
| 521 |
// for details. |
| 522 |
type RandomFunction struct { |
| 523 |
seed interface{} |
| 524 |
weight *float64 |
| 525 |
} |
| 526 |
|
| 527 |
// NewRandomFunction initializes and returns a new RandomFunction. |
| 528 |
func NewRandomFunction() *RandomFunction { |
| 529 |
return &RandomFunction{} |
| 530 |
} |
| 531 |
|
| 532 |
// Name represents the JSON field name under which the output of Source |
| 533 |
// needs to be serialized by FunctionScoreQuery (see FunctionScoreQuery.Source). |
| 534 |
func (fn *RandomFunction) Name() string { |
| 535 |
return "random_score" |
| 536 |
} |
| 537 |
|
| 538 |
// Seed is documented in 1.6 as a numeric value. However, in the source code |
| 539 |
// of the Java client, it also accepts strings. So we accept both here, too. |
| 540 |
func (fn *RandomFunction) Seed(seed interface{}) *RandomFunction { |
| 541 |
fn.seed = seed |
| 542 |
return fn |
| 543 |
} |
| 544 |
|
| 545 |
// Weight adjusts the score of the score function. |
| 546 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-function-score-query.html#_using_function_score |
| 547 |
// for details. |
| 548 |
func (fn *RandomFunction) Weight(weight float64) *RandomFunction { |
| 549 |
fn.weight = &weight |
| 550 |
return fn |
| 551 |
} |
| 552 |
|
| 553 |
// GetWeight returns the adjusted score. It is part of the ScoreFunction interface. |
| 554 |
// Returns nil if weight is not specified. |
| 555 |
func (fn *RandomFunction) GetWeight() *float64 { |
| 556 |
return fn.weight |
| 557 |
} |
| 558 |
|
| 559 |
// Source returns the serializable JSON data of this score function. |
| 560 |
func (fn *RandomFunction) Source() (interface{}, error) { |
| 561 |
source := make(map[string]interface{}) |
| 562 |
if fn.seed != nil { |
| 563 |
source["seed"] = fn.seed |
| 564 |
} |
| 565 |
// Notice that the weight has to be serialized in FunctionScoreQuery. |
| 566 |
return source, nil |
| 567 |
} |