在Elasticsearch中的“过滤器”之后,“应该”如何工作?

我正在使用Elasticsearch 6.8。我的索引中有一个文档,如下所示:

$ curl "http://localhost:9200/users/_search"|jq
{
  "took": 2,"timed_out": false,"_shards": {
    "total": 1,"successful": 1,"skipped": 0,"failed": 0
  },"hits": {
    "total": {
      "value": 1,"relation": "eq"
    },"max_score": 1,"hits": [
      {
        "_index": "users","_type": "1","_id": "WzLeNG4BVtGh82ZM-BBl","_score": 1,"_source": {
          "name": "joey","description": "xpxpxp all xpxpxp"
        }
      }
    ]
  }
}

如果我在下面的查询中使用它,确实会向我返回文档。

{
    "query": {
        "bool": {
            "filter": {
                "term": {
                    "name": "joey"
                }   
            },"should": [
                {
                    "constant_score": {
                        "filter": {
                            "match_phrase": {
                                "description": "xpxpxp dd All xpxpxp"
                            }
                        }
                    }
                }
            ]
        }
    }
}

但是如果我使用以下查询,它什么也不会返回:

{
    "query": {
        "bool": {
            "should": [
                {
                    "constant_score": {
                        "filter": {
                            "match_phrase": {
                                "description": "xpxpxp dd All xpxpxp"
                            }
                        }
                    }
                }
            ]
        }
    }
}

这两个查询请求的不同之处在于,第一个查询在filter之前有一个should。我不知道为什么这会改变should的行为?

zgfjwzwxb 回答:在Elasticsearch中的“过滤器”之后,“应该”如何工作?

您的should子句不匹配,因为match_phrase字段上的description不匹配xpxpxp dd All xpxpxp,因为该字段包含xpxpxp all xpxpxp

第一个查询返回文档,因为name字段与joey匹配。

本文链接:https://www.f2er.com/3168892.html

大家都在问