elasticsearch 8 修改分词器并数据迁移

1. 安装中文索引

下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases

注意:版本要和ES版本对应
在这里插入图片描述
解压后放入plugins文件中
在这里插入图片描述
然后重启服务:docker-compose restart elasticsearch,大概需要1分钟

2. 数据迁移

当索引存在时不能修改已有索引分词器,会出现错误:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "Mapper for [content] conflicts with existing mapper:\n\tCannot update parameter [analyzer] from [default] to [ik_max_word]"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "Mapper for [content] conflicts with existing mapper:\n\tCannot update parameter [analyzer] from [default] to [ik_max_word]"
  },
  "status" : 400
}

因此需要进行一下步骤:

  1. 使用新的mappings创建新索引
  2. 使用reindex将数据从旧索引复制到新索引
  3. 删除旧的索引,并将设置新索引的别名

使用mappings新建索引

PUT /test_new
{
  "mappings": {
    "properties": {
      "content": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

使用reindex将数据从旧索引复制到新索引

POST /_reindex
{
  "source" : {
    "index" : "test"
  },
  "dest" : {
    "index" : "test_new",
    "version_type" : "external"
  }
}

删除旧的索引并设置别名

POST /_aliases
{
    "actions" : [
      {
        "add" : {
          "index" : "test_new",
          "alias" : "test"
        }
      },
    {
        "remove_index" : {
          "index": "test"
      }
    }
  ]
}

3. 总结

重新索引耗时比较长,最好在新建索引时设计好mappings中的分词器,万不得已再使用reindex

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

llc的足迹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值