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"
      }
    }
  }
}
相关推荐
AllData公司负责人8 分钟前
实战二:AllData大模型与AI数据底座-数据传输平台-DBSyncer,搭建 Http 接口同步至 Doris,API 业务数据自动拉取入数仓
大数据·数据库·数据仓库·人工智能·算法·http·doris
飞奔的瓜牛4 小时前
Elasticsearch 日常巡检清单与实战避坑指南
大数据·elasticsearch·搜索引擎
龙腾AI白云6 小时前
多源数据融合:把零散信号拼成一致图景
大数据·人工智能·python·机器学习·知识图谱
Elasticsearch6 小时前
数据摄取路径原本只在某个人的脑子里,Elastic Streams Map 能把它画出来
elasticsearch
字节跳动数据平台6 小时前
知识引擎 VeKnow 文档解析实践:三个评测集验证,从资料入库到可信使用
大数据
岳涛7 小时前
考古|_parent 在 ES 2.0 被重写,老索引不重建就用不上新实现
elasticsearch
Elasticsearch8 小时前
两行,一个文档:在 Logstash 中配对 Elasticsearch 批量操作
elasticsearch
starzy19909 小时前
Structured Streaming 集成 Kafka 实现:读写路径、位点语义与生产调优
大数据·分布式·spark·kafka
starzy199010 小时前
Structured Streaming 案例分析:三个不同形态的流,三套完全不同的答案
大数据·spark·kafka
Aloudata19 小时前
制造企业做库存周转分析:沿用 ERP 报表,还是用语义层统一跨工厂口径?
大数据·数据分析·制造·语义层