首页 > 解决方案 > 术语查询返回 0 个结果 elasticsearch

问题描述

我有以下文件:

        {
            "_index": "testrest",
            "_type": "testrest",
            "_id": "sadfasdfw1",
            "_score": 1,
            "_source": {,
                "s_item_name": "Create",
                "request_id": "35",
                "Result": "Match",
            }
        },

我正在尝试获取具有字段term值和.MatchResultrequest_id35

这是我的查询:

{
  "query": { 
    "bool": { 
      "must": [ 
        { "term":  { "Result": "Match" }}
      ],
      "filter": {
        "term": {
            "request_id": "35"
        }
      }
    }
  }
}

但我得到了0结果。

替换termmatch我的结果。

标签: elasticsearch

解决方案


这是因为Result是一个text字段,因此,它被分析并且值被索引为match而不是Match

一般来说,您不应该使用term查询,而是更喜欢使用match查询。在您的情况下,这将解决问题。

如果您绝对想使用term查询并拥有一个名为 的字段Result.keyword,那么您也可以这样做。

回顾一下:

{
  "query": { 
    "bool": { 
      "must": [ 
        { "match":  { "Result": "Match" }}         <-- use match query
      ],
      "filter": {
        "term": {
            "request_id": "35"
        }
      }
    }
  }
}

或者

{
  "query": { 
    "bool": { 
      "must": [ 
        { "term":  { "Result.keyword": "Match" }}  <-- use term query on keyword field
      ],
      "filter": {
        "term": {
            "request_id": "35"
        }
      }
    }
  }
}

推荐阅读