elasticsearch中查询DSL中的must和filter有什么区别?

What is the difference between must and filter in Query DSL in elasticsearch?

我是弹性搜索的新手,我对 must 和 filter 感到困惑。我想在我的条款之间执行一个与操作,所以我这样做了

POST /xyz/_search

{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "city": "city1"
                    }
                },
                {
                    "term": {
                        "saleType": "sale_type1"
                    }
                }
            ]
        }
    }
}

这给了我匹配这两个术语的所需结果,并且使用这样的过滤器

POST /xyz/_search

{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "city": "city1"
                    }
                }
            ],
            "filter": {
                "term": {
                    "saleType": "sale_type1"
                }
            }
        }
    }
}

我得到了相同的结果,那么什么时候应该使用must,什么时候应该使用filter?有什么区别?

must 对分数有贡献。在filter中,查询的分数被忽略。

mustfilter中,子句(查询)必须出现在匹配的文档中。这就是得到相同结果的原因。

你可以检查这个link

Score

The relevance score of each document is represented by a positive floating-point number called the _score. The higher the _score, the more relevant the document.

查询子句为每个文档生成一个 _score

要了解分数是如何计算的,请参阅此link