I already asked this on stackoverflow but since this is an urgent question and I didn’t find answers yet in stackoverflow I posted it also in this fourm hoping there’s somebody here that can guide me how to fix this behavior THANK YOU!!
In OpenSearch (v2.18.0) I implemented a custom script_score
using Painless scripting language. When I only use query.bool.should
it is called once per document and the returned _score
is correct
However, when I combine both query.bool.should
and query.bool.must
in my query, the script_score
is called twice or three times per document, and the resulting score is the sum of all the calls. This causes the score to be higher than intended.
Why does this happen? and how can I ensure it is called only once per document when using both should
and must
in query
? Or at least prevent OpenSearch from summing the results of all calls per document and only return the result of one of these calls?
E.g. see below query (which I simplified it here so the example is easy to understand) you’ll see the script_source
source is return Integer.parseInt(doc['_id'].value);
however because I used both should
and must
in my query the calculated _score
for document 6148
is 18444
(i.e. 6148 * 3
) instead of 6148
{
"from": 0,
"size": 10,
"stored_fields": "_none_",
"docvalue_fields": [
"_id",
"_score"
],
"sort": [
{
"_score": {
"order": "asc"
}
}
],
"query": {
"function_score": {
"query": {
"bool": {
"must": [
{
"term": { "category_ids": "2" }
},
{
"terms": { "visibility": ["3", "4"] }
}
],
"should": [
{
"ids": {
"values": [
"6148"
]
}
}
],
"minimum_should_match": 1
}
},
"script_score": {
"script": {
"lang": "painless",
"source": "return Integer.parseInt(doc['_id'].value);"
}
}
}
}
}