简体   繁体   English

带有空格的Elasticsearch字词查询不起作用

[英]Elasticsearch term query with spaces not working

I'm trying to execute the following query: 我正在尝试执行以下查询:

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

This is not yielding results and I have no idea why. 这没有产生结果,我也不知道为什么。 I have parens and a space. 我有父母和一个空间。 What are my options here? 我在这里有什么选择?

You want to match exact value for which you are using term query. 您要匹配使用术语查询的确切值。 As Amit mentioned in the comment that term query doesn't use analyzer and hence it will match the documents which contains exact same tokens you need to modify the mapping for Thing.Name as below: 正如阿米特(Amit)在评论中提到的那样,术语查询不使用分析器,因此它将与包含完全相同的标记的文档匹配,您需要按以下方式修改Thing.Name的映射:

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

If the mapping is auto generated by elastic then it will have similar properties for name field as above. 如果映射是由Elastic自动生成的,则其名称字段将具有与上述相似的属性。 If it is already this then you need not make any modifications in the mapping. 如果已经存在,则无需在映射中进行任何修改。 Update you query to use Thing.Name.keyword instead of Thing.Name since a field of type keyword doesn't analyze the value and generate a single token, which is the input value itself. 更新您查询使用Thing.Name.keyword代替Thing.Name因为类型的字段keyword没有分析的价值和产生一个令牌,该令牌是输入值本身。

So the query will be : 因此查询将是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM