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
文章目录
- ES08-ES使用IK分词器
- 1-IK分词器
-
- [1. 安装 IK 插件(服务端操作)](#1. 安装 IK 插件(服务端操作))
- [2. 创建索引时指定 IK 分词器](#2. 创建索引时指定 IK 分词器)
- [3. 搜索时正常使用](#3. 搜索时正常使用)
- [⚠️ 关键坑点:已有索引不能直接改](#⚠️ 关键坑点:已有索引不能直接改)
- 2-IK代码实现

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"
}
}
}
}