您当前的位置: 首页 >  ar

一一哥Sun

暂无认证

  • 3浏览

    0关注

    622博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Day16_11_ES教程之解决ElasticSearch5.x中@Field注解之IK分词不能用的问题

一一哥Sun 发布时间:2019-07-12 13:17:49 ,浏览量:3

解决ElasticSearch5.x中@Field注解之IK分词不能用的问题 一. 概述

我在之前的项目中,整合了Springboot和ElasticSearch,实现了es的增删改查等操作.但是在检索中文词汇时,没有实现中文分词,本篇教程就是用来解决es中已有的索引库无法实现中文分词的.

SpringBoot整合es,以及安装中文分词器教程请参考我之前的两篇博文.

https://blog.csdn.net/syc000666/article/details/94975691

https://blog.csdn.net/syc000666/article/details/95525852

1. 环境

ElasticSearch版本5.6.8,SpringBoot 2.1.1.RELEASE,索引teachers

2. 问题描述

使用@Field注解给实体类指定ik分词解析器(ik_smart/ik_max_word),测试分词功能,发现并不能达到预期的效果,查看mapping,并没有自动生成ik配置.

二. 解决方案

由于ElasticSearch索引一旦建立,就无法动态修改其字段的映射类型,为了不影响线上的访问,需要无缝切换到新的索引上,使用 ElasticSearch 提供的 reindex api 来迁移数据,创建新的索引.

1. 查看已有的mapping
get /teachers/_mapping?pretty
2. 创建一个新的索引
PUT /ts
3. 设置新索引的mapping
put /ts/_mapping/ts
{
    "properties": {
        "id": {
            "type": "text",
            "fields": {
               "keyword": {
                "type": "keyword",
                 "ignore_above": 256
              }
            }
          },
    "name": {
            "type": "text",
            "analyzer": "ik_smart",
            "search_analyzer": "ik_smart",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "sex": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
}
4. 同步数据

使用 reindex 将原来的索引重建到新的索引上.

post _reindex
{
  "source":{
    "index":"teachers"
  },
  "dest":{
    "index":"ts"
  }
}
5. 查看数据是否已同步到新的索引上
GET /ts/_search
6. 使用别名,切换索引(同时删除原索引teachers)
post _aliases
{
    "actions": [
        {
            "add": {
            "index": "ts",
            "alias": "teachers"
            }
        },
        {
            "remove_index": {
            "index": "teachers"
            }
        }
    ]
}           

现在可以同时使用teachers和ts搜索数据了.

实现了中文分词.

 

关注
打赏
1665624836
查看更多评论
立即登录/注册

微信扫码登录

0.0509s