安装 Elasticsearch IK 分词器

安装 Elasticsearch IK 分词器(手动 .zip/.zip 安装)

IK 分词器(IK Analysis)是 Elasticsearch 最常用的中文分词插件,支持 细粒度分词(ik_max_word)智能切分(ik_smart)。以下是详细安装步骤:


1. 下载 IK 分词器

方式 1:直接下载预编译版本(推荐)

访问 IK Releases,选择 与 Elasticsearch 版本匹配 的插件包,例如:

bash 复制代码
# 示例:ES 8.13.0 对应的 IK 版本
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v8.13.0/elasticsearch-analysis-ik-8.13.0.zip

方式 2:源码编译(适用于自定义词典)

bash 复制代码
git clone https://github.com/medcl/elasticsearch-analysis-ik.git
cd elasticsearch-analysis-ik
git checkout v8.13.0  # 切换到对应版本分支
mvn clean package     # 编译(需 Maven 和 JDK)

编译后,在 target/releases/ 目录下生成 .zip 文件。


2. 安装 IK 插件到 Elasticsearch

(1)创建插件目录

bash 复制代码
cd /usr/local/elasticsearch/plugins
sudo mkdir ik
sudo unzip elasticsearch-analysis-ik-8.13.0.zip -d ik/
sudo chown -R elasticsearch:elasticsearch ik/  # 确保权限正确

(2)验证安装

bash 复制代码
# 查看已安装插件
/usr/local/elasticsearch/bin/elasticsearch-plugin list

输出应包含:

plaintext 复制代码
analysis-ik

3. 重启 Elasticsearch

bash 复制代码
# 如果使用 systemd
sudo systemctl restart elasticsearch

# 如果手动运行
ps -ef | grep elasticsearch  # 找到进程 ID
kill -9 <PID>               # 停止
/usr/local/elasticsearch/bin/elasticsearch -d  # 后台启动

4. 测试 IK 分词器

(1)创建测试索引

bash 复制代码
curl -XPUT "http://localhost:9200/ik_test" -H "Content-Type: application/json" -d'
{
  "settings": {
    "analysis": {
      "analyzer": {
        "ik_analyzer": {
          "type": "custom",
          "tokenizer": "ik_max_word"
        }
      }
    }
  }
}'

(2)测试分词效果

bash 复制代码
curl -XGET "http://localhost:9200/ik_test/_analyze" -H "Content-Type: application/json" -d'
{
  "analyzer": "ik_max_word",
  "text": "中华人民共和国"
}'

正常输出:

json 复制代码
{
  "tokens": [
    {"token": "中华人民共和国", "start_offset": 0, "end_offset": 7, "type": "CN_WORD", "position": 0},
    {"token": "中华", "start_offset": 0, "end_offset": 2, "type": "CN_WORD", "position": 1},
    {"token": "华人", "start_offset": 1, "end_offset": 3, "type": "CN_WORD", "position": 2},
    {"token": "人民", "start_offset": 2, "end_offset": 4, "type": "CN_WORD", "position": 3},
    {"token": "共和国", "start_offset": 4, "end_offset": 7, "type": "CN_WORD", "position": 4}
  ]
}

5. 扩展配置(可选)

(1)自定义词典

  1. plugins/ik/config/ 下创建 custom.dic 文件,每行一个词:

    plaintext 复制代码
    区块链
    深度学习
  2. 修改 IKAnalyzer.cfg.xml

    xml 复制代码
    <entry key="ext_dict">custom.dic</entry>

(2)热更新词典(无需重启)

bash 复制代码
curl -XPOST "http://localhost:9200/ik_test/_close"
curl -XPOST "http://localhost:9200/ik_test/_open"

6. 常见问题

(1)版本不匹配

  • 错误:Failed to load plugin [analysis-ik] due to version mismatch
  • 解决:下载与 Elasticsearch 完全一致 的 IK 版本。

(2)权限问题

  • 错误:Permission denied

  • 解决:

    bash 复制代码
    sudo chown -R elasticsearch:elasticsearch /usr/local/elasticsearch/plugins/ik/

(3)分词不生效

  • 检查索引是否使用了正确的分词器:

    json 复制代码
    {
      "mappings": {
        "properties": {
          "content": {
            "type": "text",
            "analyzer": "ik_max_word"
          }
        }
      }
    }

总结

步骤 命令/操作
1. 下载 IK wget https://github.com/.../elasticsearch-analysis-ik-8.13.0.zip
2. 解压到插件目录 unzip -d /usr/local/elasticsearch/plugins/ik/
3. 重启 ES systemctl restart elasticsearch
4. 测试分词 curl -XGET "http://localhost:9200/_analyze" -d'{"text":"测试文本"}'

完成! 现在你的 Elasticsearch 已支持中文分词。

相关推荐
财经资讯数据_灵砚智能13 小时前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年4月8日
大数据·人工智能·信息可视化·自然语言处理·ai编程
JosieBook14 小时前
【数据库】为何“端边云”协同架构正在重塑大数据存储格局?
大数据·数据库·架构
小真zzz14 小时前
搜极星:你的免费“AI内容验真器”
大数据·人工智能·ai·chatgpt·seo·geo
wanhengidc14 小时前
服务器租用的好处
大数据·运维·服务器·游戏·智能手机
lifallen14 小时前
Flink Agents:Watermark 与事件时间 (Event Time) 在 Agent 算子中的演进分析
java·大数据·人工智能·语言模型·flink
LDG_AGI14 小时前
【搜索引擎】Elasticsearch(三):基于script_score的自定义搜索排序
大数据·人工智能·深度学习·elasticsearch·机器学习·搜索引擎·推荐算法
Elastic 中国社区官方博客14 小时前
如何使用 Mastra 和 Elasticsearch 构建具备代理能力的 AI 应用
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
志栋智能14 小时前
从脚本到智能体:低成本IT运维自动化演进路径
大数据·运维·服务器·人工智能·自动化
LDG_AGI14 小时前
【搜索引擎】Elasticsearch(一):索引创建、数据插入、请求示例
人工智能·深度学习·算法·elasticsearch·机器学习·搜索引擎·推荐算法
企智汇-项目管理软件厂商14 小时前
企智汇项目管理软件怎么样?企智汇软件全面解析:优势、服务、安全与价格深度评测!
大数据·运维·项目管理·devops·项目管理软件·项目管理系统·企业管理软件