File: function-score-query.asciidoc

package info (click to toggle)
elasticsearch 1.6.2%2Bdfsg-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 59,348 kB
  • sloc: java: 461,436; xml: 1,913; python: 1,402; sh: 1,183; ruby: 618; perl: 172; makefile: 46
file content (626 lines) | stat: -rw-r--r-- 19,512 bytes parent folder | download
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
[[query-dsl-function-score-query]]
=== Function Score Query

The `function_score` allows you to modify the score of documents that are
retrieved by a query. This can be useful if, for example, a score
function is computationally expensive and it is sufficient to compute
the score on a filtered set of documents.

`function_score` provides the same functionality that
`custom_boost_factor`, `custom_score` and
`custom_filters_score` provided
but with additional capabilities such as distance and recency scoring (see description below).

==== Using function score

To use `function_score`, the user has to define a query and one or
several functions, that compute a new score for each document returned
by the query.

`function_score` can be used with only one function like this:

[source,js]
--------------------------------------------------
"function_score": {
    "(query|filter)": {},
    "boost": "boost for the whole query",
    "FUNCTION": {},  <1>
    "boost_mode":"(multiply|replace|...)"
}
--------------------------------------------------
<1> See <<score-functions>> for a list of supported functions.

Furthermore, several functions can be combined. In this case one can
optionally choose to apply the function only if a document matches a
given filter:

[source,js]
--------------------------------------------------
"function_score": {
    "(query|filter)": {},
    "boost": "boost for the whole query",
    "functions": [
        {
            "filter": {},
            "FUNCTION": {}, <1>
            "weight": number
        },
        {
            "FUNCTION": {} <1>
        },
        {
            "filter": {},
            "weight": number
        }
    ],
    "max_boost": number,
    "score_mode": "(multiply|max|...)",
    "boost_mode": "(multiply|replace|...)",
    "min_score" : number
}
--------------------------------------------------
<1> See <<score-functions>> for a list of supported functions.

If no filter is given with a function this is equivalent to specifying
`"match_all": {}`

First, each document is scored by the defined functions. The parameter
`score_mode` specifies how the computed scores are combined:

[horizontal]
`multiply`::    scores are multiplied (default)
`sum`::         scores are summed
`avg`::         scores are averaged
`first`::       the first function that has a matching filter
                is applied
`max`::         maximum score is used
`min`::         minimum score is used

Because scores can be on different scales (for example, between 0 and 1 for decay functions but arbitrary for `field_value_factor`) and also because sometimes a different impact of functions on the score is desirable, the score of each function can be adjusted with a user defined `weight` (). The `weight` can be defined per function in the `functions` array (example above) and is multiplied with the score computed by the respective function.
If weight is given without any other function declaration, `weight` acts as a function that simply returns the `weight`.

The new score can be restricted to not exceed a certain limit by setting
the `max_boost` parameter. The default for `max_boost` is FLT_MAX.

The newly computed score is combined with the score of the
query. The parameter `boost_mode` defines how:

[horizontal]
`multiply`::    query score and function score is multiplied (default)
`replace`::     only function score is used, the query score is ignored
`sum`::         query score and function score are added
`avg`::         average
`max`::         max of query score and function score
`min`::         min of query score and function score

By default, modifying the score does not change which documents match. To exclude
documents that do not meet a certain score threshold the `min_score` parameter can be set to the desired score threshold.

[[score-functions]]
==== Score functions

The `function_score` query provides several types of score functions:

* <<function-script-score,`script_score`>>
* <<function-weight,`weight`>>
* <<function-random,`random_score`>>
* <<function-field-value-factor,`field_value_factor`>>
* <<function-decay,decay functions>>: `gauss`, `linear`, `exp`

[[function-script-score]]
==== Script score

The `script_score` function allows you to wrap another query and customize
the scoring of it optionally with a computation derived from other numeric
field values in the doc using a script expression. Here is a
simple sample:

[source,js]
--------------------------------------------------
"script_score" : {
    "script" : "_score * doc['my_numeric_field'].value"
}
--------------------------------------------------

On top of the different scripting field values and expression, the
`_score` script parameter can be used to retrieve the score based on the
wrapped query.

Scripts are cached for faster execution. If the script has parameters
that it needs to take into account, it is preferable to reuse the same
script, and provide parameters to it:

