|
@@ -1,15 +1,13 @@
|
|
|
[[query-dsl-function-score-query]]
|
|
|
-== 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.
|
|
|
|
|
|
-=== 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
|
|
|
+more functions, that compute a new score for each document returned
|
|
|
by the query.
|
|
|
|
|
|
`function_score` can be used with only one function like this:
|
|
@@ -89,13 +87,13 @@ query. The parameter `boost_mode` defines how:
|
|
|
`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.
|
|
|
+documents that do not meet a certain score threshold the `min_score` parameter can be set to the desired score threshold.
|
|
|
|
|
|
==== Score functions
|
|
|
|
|
|
The `function_score` query provides several types of score functions.
|
|
|
|
|
|
-===== 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
|
|
@@ -135,7 +133,7 @@ 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"`
|
|
|
|
|
|
-===== 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
|
|
@@ -147,7 +145,7 @@ not.
|
|
|
"weight" : number
|
|
|
--------------------------------------------------
|
|
|
|
|
|
-===== 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
|
|
@@ -163,7 +161,7 @@ be a memory intensive operation since the values are unique.
|
|
|
}
|
|
|
--------------------------------------------------
|
|
|
|
|
|
-===== 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,
|
|
@@ -207,7 +205,7 @@ 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`.
|
|
|
|
|
|
-===== Decay functions
|
|
|
+====== 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
|
|
@@ -254,13 +252,13 @@ The `offset` and `decay` parameters are optional.
|
|
|
|
|
|
[horizontal]
|
|
|
`origin`::
|
|
|
- The point of origin used for calculating distance. Must be given as a
|
|
|
- number for numeric field, date for date fields and geo point for geo fields.
|
|
|
+ The point of origin used for calculating distance. Must be given as a
|
|
|
+ number for numeric field, date for date fields and geo point for geo fields.
|
|
|
Required for geo and numeric field. For date fields the default is `now`. Date
|
|
|
math (for example `now-1h`) is supported for origin.
|
|
|
|
|
|
`scale`::
|
|
|
- Required for all types. Defines the distance from origin at which the computed
|
|
|
+ Required for all types. Defines the distance from origin at which the computed
|
|
|
score will equal `decay` parameter. For geo fields: Can be defined as number+unit (1km, 12m,...).
|
|
|
Default unit is meters. For date fields: Can to be defined as a number+unit ("1h", "10d",...).
|
|
|
Default unit is milliseconds. For numeric field: Any number.
|
|
@@ -334,7 +332,7 @@ For single functions the three decay functions together with their parameters ca
|
|
|
|
|
|
image:images/decay_2d.png[width=600]
|
|
|
|
|
|
-===== Multiple values:
|
|
|
+====== Multiple values:
|
|
|
|
|
|
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`.
|
|
@@ -360,7 +358,7 @@ Example:
|
|
|
|
|
|
|
|
|
|
|
|
-==== Detailed example
|
|
|
+===== 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,
|
|
@@ -450,7 +448,7 @@ curl 'localhost:9200/hotels/_search/' -d '{
|
|
|
Next, we show how the computed score looks like for each of the three
|
|
|
possible decay functions.
|
|
|
|
|
|
-===== Normal decay, keyword `gauss`
|
|
|
+====== 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:
|
|
@@ -471,7 +469,7 @@ 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.
|
|
|
|
|
|
-===== Exponential decay, keyword `exp`
|
|
|
+====== 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:
|
|
@@ -480,7 +478,7 @@ image::https://f.cloud.github.com/assets/4320215/768161/082975c0-e899-11e2-86f7-
|
|
|
|
|
|
image::https://f.cloud.github.com/assets/4320215/768162/0b606884-e899-11e2-907b-aefc77eefef6.png[width="700px"]
|
|
|
|
|
|
-===== Linear' decay, keyword `linear`
|
|
|
+====== 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:
|
|
@@ -489,12 +487,12 @@ image::https://f.cloud.github.com/assets/4320215/768164/1775b0ca-e899-11e2-9f4a-
|
|
|
|
|
|
image::https://f.cloud.github.com/assets/4320215/768165/19d8b1aa-e899-11e2-91bc-6b0553e8d722.png[width="700px"]
|
|
|
|
|
|
-==== Supported fields for decay functions
|
|
|
+===== Supported fields for decay functions
|
|
|
|
|
|
Only single valued numeric fields, including time and geo locations,
|
|
|
are supported.
|
|
|
|
|
|
-==== What if a field is missing?
|
|
|
+===== What if a field is missing?
|
|
|
|
|
|
If the numeric field is missing in the document, the function will
|
|
|
return 1.
|