Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document '_name' field in 'function_score' query's function definition #7340

Merged
merged 9 commits into from
Jul 9, 2024
197 changes: 196 additions & 1 deletion _query-dsl/compound/function-score.md
Original file line number Diff line number Diff line change
Expand Up @@ -826,4 +826,199 @@ The results contain the three matching blog posts:
}
}
```
</details>
</details>

## Named functions

lrynek marked this conversation as resolved.
Show resolved Hide resolved
You can specify `_name` parameter for every function top level definition. This parameter is used to identify the function in the response. The name is useful for debugging and understanding the scoring process. Once specified, it is returned back as a part of the explanation whenever it dims possible (it applies to functions, filters and queries).

### Example

lrynek marked this conversation as resolved.
Show resolved Hide resolved
The following request sets `explain: true` for debugging purpose to retrieve the explanation of the scoring process in the response. For the ease of identifying unambiguously the functions, the `_name` parameter is set for each function:

```json
GET blogs/_search
{
"explain": true,
"size": 1,
"query": {
"function_score": {
"functions": [
{
"_name": "likes_function",
"script_score": {
"script": {
"lang": "painless",
"source": "return doc['likes'].value * 2;"
}
},
"weight": 0.6
},
{
"_name": "views_function",
"field_value_factor": {
"field": "views",
"factor": 1.5,
"modifier": "log1p",
"missing": 1
},
"weight": 0.3
},
{
"_name": "comments_function",
"gauss": {
"comments": {
"origin": 1000,
"scale": 800
}
},
"weight": 0.1
}
]
}
}
}
```
{% include copy-curl.html %}

lrynek marked this conversation as resolved.
Show resolved Hide resolved
The response will contain the explanation of the scoring process with the names of the functions in it:

<details open markdown="block">
<summary>
Response
</summary>
{: .text-delta}

```json
{
"took": 14,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 6.1600614,
"hits": [
{
"_shard": "[blogs][0]",
"_node": "_yndTaZHQWimcDgAfOfRtQ",
"_index": "blogs",
"_id": "1",
"_score": 6.1600614,
"_source": {
"name": "Semantic search in OpenSearch",
"views": 1200,
"likes": 150,
"comments": 16,
"date_posted": "2022-04-17"
},
"_explanation": {
"value": 6.1600614,
"description": "function score, product of:",
"details": [
{
"value": 1,
"description": "*:*",
"details": []
},
{
"value": 6.1600614,
"description": "min of:",
"details": [
{
"value": 6.1600614,
"description": "function score, score mode [multiply]",
"details": [
{
"value": 180,
"description": "product of:",
"details": [
{
"value": 300,
"description": "script score function(_name: likes_function), computed with script:\"Script{type=inline, lang='painless', idOrCode='return doc['likes'].value * 2;', options={}, params={}}\"",
"details": [
{
"value": 1,
"description": "_score: ",
"details": [
{
"value": 1,
"description": "*:*",
"details": []
}
]
}
]
},
{
"value": 0.6,
"description": "weight",
"details": []
}
]
},
{
"value": 0.9766541,
"description": "product of:",
"details": [
{
"value": 3.2555137,
"description": "field value function(_name: views_function): log1p(doc['views'].value?:1.0 * factor=1.5)",
"details": []
},
{
"value": 0.3,
"description": "weight",
"details": []
}
]
},
{
"value": 0.035040613,
"description": "product of:",
"details": [
{
"value": 0.35040614,
"description": "Function for field comments:",
"details": [
{
"value": 0.35040614,
"description": "exp(-0.5*pow(MIN[Math.max(Math.abs(16.0(=doc value) - 1000.0(=origin))) - 0.0(=offset), 0)],2.0)/461662.4130844683, _name: comments_function)",
"details": []
}
]
},
{
"value": 0.1,
"description": "weight",
"details": []
}
]
}
]
},
{
"value": 3.4028235e+38,
"description": "maxBoost",
"details": []
}
]
}
]
}
}
]
}
}
```
</details>
lrynek marked this conversation as resolved.
Show resolved Hide resolved
{% include copy-curl.html %}

lrynek marked this conversation as resolved.
Show resolved Hide resolved
lrynek marked this conversation as resolved.
Show resolved Hide resolved
As you can see, the explanation contains the name in the respective function's description.
Loading