在script_score中访问数组

Max*_*Max 5 elasticsearch

对于我的一个ElasticSearch查询,我想定义一个函数得分并按它排序.功能分数采用计算得分并为其添加提升值.每个文档的增强值都存储在数字字段中.

像这样:

{
    "from": 0,
    "size": 10,
    "query": {
        "function_score": {
            "functions": [
                {
                    "script_score": {
                        "script": "_score + doc['boostFactor'].value"
                    }
                }
            ],
            "query": {
                "filtered": {
                    "query": {
                        "query_string": {
                            "query": "search for this!"
                        }
                    }
                }
            },
            "boost_mode": "replace"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

到目前为止一切顺利,这很好.现在问题出在这里:前进,每个文档都有一个boost值数组:

"boostFactor": [ 0, 0.5, 1, 3 ]
Run Code Online (Sandbox Code Playgroud)

在启动查询时,我希望脚本得分能够根据预定义的索引选择数组的特定元素.所以让我们说index = 2,那么脚本得分可能看起来像这样:

                "script_score": {
                    "script": "_score + doc['boostFactor'][2].value"
                }
Run Code Online (Sandbox Code Playgroud)

不幸的是,ElasticSearch不接受这个:

...Query Failed [Failed to execute main query]]; 
nested: CompileException[[Error: unexpected token in constructor]...
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

kie*_*lni 7

doc['boostFactor'].value返回单个值.要获得整个阵列,请使用doc['boostFactor'].values; 那么你可以按照你的期望在数组中获得一个特定的索引:

            {
                "script_score": {
                    "script": "_score + doc['boostFactor'].values[2]"
                }
            }
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅Elasticsearch文档的脚本部分:http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html

  • 这种方法的问题是doc ['boostFactor']不保留数组元素的顺序.请参阅:http://elasticsearch-users.115913.n3.nabble.com/Accessing-array-field-within-Native-Plugin-td4042848.html (7认同)