[source,js]
--------------------------------------------------
"script_score": {
    "lang": "lang",
    "params": {
        "param1": value1,
        "param2": value2
     },
    "script": "_score * doc['my_numeric_field'].value / pow(param1, param2)"
}
--------------------------------------------------

Note that unlike the `custom_score` query, the
score of the query is multiplied with the result of the script scoring. If
you wish to inhibit this, set `"boost_mode": "replace"`

[[function-weight]]
==== Weight

The `weight` score allows you to multiply the score by the provided
`weight`. This can sometimes be desired since boost value set on
specific queries gets normalized, while for this score function it does
not.

[source,js]
--------------------------------------------------
"weight" : number
--------------------------------------------------

[[function-random]]
===== Random

The `random_score` generates scores using a hash of the `_uid` field,
with a `seed` for variation. If `seed` is not specified, the current
time is used.

NOTE: Using this feature will load field data for `_uid`, which can
be a memory intensive operation since the values are unique.

[source,js]
--------------------------------------------------
"random_score": {
    "seed" : number
}
--------------------------------------------------

[[function-field-value-factor]]
==== Field Value factor

The `field_value_factor` function allows you to use a field from a document to
influence the score. It's similar to using the `script_score` function, however,
it avoids the overhead of scripting. If used on a multi-valued field, only the
first value of the field is used in calculations.

As an example, imagine you have a document indexed with a numeric `popularity`
field and wish to influence the score of a document with this field, an example
doing so would look like:

[source,js]
--------------------------------------------------
"field_value_factor": {
  "field": "popularity",
  "factor": 1.2,
  "modifier": "sqrt",
  "missing": 1
}
--------------------------------------------------

Which will translate into the following formula for scoring:

`sqrt(1.2 * doc['popularity'].value)`

There are a number of options for the `field_value_factor` function:

[horizontal]
`field`::

    Field to be extracted from the document.

`factor`::

    Optional factor to multiply the field value with, defaults to `1`.

`modifier`::

    Modifier to apply to the field value, can be one of: `none`, `log`,
    `log1p`, `log2p`, `ln`, `ln1p`, `ln2p`, `square`, `sqrt`, or `reciprocal`.
    Defaults to `none`.

`missing`::

    Value used if the document doesn't have that field. The modifier
    and factor are still applied to it as though it were read from the document.


Keep in mind that taking the log() of 0, or the square root of a negative number
is an illegal operation, and an exception will be thrown. Be sure to limit the
values of the field with a range filter to avoid this, or use `log1p` and
`ln1p`.

[[function-decay]]
==== Decay functions

Decay functions score a document with a function that decays depending
on the distance of a numeric field value of the document from a user
given origin. This is similar to a range query, but with smooth edges
instead of boxes.

To use distance scoring on a query that has numerical fields, the user
has to define an `origin` and a `scale` for each field. The `origin`
is needed to define the ``central point'' from which the distance
is calculated, and the `scale` to define the rate of decay. The
decay function is specified as

[source,js]
--------------------------------------------------
"DECAY_FUNCTION": { <1>
    "FIELD_NAME": { <2>
          "origin": "11, 12",
          "scale": "2km",
          "offset": "0km",
          "decay": 0.33
    }
}
--------------------------------------------------
<1> The `DECAY_FUNCTION` should be one of `linear`, `exp`, or `gauss`.
<2> The specified field must be a numeric field.

In the above example, the field is a <<mapping-geo-point-type>> and origin can be provided in geo format. `scale` and `offset` must be given with a unit in this case. If your field is a date field, you can set `scale` and `offset` as days, weeks, and so on. Example:

[source,js]
--------------------------------------------------
    "gauss": {
        "date": {
              "origin": "2013-09-17", <1>
              "scale": "10d",
              "offset": "5d", <2>
              "decay" : 0.5 <2>
        }
    }
--------------------------------------------------
<1> The date format of the origin depends on the <<mapping-date-format>> defined in
    your mapping. If you do not define the origin, the current time is used.
<2> The `offset` and `decay` parameters are optional.

[horizontal]
`offset`::
    If an `offset` is defined, the decay function will only compute the
    decay function for documents with a distance greater that the defined
    `offset`. The default is 0.

`decay`::
    The `decay` parameter defines how documents are scored at the distance
    given at `scale`. If no `decay` is defined, documents at the distance
    `scale` will be scored 0.5.

In the first example, your documents might represents hotels and contain a geo
location field. You want to compute a decay function depending on how
far the hotel is from a given location. You might not immediately see
what scale to choose for the gauss function, but you can say something
like: "At a distance of 2km from the desired location, the score should
be reduced by one third."
The parameter "scale" will then be adjusted automatically to assure that
the score function computes a score of 0.5 for hotels that are 2km away
from the desired location.


In the second example, documents with a field value between 2013-09-12 and 2013-09-22 would get a weight of 1.0 and documents which are 15 days from that date a weight of 0.5.

===== Supported decay functions

The `DECAY_FUNCTION` determines the shape of the decay:

`gauss`::
+
--
Normal decay, computed as:

image:images/Gaussian.png[]

where image:images/sigma.png[] is computed to assure that the score takes the value `decay` at distance `scale` from `origin`+-`offset`

image:images/sigma_calc.png[]

See <<gauss-decay>> for graphs demonstrating the curve generated by the `gauss` function.

--

`exp`::
+
--
Exponential decay, computed as:

image:images/Exponential.png[]

where again the parameter image:images/lambda.png[] is computed to assure that the score takes the value `decay` at distance `scale` from `origin`+-`offset`

image:images/lambda_calc.png[]

See <<exp-decay>> for graphs demonstrating the curve generated by the `exp` function.

--

`linear`::
+
--
Linear decay, computed as:

image:images/Linear.png[].


where again the parameter `s` is computed to assure that the score takes the value `decay` at distance `scale` from `origin`+-`offset`

image:images/s_calc.png[]

In contrast to the normal and exponential decay, this function actually
sets the score to 0 if the field value exceeds twice the user given
scale value.

See <<linear-decay>> for graphs demonstrating the curve generated by the `linear` function.
--

===== Multi-value fields:

If a field used for computing the decay contains multiple values, per default the value closest to the origin is chosen for determining the distance.
This can be changed by setting `multi_value_mode`.

[horizontal]
`min`:: Distance is the minimum distance
`max`:: Distance is the maximum distance
`avg`:: Distance is the average distance
`sum`:: Distance is the sum of all distances

Example:

[source,js]
--------------------------------------------------
    "DECAY_FUNCTION": {
        "FIELD_NAME": {
              "origin": ...,
              "scale": ...
        },
        "multi_value_mode": "avg"
    }
--------------------------------------------------



==== Detailed example

Suppose you are searching for a hotel in a certain town. Your budget is
limited. Also, you would like the hotel to be close to the town center,
so the farther the hotel is from the desired location the less likely
you are to check in.

You would like the query results that match your criterion (for
example, "hotel, Nancy, non-smoker") to be scored with respect to
distance to the town center and also the price.

Intuitively, you would like to define the town center as the origin and
maybe you are willing to walk 2km to the town center from the hotel. +
In this case your *origin* for the location field is the town center
and the *scale* is ~2km.

If your budget is low, you would probably prefer something cheap above
something expensive.  For the price field, the *origin* would be 0 Euros
and the *scale* depends on how much you are willing to pay, for example 20 Euros.

In this example, the fields might be called "price" for the price of the
hotel and "location" for the coordinates of this hotel.

The function for `price` in this case would be

[source,js]
--------------------------------------------------
"gauss": { <1>
    "price": {
          "origin": "0",
          "scale": "20"
    }
}
--------------------------------------------------
<1> The decay function could also be `linear` or `exp`.

and for `location`:

[source,js]
--------------------------------------------------

"gauss": { <1>
    "location": {
          "origin": "11, 12",
          "scale": "2km"
    }
}
--------------------------------------------------
<1> The decay function could also be `linear` or `exp`.


Suppose you want to multiply these two functions on the original score,
the request would look like this:

[source,js]
--------------------------------------------------
GET /hotels/_search/
{
  "query": {
    "function_score": {
      "functions": [
        {
          "gauss": {
            "price": {
              "origin": "0",
              "scale": "20"
            }
          }
        },
        {
          "gauss": {
            "location": {
              "origin": "11, 12",
              "scale": "2km"
            }
          }
        }
      ],
      "query": {
        "match": {
          "properties": "balcony"
        }
      },
      "score_mode": "multiply"
    }
  }
}
--------------------------------------------------
// AUTOSENSE

