Elasticsearch 搜索不区分大小写

自定义 normalizer ,并且在需要忽略大小写的字段上指定自定义的 normalizer 。

创建索引

curl -XPUT -H "Content-Type: application/json" http://10.0.5.10:9200/account-list-v5?pretty -d '{
    "settings": {
      "index": {
        "mapper": {
          "dynamic": "true"
        },
        "analysis": {
          "analyzer": {
            "default": {
              "type": "ik_max_word"
            }
          },
          "searchAnalyzer": {
            "default": {
              "type": "ik_max_word"
            }
          },
          "normalizer": {
            "my_normalizer": {
              "type": "custom",
              "char_filter": [],
              "filter": ["lowercase", "asciifolding"]
            }
          }
        }
      }
    },
    "mappings": {
      "account-list-item": {
        "properties": {
          "wechatId": {
            "type": "keyword",
            "normalizer": "my_normalizer"
          },
          "badge": {
            "type": "keyword"
          }
        }
      }
    }
}';

重建索引

curl -XPOST -H "Content-Type: application/json" http://10.0.5.10:9200/_reindex?pretty -d '{
    "source": {"index": "account-list-v3"},
    "dest": {"index": "account-list-v5"}
}';

替换别名

curl -XPOST -H "Content-Type: application/json" http://10.0.5.10:9200/_aliases?pretty -d '{
    "actions" : [
      {"remove": {"index": "account-list-v4", "alias": "account-list"}},
      {"add": {"index": "account-list-v5", "alias": "account-list"}}
    ]
}';