elasticsearch 实现查询忽略大小写

  |   0 评论   |   0 浏览

对于elasticsearch查询忽略大小写的问题,官方给出了一个解决方案,使用normalizer来解决,normalizer是keyword的一个属性,可以对keyword生成的单一term,query_string做进一步的处理,比如lowercase,小写转换,使用方式和自定义分词器有相似之处,需要自定义,具体步骤如下。
5 此时由于elasticsearch的索引已经建好,由于es的规则,只能新增字段,不能删除或者修改已经存在的字段,可以动态更新原有索引的setting,动态更新setting,需要先关闭索引,执行玩更新操作以后,再开启索引,步骤如下:

1、在需要操作的索引上操作

//关闭index
POST test/_close
//设置settings
PUT test/_settings
{
  "index": {
    "analysis": {
      "normalizer": {
        "lowercase_normalizer": {
          "type": "custom",
          "char_filter": [],
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  }
}
 //打开index
POST test/_open

2、创建索引并备份需要操作的索引

PUT /xsj_test
POST /_reindex
{
  "source": {
    "index": "test"
  },
  "dest": {
    "index": "xsj_test"
  }
}

3、新建一个新索引,设置normalizer属性,命令如下

PUT 
{
"settings": {
    "number_of_replicas": 1,
    "number_of_shards": 1,
    "analysis": {
      "normalizer": {
        "lowercase_normalizer": {
          "type": "custom",
          "char_filter": [],
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  },
  "mappings": {
    "_doc": {
      "properties": {
        "name_code": {
          "type": "keyword",
          "normalizer": "lowercase_normalizer"
        },
        "canonical_name": {
          "type": "keyword",
          "normalizer": "lowercase_normalizer"
        },
        "col_order": {
          "type": "keyword",
          "normalizer": "lowercase_normalizer"
        },
        "kingdom": {
          "type": "keyword",
          "normalizer": "lowercase_normalizer"
        },
        "sp2000_status_id": {
          "type": "keyword",
          "normalizer": "lowercase_normalizer"
        },
        "phylum": {
          "type": "keyword",
          "normalizer": "lowercase_normalizer"
        },
        "genus": {
          "type": "keyword",
          "normalizer": "lowercase_normalizer"
        },
        "infraspecies": {
          "type": "keyword",
          "normalizer": "lowercase_normalizer"
        },
        "species": {
          "type": "keyword",
          "normalizer": "lowercase_normalizer"
        },
        "authorship": {
          "type": "keyword",
          "normalizer": "lowercase_normalizer"
        },
        "name_status": {
          "type": "keyword",
          "normalizer": "lowercase_normalizer"
        },
        "parent_id": {
          "type": "keyword",
          "normalizer": "lowercase_normalizer"
        },
        "rank": {
          "type": "keyword",
          "normalizer": "lowercase_normalizer"
        },
        "infraspecies_marker": {
          "type": "keyword",
          "normalizer": "lowercase_normalizer"
        },
        "family": {
          "type": "keyword",
          "normalizer": "lowercase_normalizer"
        },
        "class": {
          "type": "keyword",
          "normalizer": "lowercase_normalizer"
        }
      }
    }
  }
}

4、新建完索引以后,还原数据,命令如下

POST /_reindex
{
  "source": {
    "index": "test"
  },
  "dest": {
    "index": "xsj_test"
  }
}

来源:https://blog.csdn.net/juanqiao7497/article/details/112249551


标题:elasticsearch 实现查询忽略大小写
作者:zytops
地址:https://zytops.com/articles/2022/12/07/1670398504342.html