调整elasticsearch分词器,达到like搜索效果 -基于elasticsearch 6.8

发布时间 2023-12-25 14:23:26作者: 水滴aym

基于elasticsearch 6.8

如果发面es里索引字段,默认搜索时会拆成单字搜索,此时,需要更换分词器,更新已有的分词器会比较麻烦,步骤如下:

1.新建个新索引,这里把它叫做new_index
2.copy老索引数据到新索引
3.删除老索引
4.建立个别名,名字与老索引相同,指向新索引
5.测试效果

下面操作的DSL语句
1.操作语句

new_index   PUT
{
  "mappings": {
    "_doc": {
      "properties": {
          "customerName": {
            "type": "text",
            "analyzer": "keyword"
          },
          "name": {
            "type": "text",
            "analyzer": "keyword"
          }
       }
    }
  }
}

注意这里,分词器要设为keyword,这里可达到模糊搜索效果

2.操作语句
复制索引

_reindex   POST
{
  "source": {
    "index": "old_index"
  },
  "dest": {
    "index": "new_index"
  }
}

3.操作语句

old_index DELETE

4.操作语句

_aliases   POST
{
  "actions": [
    {
      "add": {
        "index": "new_index",
        "alias": "old_index"
      }
    }
  ]
}

上面DSL语句在es的谷哥浏览器head插件页面操作,可成功实现模糊搜索

es的学习推荐官方文档,比较详细,可按版本来阅读.