背景:在搜索的时候经常会有一种场景就是忽略大小写,不管你输入的是全大写还是全小写还是大小写混合,希望都能够搜索出结果。
解决思路: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*"
- }
- }
- }
- }
复制代码 结果- {
- "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"
- }
- }
- ]
- }
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |