elasticsearch Elasticsearch:如何实现区分大小写的术语查询?
我尝试通过名为unit的字段来查询项目,该字段区分大小写(如kWh),但我的术语查询仅在查询kWh(小写W)时匹配。我在文档中看到的是,这个术语应该是区分大小写的正确术语,所以我不确定我做错了什么
elasticsearch Elasticsearch:如何实现区分大小写的术语查询?,
elasticsearch,elasticsearch-6,
elasticsearch,Elasticsearch 6,我尝试通过名为unit的字段来查询项目,该字段区分大小写(如kWh),但我的术语查询仅在查询kWh(小写W)时匹配。我在文档中看到的是,这个术语应该是区分大小写的正确术语,所以我不确定我做错了什么 ## Create an item curl -X POST "localhost:9200/my_index/my_type/my_id" -H 'Content-Type: application/json' -d'{"point_name" : "my_point_name", "unit" :
## Create an item
curl -X POST "localhost:9200/my_index/my_type/my_id" -H 'Content-Type: application/json' -d'{"point_name" : "my_point_name", "unit" : "kWh"}'
=> {"_index":"my_index","_type":"my_type","_id":"my_id","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"created":true}
## Try to query it by unit with exact match (kWh)
curl -X GET "localhost:9200/my_index/my_type/_search" -H 'Content-Type: application/json' -d'{"query" : { "bool" : {"must" : [{ "term" : {"unit" : "kWh"}}]}}}'
=> {"took":36,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}
## Query with lower case unit kwh
curl -X GET "localhost:9200/my_index/my_type/_search" -H 'Content-Type: application/json' -d'{"query" : { "bool" : {"must" : [{ "term" : {"unit" : "kwh"}}]}}}'
=> {"took":12,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.2876821,"hits":[{"_index":"my_index","_type":"my_type","_id":"my_id","_score":0.2876821,"_source":{"point_name" : "my_point_name", "unit" : "kWh"}}]}}
我不想在这里使用match,因为我也通过其他字段创建这些查询,并且我希望确保精确的匹配行为。有人能告诉我这个查询如何正确,为什么这个术语查询不起作用吗
我正在使用此dockerimage作为我的服务器:
docker.elastic.co/elasticsearch/elasticsearch:6.2.4
您应该检查字段单元的类型。很可能是文本
,它是分析的
,将存储小写的术语。您需要的是关键字
类型。如果您使用的是“默认”映射,那么很可能存在一个unit.keyword
字段,该字段将与术语
查询区分大小写。另见。啊,谢谢你。关键字解决了我的问题。如果你喜欢写一个答案,我会接受它的伟大!没关系,我想这个问题已经被问了很多次了:)