一、写在前面
es查询默认区分大小写
二、实现原理
normalizer是 keyword的一个属性
参考
听说你还没掌握 ElasticSearch Normalizer 的使用方法?_yinni11的博客-CSDN博客
二、实现区分大小写的三种方案
方案一:直接创建对应的mapping
适用于还没有创建mapping的场景
PUT testIndex
{
"settings":{
"index":{
"number_of_shards":"14",
"number_of_replicas":"2",
"analysis":{
"normalizer":{
"lowercase_normalizer":{
"filter":[
"lowercase"
],
"type":"custom",
"char_filter":[
]
}
}
}
}
},
"mappings":{
"person":{
"dynamic_templates":[
{
"notanalyzed":{
"match_mapping_type":"string",
"mapping":{
"index":true,
"type":"keyword"
}
}
}
],
"dynamic":true,
"enabled":true,
"properties":{
"id":{
"type":"long"
},
"testLower":{
"type":"keyword",
"normalizer":"lowercase_normalizer"
}
}
}
}
}
方案二:新建mapping然后将老的数据导入到新的mapping
参考elasticsearch 实现查询忽略大小写_elasticsearch 忽略大小写_一个想努力学技术的程序员的博客-CSDN博客
方案三、在现有mapping基础上配置normalizer并新增字段,
1、关闭索引,不然会报“Can't update non dynamic settings ”
POST test_index/_close
2、settings中设置normalizer
PUT test_index/_settings
{
"index": {
"analysis": {
"normalizer":{
"lowercase_normalizer":{
"type":"custom",
"char_filter":[
],
"filter":[
"lowercase"
]
}
}
}
}
}
3、打开索引
POST test_index/_open
4、新增字段
PUT test_index/person/_mapping
{
"properties": {
"testFieldLower": {
"type": "keyword",
"normalizer": "lowercase_normalizer"
}
}
}
参考
elasticsearch 实现查询忽略大小写_elasticsearch 忽略大小写_一个想努力学技术的程序员的博客-CSDN博客
文章介绍了Elasticsearch中如何通过Normalizer实现查询时忽略大小写的效果,提供了三种方案:一是创建新的mapping,二是新建mapping再导入数据,三是对现有mapping进行更新并新增字段。每种方案都详细给出了配置步骤。
1万+

被折叠的 条评论
为什么被折叠?



