Elasticsearch:如何实现区分大小写的术语查询?

时间:2018-05-17 11:30:24

标签: 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" : "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"}}]}}

我不想在这里使用匹配,因为我也在其他字段中创建这些查询,并且我想确保完全匹配行为。任何人都可以指出我的查询是如何正确的以及为什么这个术语查询不起作用?

我使用这个dockerimage作为我的服务器:

docker.elastic.co/elasticsearch/elasticsearch:6.2.4

0 个答案:

没有答案