Query_string 与 function_score 组合总是给出 1.0 分
Query_string in combination with function_score always gives score 1.0
当我尝试向我的 Elasticsearch 发出 query_string 请求时,该请求使用 function_score (script_score
) 来操纵其默认分数。但我似乎总是得到 1.0
的基数 _score
。
我的模型看起来像这样:
{
"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;"
}
}
}
}
}
我得到的结果正是我想要的,但只有 returns 来自 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
}
]
}
}
我的猜测是我的请求正文格式不正确,因为当我完全取出 function_score 时,所有分数都只是 1.0
。
我明白了。这实际上是脚本本身的问题,而不是请求主体的结构。
该函数 仅返回应该与 _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;
当我尝试向我的 Elasticsearch 发出 query_string 请求时,该请求使用 function_score (script_score
) 来操纵其默认分数。但我似乎总是得到 1.0
的基数 _score
。
我的模型看起来像这样:
{
"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;"
}
}
}
}
}
我得到的结果正是我想要的,但只有 returns 来自 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
}
]
}
}
我的猜测是我的请求正文格式不正确,因为当我完全取出 function_score 时,所有分数都只是 1.0
。
我明白了。这实际上是脚本本身的问题,而不是请求主体的结构。
该函数 仅返回应该与 _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;