Jieba 中文分词 Elasticsearch 插件
- 更新 ES 版本到 8.11.1
- 更新 Java 版本到 17
- 更新 jieba-analysis 依赖
- 新增
7.17.x分支,支持 ES7.17.0 - 新增
8.4.1分支,支持 ES8.4.1
- 支持动态添加字典,不重启 ES
- 简单修改即可适配不同版本的 ES
| 组件 | 版本 |
|---|---|
| Gradle | 7.6 |
| Java | 17+ (需与目标 ES 版本兼容) |
| Elasticsearch | 8.11.1 (当前默认) |
git clone https://github.com/sing1ee/elasticsearch-jieba-plugin.git --recursive
cd elasticsearch-jieba-plugin./gradlew clean pz构建成功后,插件包位于:build/distributions/elasticsearch-jieba-plugin-{version}.zip
# 复制到 ES 插件目录
cp build/distributions/elasticsearch-jieba-plugin-1.0.0.zip ${ES_HOME}/plugins/
# 解压
cd ${ES_HOME}/plugins/
unzip elasticsearch-jieba-plugin-1.0.0.zip
rm elasticsearch-jieba-plugin-1.0.0.zip
# 启动 ES
cd ${ES_HOME}
./bin/elasticsearch# 测试 jieba_search 分词器
curl -X POST "localhost:9200/_analyze" -H 'Content-Type: application/json' -d'
{
"analyzer": "jieba_search",
"text": "中华人民共和国国歌"
}
'
# 测试 jieba_index 分词器
curl -X POST "localhost:9200/_analyze" -H 'Content-Type: application/json' -d'
{
"analyzer": "jieba_index",
"text": "中华人民共和国国歌"
}
'如果需要适配其他版本的 Elasticsearch,按以下步骤操作:
根据目标 ES 版本,查阅 ES 和 JDK 版本对应关系,确定需要的 Java 版本。
例如:
| ES 版本 | 推荐 Java 版本 |
|---|---|
| 8.x | 17, 21 |
| 7.17.x | 11, 17 |
| 7.x | 11 |
修改以下三处:
// 1. 插件版本(可选)
version = '1.0.0'
// 2. Java 版本
sourceCompatibility = "17" // 改为你的 Java 版本
targetCompatibility = "17" // 改为你的 Java 版本
// 3. ES 依赖版本
dependencies {
implementation 'org.elasticsearch:elasticsearch:8.11.1' // 改为目标 ES 版本
}# 插件版本
version=1.0.0
# Java 版本
java.version=17
# ES 版本(必须与 build.gradle 中一致)
elasticsearch.version=8.11.1java -version确保当前 Java 版本与配置一致。
./gradlew clean pz构建成功后,检查生成的 zip 包:
ls -la build/distributions/如果遇到 ClassNotFoundException: org.gradle.wrapper.GradleWrapperMain,执行:
# 如果已安装 Gradle
gradle wrapper --gradle-version 7.6
# 或手动下载
curl -L -o gradle/wrapper/gradle-wrapper.jar \
https://github.com/gradle/gradle/raw/v7.6.0/gradle/wrapper/gradle-wrapper.jar如果遇到 错误: 无效的源发行版,检查:
- 系统 Java 版本:
java -version build.gradle中的sourceCompatibilityplugin-descriptor.properties中的java.version
确保三者一致。
| 分支 | tag | elasticsearch版本 | Release Link |
|---|---|---|---|
| master | - | v8.11.1 | 从源码构建 |
| 7.7.0 | tag v7.7.1 | v7.7.0 | Download: v7.7.0 |
| 7.4.2 | tag v7.4.2 | v7.4.2 | Download: v7.4.2 |
| 7.3.0 | tag v7.3.0 | v7.3.0 | Download: v7.3.0 |
| 7.0.0 | tag v7.0.0 | v7.0.0 | Download: v7.0.0 |
| 6.4.0 | tag v6.4.1 | v6.4.0 | Download: v6.4.1 |
| 6.0.0 | tag v6.0.0 | v6.0.0 | Download: v6.0.1 |
| 5.4.0 | tag v5.4.0 | v5.4.0 | Download: v5.4.0 |
| 5.3.0 | tag v5.3.0 | v5.3.0 | Download: v5.3.0 |
| 5.2.2 | tag v5.2.2 | v5.2.2 | Download: v5.2.2 |
| 5.2.1 | tag v5.2.1 | v5.2.1 | Download: v5.2.1 |
| 5.2 | tag v5.2.0 | v5.2.0 | Download: v5.2.0 |
| 5.1.2 | tag v5.1.2 | v5.1.2 | Download: v5.1.2 |
| 5.1.1 | tag v5.1.1 | v5.1.1 | Download: v5.1.1 |
将 .dict 后缀的词典文件放入 ${ES_HOME}/plugins/jieba/dic 目录,格式如下:
小清新 3
百搭 3
显瘦 3
隨身碟 100
你的词语 词频
mkdir -p ${ES_HOME}/config/stopwordscp ${ES_HOME}/plugins/jieba/dic/stopwords.txt ${ES_HOME}/config/stopwords/PUT http://localhost:9200/jieba_index{
"settings": {
"analysis": {
"filter": {
"jieba_stop": {
"type": "stop",
"stopwords_path": "stopwords/stopwords.txt"
},
"jieba_synonym": {
"type": "synonym",
"synonyms_path": "synonyms/synonyms.txt"
}
},
"analyzer": {
"my_ana": {
"tokenizer": "jieba_index",
"filter": [
"lowercase",
"jieba_stop",
"jieba_synonym"
]
}
}
}
}
}POST http://localhost:9200/jieba_index/_analyze
{
"analyzer": "my_ana",
"text": "黄河之水天上来"
}响应示例:
{
"tokens": [
{ "token": "黄河", "start_offset": 0, "end_offset": 2, "type": "word", "position": 0 },
{ "token": "黄河之水天上来", "start_offset": 0, "end_offset": 7, "type": "word", "position": 0 },
{ "token": "之水", "start_offset": 2, "end_offset": 4, "type": "word", "position": 1 },
{ "token": "天上", "start_offset": 4, "end_offset": 6, "type": "word", "position": 2 },
{ "token": "上来", "start_offset": 5, "end_offset": 7, "type": "word", "position": 2 }
]
}MIT License
- 迁移自 jieba-solr
- 基于 jieba 中文分词
- 感谢 huaban 提供的 jieba-analysis 依赖