Next, we show how the computed score looks like for each of the three
possible decay functions.

[[gauss-decay]]
===== Normal decay, keyword `gauss`

When choosing `gauss` as the decay function in the above example, the
contour and surface plot of the multiplier looks like this:

image::https://f.cloud.github.com/assets/4320215/768157/cd0e18a6-e898-11e2-9b3c-f0145078bd6f.png[width="700px"]

image::https://f.cloud.github.com/assets/4320215/768160/ec43c928-e898-11e2-8e0d-f3c4519dbd89.png[width="700px"]

Suppose your original search results matches three hotels :

* "Backback Nap"
* "Drink n Drive"
* "BnB Bellevue".

"Drink n Drive" is pretty far from your defined location (nearly 2 km)
and is not too cheap (about 13 Euros) so it gets a low factor a factor
of 0.56. "BnB Bellevue" and "Backback Nap" are both pretty close to the
defined location but "BnB Bellevue" is cheaper, so it gets a multiplier
of 0.86 whereas "Backpack Nap" gets a value of 0.66.

[[exp-decay]]
===== Exponential decay, keyword `exp`

When choosing `exp` as the decay function in the above example, the
contour and surface plot of the multiplier looks like this:

image::https://f.cloud.github.com/assets/4320215/768161/082975c0-e899-11e2-86f7-174c3a729d64.png[width="700px"]

image::https://f.cloud.github.com/assets/4320215/768162/0b606884-e899-11e2-907b-aefc77eefef6.png[width="700px"]

[[linear-decay]]
===== Linear decay, keyword `linear`

When choosing `linear` as the decay function in the above example, the
contour and surface plot of the multiplier looks like this:

image::https://f.cloud.github.com/assets/4320215/768164/1775b0ca-e899-11e2-9f4a-776b406305c6.png[width="700px"]

image::https://f.cloud.github.com/assets/4320215/768165/19d8b1aa-e899-11e2-91bc-6b0553e8d722.png[width="700px"]

==== Supported fields for decay functions

Only numeric, date, and geo-point fields are supported.

==== What if a field is missing?

If the numeric field is missing in the document, the function will
return 1.

==== Relation to `custom_boost`, `custom_score` and `custom_filters_score`

The `custom_boost_factor` query

[source,js]
--------------------------------------------------
"custom_boost_factor": {
    "boost_factor": 5.2,
    "query": {...}
}
--------------------------------------------------

becomes

[source,js]
--------------------------------------------------
"function_score": {
    "weight": 5.2,
    "query": {...}
}
--------------------------------------------------

The `custom_score` query

[source,js]
--------------------------------------------------
"custom_score": {
    "params": {
        "param1": 2,
        "param2": 3.1
    },
    "query": {...},
    "script": "_score * doc['my_numeric_field'].value / pow(param1, param2)"
}
--------------------------------------------------

becomes

[source,js]
--------------------------------------------------
"function_score": {
    "boost_mode": "replace",
    "query": {...},
    "script_score": {
        "params": {
            "param1": 2,
            "param2": 3.1
        },
        "script": "_score * doc['my_numeric_field'].value / pow(param1, param2)"
    }
}
--------------------------------------------------

and the `custom_filters_score`

[source,js]
--------------------------------------------------
"custom_filters_score": {
    "filters": [
        {
            "boost": "3",
            "filter": {...}
        },
        {
            "filter": {...},
            "script": "_score * doc['my_numeric_field'].value / pow(param1, param2)"
        }
    ],
    "params": {
        "param1": 2,
        "param2": 3.1
    },
    "query": {...},
    "score_mode": "first"
}
--------------------------------------------------

becomes:

[source,js]
--------------------------------------------------
"function_score": {
    "functions": [
        {
            "weight": "3",
            "filter": {...}
        },
        {
            "filter": {...},
            "script_score": {
                "params": {
                    "param1": 2,
                    "param2": 3.1
                },
                "script": "_score * doc['my_numeric_field'].value / pow(param1, param2)"
            }
        }
    ],
    "query": {...},
    "score_mode": "first"
}
--------------------------------------------------