ElasticSearch教程FG008-ElasticSearch分词器配置与使用实战
Part01-基础概念与理论知识
1.1 分词器概念
分词器(Analyzer)是ElasticSearch中负责将文本转换为词项(Term)的组件,是全文搜索的核心。分词器的质量直接影响搜索结果的准确性。更多视频教程www.fgedu.net.cn
分词器的主要作用:
- 将文本分割成词项
- 处理词项(如小写转换、停用词过滤)
- 生成倒排索引
1.2 分词器组成
一个完整的分词器由三部分组成:
- 字符过滤器(Character Filter):处理原始文本,如去除HTML标签
- 分词器(Tokenizer):将文本分割成词项
- 词项过滤器(Token Filter):处理词项,如小写转换、停用词过滤、同义词处理
1.3 内置分词器
ElasticSearch内置了多种分词器:
- Standard Analyzer:默认分词器,适用于大多数语言
- Simple Analyzer:简单分词器,按非字母字符分割
- Whitespace Analyzer:空白分词器,按空白字符分割
- Keyword Analyzer:关键词分词器,不进行分词
- Pattern Analyzer:模式分词器,按正则表达式分割
- Language Analyzer:语言特定分词器,如英语、法语等
学习交流加群风哥微信: itpux-com
Part02-生产环境规划与建议
2.1 分词器选择原则
选择分词器时应考虑以下因素:
- 语言类型:不同语言需要不同的分词策略
- 业务需求:搜索精度 vs 召回率
- 性能要求:分词速度 vs 质量
- 维护成本:内置分词器 vs 自定义分词器
2.2 中文分词器推荐
对于中文文本,推荐使用以下分词器:
- IK Analyzer:开源中文分词器,支持自定义词典
- HanLP Analyzer:基于HanLP的中文分词器
- Jieba Analyzer:基于Jieba的中文分词器
2.3 生产环境配置建议
生产环境中,分词器配置应注意:
- 合理设置分词器参数
- 定期更新词典
- 监控分词性能
- 进行分词效果测试
学习交流加群风哥QQ113257174
Part03-生产环境项目实施方案
3.1 IK分词器安装
安装IK分词器插件:
cd /es/app/elasticsearch-8.10.0
# 安装IK分词器插件
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v8.10.0/elasticsearch-analysis-ik-8.10.0.zip
# 执行
# 输出日志
-> Installing https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v8.10.0/elasticsearch-analysis-ik-8.10.0.zip
-> Downloading https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v8.10.0/elasticsearch-analysis-ik-8.10.0.zip
[=================================================] 100%
-> Installed analysis-ik
3.2 自定义分词器配置
创建自定义分词器:
curl -X PUT “http://192.168.1.10:9200/fgedu-articles” -H “Content-Type: application/json” -d ‘{
“settings”: {
“number_of_shards”: 5,
“number_of_replicas”: 2,
“analysis”: {
“analyzer”: {
“custom_ik_analyzer”: {
“type”: “custom”,
“tokenizer”: “ik_max_word”,
“filter”: [“lowercase”, “stop”]
}
}
}
},
“mappings”: {
“properties”: {
“title”: {
“type”: “text”,
“analyzer”: “custom_ik_analyzer”
},
“content”: {
“type”: “text”,
“analyzer”: “custom_ik_analyzer”
}
}
}
}’
# 执行
# 输出日志
{
“acknowledged”: true,
“shards_acknowledged”: true,
“index”: “fgedu-articles”
}
3.3 分词器测试实战
测试分词器效果:
curl -X POST “http://192.168.1.10:9200/_analyze” -H “Content-Type: application/json” -d ‘{
“analyzer”: “ik_max_word”,
“text”: “ElasticSearch是一个分布式搜索引擎”
}’
# 执行
# 输出日志
{
“tokens”: [
{
“token”: “elasticsearch”,
“start_offset”: 0,
“end_offset”: 13,
“type”: “ENGLISH”,
“position”: 0
},
{
“token”: “是”,
“start_offset”: 13,
“end_offset”: 14,
“type”: “CN_CHAR”,
“position”: 1
},
{
“token”: “一个”,
“start_offset”: 14,
“end_offset”: 16,
“type”: “CN_WORD”,
“position”: 2
},
{
“token”: “分布式”,
“start_offset”: 16,
“end_offset”: 19,
“type”: “CN_WORD”,
“position”: 3
},
{
“token”: “搜索”,
“start_offset”: 19,
“end_offset”: 21,
“type”: “CN_WORD”,
“position”: 4
},
{
“token”: “引擎”,
“start_offset”: 21,
“end_offset”: 23,
“type”: “CN_WORD”,
“position”: 5
}
]
}
更多学习教程公众号风哥教程itpux_com
Part04-生产案例与实战讲解
4.1 电商搜索分词配置
电商搜索场景的分词器配置:
curl -X PUT “http://192.168.1.10:9200/fgedu-products” -H “Content-Type: application/json” -d ‘{
“settings”: {
“number_of_shards”: 5,
“number_of_replicas”: 2,
“analysis”: {
“analyzer”: {
“product_analyzer”: {
“type”: “custom”,
“tokenizer”: “ik_smart”,
“filter”: [“lowercase”]
}
}
}
},
“mappings”: {
“properties”: {
“product_name”: {
“type”: “text”,
“analyzer”: “product_analyzer”,
“fields”: {
“keyword”: {
“type”: “keyword”
}
}
},
“description”: {
“type”: “text”,
“analyzer”: “product_analyzer”
}
}
}
}’
# 执行
# 输出日志
{
“acknowledged”: true,
“shards_acknowledged”: true,
“index”: “fgedu-products”
}
4.2 日志分析分词配置
日志分析场景的分词器配置:
curl -X PUT “http://192.168.1.10:9200/fgedu-logs” -H “Content-Type: application/json” -d ‘{
“settings”: {
“number_of_shards”: 3,
“number_of_replicas”: 1,
“analysis”: {
“analyzer”: {
“log_analyzer”: {
“type”: “custom”,
“tokenizer”: “whitespace”,
“filter”: [“lowercase”]
}
}
}
},
“mappings”: {
“properties”: {
“message”: {
“type”: “text”,
“analyzer”: “log_analyzer”
}
}
}
}’
# 执行
# 输出日志
{
“acknowledged”: true,
“shards_acknowledged”: true,
“index”: “fgedu-logs”
}
4.3 分词器性能调优
分词器性能调优:
curl -X POST “http://192.168.1.10:9200/_analyze” -H “Content-Type: application/json” -d ‘{
“analyzer”: “ik_smart”,
“text”: “这是一段测试文本,用于测试分词器性能。ElasticSearch是一个强大的搜索引擎,能够快速处理大量数据。”
}’
# 执行
# 输出日志
{
“tokens”: [
{
“token”: “这是”,
“start_offset”: 0,
“end_offset”: 2,
“type”: “CN_WORD”,
“position”: 0
},
{
“token”: “一段”,
“start_offset”: 2,
“end_offset”: 4,
“type”: “CN_WORD”,
“position”: 1
},
{
“token”: “测试”,
“start_offset”: 4,
“end_offset”: 6,
“type”: “CN_WORD”,
“position”: 2
},
{
“token”: “文本”,
“start_offset”: 6,
“end_offset”: 8,
“type”: “CN_WORD”,
“position”: 3
},
{
“token”: “用于”,
“start_offset”: 9,
“end_offset”: 11,
“type”: “CN_WORD”,
“position”: 4
},
{
“token”: “测试”,
“start_offset”: 11,
“end_offset”: 13,
“type”: “CN_WORD”,
“position”: 5
},
{
“token”: “分词器”,
“start_offset”: 13,
“end_offset”: 16,
“type”: “CN_WORD”,
“position”: 6
},
{
“token”: “性能”,
“start_offset”: 16,
“end_offset”: 18,
“type”: “CN_WORD”,
“position”: 7
},
{
“token”: “elasticsearch”,
“start_offset”: 19,
“end_offset”: 32,
“type”: “ENGLISH”,
“position”: 8
},
{
“token”: “是”,
“start_offset”: 32,
“end_offset”: 33,
“type”: “CN_CHAR”,
“position”: 9
},
{
“token”: “一个”,
“start_offset”: 33,
“end_offset”: 35,
“type”: “CN_WORD”,
“position”: 10
},
{
“token”: “强大”,
“start_offset”: 35,
“end_offset”: 37,
“type”: “CN_WORD”,
“position”: 11
},
{
“token”: “的”,
“start_offset”: 37,
“end_offset”: 38,
“type”: “CN_CHAR”,
“position”: 12
},
{
“token”: “搜索引擎”,
“start_offset”: 38,
“end_offset”: 42,
“type”: “CN_WORD”,
“position”: 13
},
{
“token”: “能够”,
“start_offset”: 43,
“end_offset”: 45,
“type”: “CN_WORD”,
“position”: 14
},
{
“token”: “快速”,
“start_offset”: 45,
“end_offset”: 47,
“type”: “CN_WORD”,
“position”: 15
},
{
“token”: “处理”,
“start_offset”: 47,
“end_offset”: 49,
“type”: “CN_WORD”,
“position”: 16
},
{
“token”: “大量”,
“start_offset”: 49,
“end_offset”: 51,
“type”: “CN_WORD”,
“position”: 17
},
{
“token”: “数据”,
“start_offset”: 51,
“end_offset”: 53,
“type”: “CN_WORD”,
“position”: 18
}
]
}
风哥提示:对于大规模索引,建议使用ik_smart分词器以提高性能
Part05-风哥经验总结与分享
5.1 分词器最佳实践
- 根据业务场景选择合适的分词器
- 合理配置分词器参数
- 使用自定义词典提高分词准确性
- 定期更新词典以适应新词汇
- 结合使用不同分词器以提高搜索效果
5.2 常见分词问题
- 分词效果不佳导致搜索结果不准确
- 分词速度慢影响索引性能
- 词典更新不及时导致新词汇无法正确分词
- 自定义分词器配置错误
- 分词器与业务需求不匹配
5.3 生产环境监控
- 监控分词器性能
- 定期测试分词效果
- 收集用户搜索反馈
- 持续优化分词策略
- 建立分词效果评估机制
from ElasticSearch视频:www.itpux.com
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
