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"
      }
    }
  }
}
相关推荐
泡干脆面就番茄1 小时前
Hive-DML:Load-Insert-Export-Import四种数据进出方式
大数据·数据仓库·hive·hadoop
加百力1 小时前
沪指险守3700点,券商、银行午后发力,创业板跌近3%,半导体尾盘反扑,恒科指跌3%,科网股普跌
大数据
eBest数字化转型方案2 小时前
快消渠道 AI 落地路线:拍照、数据、决策三阶段的工程逻辑
大数据·人工智能
百胜软件@百胜软件2 小时前
百胜软件黄飞:让AI真正走进零售,把不确定性变成“胜券在握”
大数据·人工智能·零售
4SAPI2 小时前
官方API、API聚合平台与自建LLM Gateway:三种方案怎么选
大数据·人工智能·gateway·php
ManageEngineITSM3 小时前
工单优先级怎么定?影响×紧急度矩阵的设计与落地方法
java·大数据·运维·前端·数据库·变更管理
智商网输送线配件3 小时前
电子制造零散耗材补货实战:小批量混批采购平台怎么选才不停机?
大数据·服务器·前端·数据库·自动化
程序员IT行业3 小时前
广东行政事业单位物资管理系统选型:先定功能需求,再定部署架构
大数据·架构
橙色云-智橙协同研发3 小时前
汽车零部件行业:如何应对主机厂的严苛审核?
大数据·运维·服务器·人工智能·安全