在 elasticsearch 中,查询 DSL 中的必须和过滤器有什么区别?

我是新的弹性搜索和我之间的必须过滤混淆。我想在我的条件之间执行一个和运算,所以我这样做了

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?有什么区别吗?

42103 次浏览

must contributes to the score. In filter, the score of the query is ignored.

In both must and filter, the clause(query) must appear in matching documents. This is the reason for getting same results.

You may check this 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.

A query clause generates a _score for each document.

To know how score is calculated, refer this link