如何在Elasticsearch中实现区分大小写的搜索? [英] How to implement case sensitive search in elasticsearch?
问题描述
我的索引文档中有一个字段,我需要在区分大小写的情况下进行搜索。我正在使用匹配查询来获取结果。
我的数据文档的示例是:
I have a field in my indexed documents where i need to search with case being sensitive. I am using the match query to fetch the results. An example of my data document is :
{
"name" : "binoy",
"age" : 26,
"country": "India"
}
现在,当我给出以下查询时:
Now when I give the following query:
{
"query" : {
"match" : {
"name" : "Binoy"
}
}
}
它为我提供了 binoy与 Binoy的匹配项。我希望搜索区分大小写,默认情况下,elasticsearch似乎不区分大小写。
It gives me a match for "binoy" against "Binoy". I want the search to be case sensitive. It seems by default,elasticsearch seems to go with case being insensitive. How to make the search case sensitive in elasticsearch?
推荐答案
在映射中,您可以将字段定义为not_analyzed。
In the mapping you can define the field as not_analyzed.
curl -X PUT "http://localhost:9200/sample" -d '{
"index": {
"number_of_shards": 1,
"number_of_replicas": 1
}
}'
echo
curl -X PUT "http://localhost:9200/sample/data/_mapping" -d '{
"data": {
"properties": {
"name": {
"type": "string",
"index": "not_analyzed"
}
}
}
}'
现在,如果您可以执行常规索引并进行常规搜索,它将不会对其进行分析并确保其提供不区分大小写的搜索。
Now if you can do normal index and do normal search , it wont analyze it and make sure it deliver case insensitive search.
这篇关于如何在Elasticsearch中实现区分大小写的搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!