Query_string与function_score结合使用总是得分1.0

时间:2018-01-02 18:11:38

标签: elasticsearch elasticsearch-painless

当我尝试向我的Elasticsearch发出query_string请求时,它使用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
            }
        ]
    }
}

我的猜测是我的请求正文格式不正确,因为当我完全取出function_score时,所有得分都只是1.0

1 个答案:

答案 0 :(得分:0)

我明白了。这实际上是脚本本身的问题,而不是请求体的结构。

该函数仅返回因子,该因子应该与expandedRowKeys={this.state.expandedKeys}值相乘。相反,它需要自己进行乘法运算。

这是一个比较简单的脚本:

_score