Query_string в сочетании с function_score всегда дает оценку 1,0
Когда я пытаюсь сделать запрос query_string к моему Elasticsearch, который использует function_score (script_score
) манипулировать своим счетом по умолчанию. Но я всегда, кажется, получаю базу _score
из 1.0
,
Моя модель выглядит так:
{
"name": "Secret Birthday Party",
"description": "SECRET! Discuss with discretion",
"_userCounters": [
{
"user": "king",
"count": 12
},
{
"user": "joseph",
"count": 1
}
]
}
Мой запрос со скриптом function_score выглядит так:
{
"query" : {
"function_score" : {
"query": {
"query_string": {
"query": "secret",
"analyze_wildcard": true,
"fields": [
"name", "description"
]
}
},
"script_score": {
"script": {
"inline" : "int scoreBoost = 1; for (int i = 0; i < params['_source']['_userCounters'].length; i++) { if (params['_source']['_userCounters'][i].user == 'joseph') { scoreBoost += params['_source']['_userCounters'][i].count; } } return scoreBoost;"
}
}
}
}
}
Я получаю результат, который находит именно то, что мне нужно, но возвращает только значение из скрипта function_score. Кажется, встроенная оценка больше не работает. Это ответ, который я получаю:
{
"_index": "test3",
"_type": "projects",
"_id": "7",
"_score": 2, // this is exactly the return value of the script_score. What I want instead is that this value gets multiplied with the normal score of ES
"_source": {
"name": "Secret Birthday Party",
"description": "SECRET! Discuss with discretion",
"_userCounters": [
{
"user": "queen",
"count": 12
},
{
"user": "daniel",
"count": 1
}
]
}
}
Я предполагаю, что мое тело запроса не в правильном формате, так как все оценки просто 1.0
когда я вынимаю function_score полностью.
1 ответ
Я понял. На самом деле это была проблема самого сценария, а не структуры тела запроса.
Функция только возвращала коэффициент, который должен быть умножен на _score
значение. Вместо этого он должен сам сделать умножение.
Вот скрипт немного более читабельный:
int scoreBoost = 1;
for (int i = 0; i < params['_source']['_userData'].length; i++) {
if (params['_source']['_userData'][i].user == '{userId}') {
scoreBoost += params['_source']['_userData'][i].count;
}
}
// the error was here: only the scoreBoost value was returned
// the fix is to multiply it with the _score value
return _score * scoreBoost;