Package 在must内的Elasticsearch constant_score未返回预期结果

nkoocmlb  于 2022-11-02  发布在  ElasticSearch
关注(0)|答案(1)|浏览(118)

我有以下ES查询:

{
    "query": {
        "bool": {
            "should": [
                {
                    "constant_score": {
                        "boost": 5,
                        "filter": {
                            "bool": {
                                "must": [
                                    {
                                        "ids": {
                                            "values": [
                                                "winnerAthlete-A"
                                            ]
                                        }
                                    },
                                    {
                                        "dis_max": {
                                            "queries": [
                                                {
                                                    "bool": {
                                                        "filter": {
                                                            "term": {
                                                                "isAthlete": true
                                                            }
                                                        }
                                                    }
                                                },
                                                {
                                                    "bool": {
                                                        "filter": {
                                                            "term": {
                                                                "isWinner": true
                                                            }
                                                        }
                                                    }
                                                }
                                            ]
                                        }
                                    }
                                ]
                            }
                        }
                    }
                },
                {
                    "constant_score": {
                        "boost": 4,
                        "filter": {
                            "bool": {
                                "must": [
                                    {
                                        "ids": {
                                            "values": [
                                                "winnerAthlete-B"
                                            ]
                                        }
                                    },
                                    {
                                        "dis_max": {
                                            "queries": [
                                                {
                                                    "bool": {
                                                        "filter": {
                                                            "term": {
                                                                "isAthlete": true
                                                            }
                                                        }
                                                    }
                                                },
                                                {
                                                    "bool": {
                                                        "filter": {
                                                            "term": {
                                                                "isWinner": true
                                                            }
                                                        }
                                                    }
                                                }
                                            ]
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        }
    }
}

它确实返回了我所期望的结果:2个文档winnerAthlete-AwinnerAthlete-B,将得分5.0分配给winnerAthlete-A,将得分4.0分配给winnerAthlete-B
现在,当我将查询第三行的should转换为must时,该查询与任何文档都不匹配,而我期望得到完全相同的结果。我无法理解为什么。我曾尝试使用ES _explain关键字来理解为什么在使用must时该查询不匹配,但它没有帮助我。
知道为什么用must重写的这个查询不返回任何东西,而should版本却返回预期的结果吗?

eaf3rand

eaf3rand1#

Should的作用类似于“OR”。它将返回与任何子句匹配的文档。
Must的作用类似于“AND”。文档必须同时满足这两个子句。
您的查询未返回任何结果,因为不存在ID为winnerAthlete-A和winnerAthlete-B的单个文档

相关问题