简体   繁体   English

Elasticsearch 术语查询问题

[英]Elasticsearch term query issue

enter image description here在此处输入图像描述

enter image description here在此处输入图像描述

As the pictures show the record field of dispatchvoucher value is "True" But when I searched with the term it cannot found any record.如图所示,dispatchvoucher 值的记录字段为“True” 但是当我用这个词搜索时,它找不到任何记录。 when I changed the value to "true", the result match.当我将值更改为“true”时,结果匹配。 What's the reason for this?这是什么原因? enter image description here在此处输入图像描述

As mentioned in the documentation:如文档中所述:

Avoid using the term query for text fields.避免对文本字段使用术语查询。

By default, Elasticsearch changes the values of text fields as part of analysis.默认情况下,Elasticsearch 会在分析过程中更改文本字段的值。 This can make finding exact matches for text field values difficult.这会使查找文本字段值的精确匹配变得困难。

To search text field values, use the match query instead.要搜索文本字段值,请改用匹配查询。

The standard analyzer is the default analyzer which is used if none is specified. 标准分析器是默认分析器,如果没有指定则使用。 It provides grammar-based tokenization.它提供基于语法的标记化。

GET /_analyze
    {
      "analyzer" : "standard",
      "text" : "True"
    }

The token generated is -生成的令牌是 -

{
  "tokens": [
    {
      "token": "true", 
      "start_offset": 0,
      "end_offset": 4,
      "type": "<ALPHANUM>",
      "position": 0
    }
  ]
}

Term query returns documents that contain an exact term in a provided field. 术语查询返回在提供的字段中包含确切术语的文档。 Since True gets tokenized to true , so when you are using the term query for "dispatchvoucher": "True" , it will not show any results.由于True被标记为true ,因此当您对"dispatchvoucher": "True"使用术语查询时,它不会显示任何结果。

You can either change your index mapping to您可以将索引映射更改为

{
  "mappings": {
    "properties": {
      "dispatchvoucher": {
        "type": "keyword"
      }
    }
  }
}

OR You need to add .keyword to the dispatchvoucher field.或者您需要将.keyword添加到dispatchvoucher字段。 This uses the keyword analyzer instead of the standard analyzer (notice the ".keyword" after dispatchvoucher field).这使用关键字分析器而不是标准分析器(注意dispatchvoucher字段后的“.keyword”)。

Adding a working example with index data, search query, and search result添加带有索引数据、搜索查询和搜索结果的工作示例

Index Data:指数数据:

{
  "dispatchvoucher": "True"
}

Search Query:搜索查询:

{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "dispatchvoucher.keyword": "True"
        }
      }
    }
  }
}

Search Result:搜索结果:

"hits": [
      {
        "_index": "65605120",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.0,
        "_source": {
          "dispatchvoucher": "True"
        }
      }
    ]

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

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