首页 > 解决方案 > 处理倒数函数时如何防止elasticsearch中的分片失败

问题描述

我必须执行弹性搜索查询,我需要在其中修改查询检索到的文档的分数。我正在使用 function_score 在倒数函数的帮助下修改分数。以下是我的代码,

{
        "function_score": {
            "query": {
                "bool": {
                    "must": [{"match":{"course": "IT"}}]
                }
            },
            "functions": [{
                    "field_value_factor": {
                        "field": "users_score_nested.rank",
                        "modifier": "reciprocal",
                        "missing": 1
                    }
                }
            ],
            "boost_mode": "multiply",
            "score_mode": "sum"
        }
    }

在这里,我需要根据课程字段和排名字段的过滤器来更改我的分数。排名值可以有 0、NULL、1、2 等值。对于排名较高的候选人(最低排名值),分数应该很高。但由于 0 或 NULL 值分片失败发生。除了更改排名值之外,有没有办法防止分片失败或者我可以使用除倒数函数之外的任何其他函数吗?

我得到的错误如下,

{
    "root_cause": [{
        "type": "exception",
        "reason": "Result of field modification [reciprocal(0.0)] must be a number"
    }],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query"
 }

标签: elasticsearchelastic-stack

解决方案


您所能做的就是从数据中删除“users_score_nested.rank”,以便 function_score 查询的“missing”参数生效,倒数为 1。

恐怕没有办法绕过异常。或者,您可以使用 function_score 查询的 script_score 参数

"script_score" : {
            "script" : {
                "source": "Math.pow(doc['users_score_nested']['rank']+1,-1)"
            }
        }

注意:确保字段的 是"type"并且它作为映射的一部分包含在父级中。"rank""integer"


推荐阅读