-
Aivaras Gotovskis authoredAivaras Gotovskis authored
Code owners
FunctionScore.md 3.29 KiB
Function Score Query
More info about function score query is in the official elasticsearch docs
The function score query 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.
Example with linear decay function
{
"query": {
"function_score": {
"query": {
"range": {
"price": {
"gte": 10,
"lte": 100
}
}
},
"functions": [
{
"linear": {
"price": {
"origin": 10,
"scale": 50,
"offset": 0,
"decay" : 0.5
}
}
}
]
}
}
}
In DSL
$rangeQuery = new RangeQuery('price');
$rangeQuery->addParameter(RangeQuery::GTE, 10);
$rangeQuery->addParameter(RangeQuery::LTE, 100);
$functionScoreQuery = new FunctionScoreQuery($rangeQuery);
$functionScoreQuery->addDecayFunction(
'linear',
'price',
[
'origin' => 10,
'scale' => 50,
'offset' => 0,
'decay' => 0.5
]
);
$search = new Search();
$search->addQuery($functionScoreQuery);
$queryArray = $search->toArray();
Example weight function to multiply score of subset
{
"query": {
"function_score": {
"query": {
"match_all": {}
},
"functions": [
{
"weight": 2,
"filter": {
"range": {
"price": {
"gte": 10,
"lte": 40
}
}
}
}
]
}
}
}
In DSL: