背景:在搜索的时候经常会有一种场景就是忽略大小写,不管你输入的是全大写还是全小写还是大小写混合,希望都能够搜索出结果。
解决思路:Elasticsearch中的lowercase normalizer将文本转换为小写,并将其作为规范形式存储在索引中。这可以确保在搜索和比较文本时忽略大写字母的差异,从而提高搜索的准确性和一致性
实现方案:
开发过程中,经常会遇到索引是之前已经建好的,由于es的规则,只能新增字段,不能删除或者修改已经存在的字段,可以动态更新原有索引的setting,动态更新setting,需要先关闭索引,执行玩更新操作以后,再开启索引,步骤如下:
index_name是 索引名称
field_name是字段名称
POST index_name/_close PUT /index_name/_settings { "index":{ "analysis":{ "normalizer":{ "lowercase_normalizer":{ "type": "custom", "char_filter": [], "filter": ["lowercase"] } } } } } POST index_name/_open PUT /index_name/_mapping { "properties" : { "field_name" : { "type" : "keyword", "normalizer":"lowercase_normalizer" } } }
举个例子:
新建一个索引:
PUT /lowercase_normalizer_index { "mappings" : { "properties" : { "id" : { "type" : "keyword" } } } }
新增字段 设置属性
POST lowercase_normalizer_index/_close PUT /lowercase_normalizer_index/_settings { "index":{ "analysis":{ "normalizer":{ "lowercase_normalizer":{ "type": "custom", "char_filter": [], "filter": ["lowercase"] } } } } } POST lowercase_normalizer_index/_open PUT /lowercase_normalizer_index/_mapping { "properties" : { "name" : { "type" : "keyword", "normalizer":"lowercase_normalizer" } } }
插入一些数据
PUT lowercase_normalizer_index/_doc/1 { "id": "1", "name": "Mohammed Ahmed" }
PUT lowercase_normalizer_index/_doc/2 { "id": "2", "name": "Sophie Martin" }
PUT lowercase_normalizer_index/_doc/3 { "id": "3", "name": "mohammed ahmed" }
验证
GET lowercase_normalizer_index/_search { "query": { "wildcard": { "name": { "value": "*moham*" } } } }
结果文章来源:https://www.toymoban.com/news/detail-482373.html
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "lowercase_normalizer_index", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "id" : "1", "name" : "Mohammed Ahmed" } }, { "_index" : "lowercase_normalizer_index", "_type" : "_doc", "_id" : "3", "_score" : 1.0, "_source" : { "id" : "3", "name" : "mohammed ahmed" } } ] } }
文章来源地址https://www.toymoban.com/news/detail-482373.html
到了这里,关于elasticsearch 实现查询忽略大小写的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!