背景
有时我们想cms搜索的时候更精准一点,可能会用到wildcard不分词的模糊搜索,但是不分词就有区分大小写搜索的问题
解决方案
1.在es查询时不区分大小写,可以让查询更方便,具体设置如下:
{
"settings": {
"analysis": {
"analyzer": {
"caseSensitive": {
"filter": "lowercase",
"type": "custom",
"tokenizer": "keyword"
}
}
}
},
"mappings": {
"personInfo": {
"properties": {
"userName": {
"type": "string",
"analyzer": "caseSensitive",
"search_analyzer": "caseSensitive"
}
}
}
}
}
设置完mappings后,在搜索的时候搜索的 userName 转成小写进行搜索
{
"query": {
"wildcard": {
"userName": "*zhangsan*"
}
}
}
这样设置后就可以了。
2.当然也可以用should查询,先同时查询大写和小写的匹配
{ "query": { "bool"{ "must":[ { "bool":{ "should":[ { "wildcard": { "userName": "*zhangsan*" } }, { "wildcard": { "userName": "*ZHANGSAN*" } } ] } } ] } } }
《本文》有 0 条评论