首页 > 解决方案 > 带有空格的 Elasticsearch 术语查询不起作用

问题描述

我正在尝试执行以下查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "Thing.Name": {
              "value": "(item) test",
              "boost": 1
            }
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  }
}

这没有产生结果,我不知道为什么。我有括号和空格。我在这里有什么选择?

标签: elasticsearch

解决方案


您想要匹配您使用术语查询的确切值。正如 Amit 在评论中提到的那样,术语查询不使用分析器,因此它将匹配包含完全相同标记的文档,您需要修改 Thing.Name 的映射,如下所示:

{
  "Thing": {
    "properties": {
      "Name": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

如果映射是由 elastic 自动生成的,那么它的 name 字段将具有与上面类似的属性。如果它已经是这样,那么您无需在映射中进行任何修改。更新您的查询以使用Thing.Name.keyword,而不是Thing.Name因为类型字段keyword不分析值并生成单个标记,即输入值本身。

所以查询将是:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "Thing.Name.keyword": {
              "value": "(item) test",
              "boost": 1
            }
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  }
}

推荐阅读