ES08-ES使用IK分词器

ES08-ES使用IK分词器

shell 复制代码
1-ES使用IK分词器->底层逻辑:【ES要安装IK分词】+【创建索引时指定IK分词器】+【ES通用查询即可】
2-创建索引时指定IK分词器->后续数据插入的时候,类似RAG的embedding,直接存储的数据就是IK分词器处理后的数据
- ik_max_word(最细粒度)
- ik_smart(粗粒度)
3-针对已经存在的index,不能直接更换分词->解决方案:使用_reindex进行index1->index2的数据迁移
4-IK分词器代码实现仓库-ESIkController:https://gitee.com/enzoism/springboot-springdata-es
5-ES容器化部署方案(鉴权+非鉴权):https://gitee.com/enzoism/knowledge_2026_es

文章目录


1-IK分词器

更换 IK 分词器不是搜索时做的,而是要在 建索引(mapping)阶段指定。完整流程如下:

1. 安装 IK 插件(服务端操作)

IK 是第三方插件,需要先安装到 ES 节点上:

bash 复制代码
# ES 5.x/6.x/7.x 对应下载对应版本,例如 ES 7.x:
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.17.0/elasticsearch-analysis-ik-7.17.0.zip

# 重启 Elasticsearch

注意:IK 版本必须与 ES 版本一致。多节点集群每个节点都要装。

2. 创建索引时指定 IK 分词器

在 Postman 中发送 PUT 请求创建索引:

json 复制代码
PUT http://localhost:9200/my_index
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "content": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      }
    }
  }
}
  • ik_max_word:最细粒度切分,建索引时用(保证召回率)
  • ik_smart:粗粒度切分,搜索时用(保证精确度)

3. 搜索时正常使用

json 复制代码
GET http://localhost:9200/my_index/_search
{
  "query": {
    "match": {
      "title": "中华人民共和国"
    }
  }
}

因为 mapping 里已经指定了 search_analyzer,Postman 发查询请求时无需再指定分词器,ES 会自动使用 ik_smart。

⚠️ 关键坑点:已有索引不能直接改

如果索引已经存在且用的是 standard 分词器,不能原地修改 analyzer,需要重建索引:

json 复制代码
PUT http://localhost:9200/my_index_v2
{
  "mappings": {
    "properties": {
      "title": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" }
    }
  }
}

然后迁移数据(推荐用 _reindex):

json 复制代码
POST http://localhost:9200/_reindex
{
  "source": { "index": "my_index" },
  "dest": { "index": "my_index_v2" }
}

最后可以用别名 my_index 指向 my_index_v2,做到对业务无感切换。

一句话总结:分词器在 mapping 里定,Postman 只管发请求;已有数据必须用 reindex 迁移,否则旧数据仍是按标准分词器建立的倒排索引,搜不出效果。


2-IK代码实现

IK分词器代码实现仓库-ESIkController:https://gitee.com/enzoism/springboot-springdata-es

1-ik_max_word(最细粒度)

2-ik_smart(粗粒度)


3-创建Index时配置-Java版本

java 复制代码
/**
 * IK 分词演示文档。
 * <p>
 * 分词器在 <b>建索引(mapping)阶段</b>通过实体注解指定,而不是搜索时:
 * <ul>
 *   <li>analyzer = ik_max_word:建索引时最细粒度切分,保证召回率</li>
 *   <li>searchAnalyzer = ik_smart:搜索时粗粒度切分,保证精确度。
 *       mapping 里指定后,查询请求无需(也不应)再传 analyzer,ES 自动套用</li>
 * </ul>
 * 前提:ES 节点需安装 elasticsearch-analysis-ik 插件,版本必须与 ES 一致(本地 8.15.3 已装)。
 */
@Document(indexName = "index_ik_demo")
@Setting(replicas = 0, shards = 1)   // 开发阶段简化
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class IkDoc {

    @Id
    @Field(type = FieldType.Keyword)
    private String docId;

    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private String title;

    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private String content;
}

4-创建Index时配置-JSON版本

json 复制代码
PUT http://localhost:9200/my_index
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "content": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      }
    }
  }
}
相关推荐
其实防守也摸鱼18 小时前
个人学习 Pwn 思路:从基础到漏洞利用的进阶路径
大数据·前端·人工智能·chrome·算法·安全·网络安全
美林数据Tempodata18 小时前
高职大数据技术专业升级的六个落地样本:从产业链需求库到实训室数据链路
大数据·人工智能·数字化转型·产教融合·ai教育
跨境卫士苏苏19 小时前
TikTokShop关联账户认定指引更新跨境代运营团队需要注意哪些管理责任
大数据·人工智能·市场营销·跨境电商·营销策略
迅易科技20 小时前
数据质量治理:从异常告警走向自动化根因诊断
大数据
组工部管理能手李哥20 小时前
一次政务知识库体验:AI回答之外,我更在意那行引用来源
大数据·人工智能·大模型·政务
GrowingIO技术社区21 小时前
SaaS与私有化部署怎么比较?责任、集成与成本
大数据·数据挖掘·产品
GrowingIO技术社区21 小时前
CDP项目如何评估?从业务问题到能力测试
大数据·数据分析
m0_7156744321 小时前
政务泛监测体系能力排行:AI 重塑监测边界,治理的颗粒度革命
大数据·网络·人工智能·安全·网络安全·政务
GrowingIO技术社区1 天前
多端用户身份怎么统一?OneID规则与验收清单
大数据·数据挖掘·数据分析
躺柒1 天前
读数据安全架构设计与实践20安全政策文件体系(上)
大数据·人工智能·安全·数据安全·安全架构·安全体系