您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# ElasticSearch中如何使用IK分词器
## 一、IK分词器简介
IK Analyzer(简称IK分词器)是一款专为中文分词设计的开源分词工具,由medcl开发并贡献给ElasticSearch社区。它具备以下核心特性:
1. **智能切分**:支持细粒度切分(ik_smart)和最大词量切分(ik_max_word)两种模式
2. **扩展词典支持**:允许用户自定义专业术语和网络新词
3. **停用词过滤**:可配置过滤无意义的常用词
4. **兼容性好**:完美适配ElasticSearch各主流版本
## 二、安装IK分词器
### 1. 环境准备
确保已安装:
- ElasticSearch 7.x/8.x
- Kibana(可选,用于测试)
### 2. 安装方式
#### 方式一:通过elasticsearch-plugin安装
```bash
# ES7.x
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.17.7/elasticsearch-analysis-ik-7.17.7.zip
# ES8.x
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v8.11.1/elasticsearch-analysis-ik-8.11.1.zip
{ES_HOME}/plugins/ik
目录GET _cat/plugins
应返回包含analysis-ik
的条目
在elasticsearch.yml
中添加:
index.analysis.analyzer.default.type: ik_max_word
{ES_HOME}/config/analysis-ik/
目录下创建:
custom.dic
(主词典)stopword.dic
(停用词词典)IKAnalyzer.cfg.xml
:<entry key="ext_dict">custom.dic</entry>
<entry key="ext_stopwords">stopword.dic</entry>
PUT /news_articles
{
"settings": {
"analysis": {
"analyzer": {
"my_ik_analyzer": {
"type": "custom",
"tokenizer": "ik_max_word",
"filter": ["lowercase"]
}
}
}
},
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "my_ik_analyzer",
"search_analyzer": "ik_smart"
}
}
}
}
POST _analyze
{
"analyzer": "ik_max_word",
"text": "中华人民共和国国歌"
}
返回结果示例:
{
"tokens": [
{"token": "中华人民共和国", "start_offset": 0, "end_offset": 7...},
{"token": "中华", "start_offset": 0...},
{"token": "人民共和国", "start_offset": 2...},
{"token": "国歌", "start_offset": 7...}
]
}
无需重启ES的更新方式:
POST _nodes/reload_secure_settings
{
"secure_settings_password": "your_password"
}
在analysis-ik
目录创建synonyms.txt
:
中国, 中华, 华夏
配置映射时添加:
"filter": {
"my_synonym": {
"type": "synonym",
"synonyms_path": "analysis-ik/synonyms.txt"
}
}
需要安装pinyin插件:
"analyzer": {
"pinyin_ik": {
"tokenizer": "ik_max_word",
"filter": ["pinyin_filter"]
}
}
词典不生效
分词结果不符合预期
_analyze
API测试验证standard
分词器性能优化建议
ik_smart
模式keyword
类型做精确匹配电商场景:
ik_max_word
keyword
内容搜索:
"content": {
"type": "text",
"fields": {
"ik": {"type": "text", "analyzer": "ik_max_word"},
"raw": {"type": "keyword"}
}
}
版本升级:
IK分词器作为ElasticSearch中文处理的核心组件,正确配置后能显著提升搜索质量。建议结合业务场景灵活选择分词策略,并通过持续优化词典保持分词效果。对于特殊需求,可考虑二次开发IK分词器或结合其他分析插件实现更复杂的文本处理流程。 “`
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。