需要组合Elastic Search的中文分词,拼音,自动补全,错别字或者英文拼写错误等功能。还要支持热点词。
IK中文分词
安装
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.5.3/elasticsearch-analysis-ik-5.5.3.zip # 需要安装和es对应一致的IK版本。
配置
ik的配置文件位置在 es/config/analysis-ik
vim IKAnalyzer.cfg.xml
1 | <?xml version="1.0" encoding="UTF-8"?> |
基本操作
测试分词器效果
- ik_max_word: 尽量多的分词
1 | curl -XGET 'http://localhost:9200/_analyze/?pretty' -d '{ |
- 返回值如下
{
"tokens" : [
{
"token" : "中华人民共和国",
"start_offset" : 0,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "中华人民",
"start_offset" : 0,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "中华",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "华人",
"start_offset" : 1,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 3
},
{
"token" : "人民共和国",
"start_offset" : 2,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 4
},
{
"token" : "人民",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 5
},
{
"token" : "共和国",
"start_offset" : 4,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 6
},
{
"token" : "共和",
"start_offset" : 4,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 7
},
{
"token" : "国",
"start_offset" : 6,
"end_offset" : 7,
"type" : "CN_CHAR",
"position" : 8
},
{
"token" : "国歌",
"start_offset" : 7,
"end_offset" : 9,
"type" : "CN_WORD",
"position" : 9
}
]
}
- ik_smart智能分词
1 | curl -XGET 'http://localhost:9200/_analyze/?pretty' -d '{ |
- 返回值
{
"tokens" : [
{
"token" : "中华人民共和国",
"start_offset" : 0,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "国歌",
"start_offset" : 7,
"end_offset" : 9,
"type" : "CN_WORD",
"position" : 1
}
]
}
创建一个使用中文分词器的索引
查看当前索引
curl -XGET ‘localhost:9200/_cat/indices?v&pretty’ # 查看当前索引
创建空索引
curl -XPUT ‘localhost:9200/zhongwen/?pretty’
设置映射类型
1 | curl -XPOST 'localhost:9200/zhongwen/news/_mapping?pretty' -d '{ |
测试验证
基本测试
1 | curl -XGET 'http://localhost:9200/pinyin_plug_test_index/_analyze?pretty' -d ' |
全部 TODO 参考官方文档 https://github.com/medcl/elasticsearch-analysis-pinyin
自动补全
热点词
错别字纠错插件
整合pinyin及ik中文
安装及配置
参见前文
索引创建及操作
创建索引
1 | curl -XPUT 'http://localhost:9200/ik_plus_pinyin/?pretty' -d ' |
创建type的mapping
1 | curl -XPOST "http://localhost:9200/ik_plus_pinyin/keyword/_mapping?pretty" -d ' |
基本操作
构造数据
curl -XPOST http://localhost:9200/ik_plus_pinyin/keyword/?pretty -d’{“name1”:”刘德华”}’
curl -XPOST http://localhost:9200/ik_plus_pinyin/keyword/?pretty -d’{“name1”:”中华人民共和国国歌”}’
构造测试
构造一个带权重的测试
- 组合了各种情况,拼音,首字母及汉字混合的情况。可以将query字段取值为
[“ldh”, “ldehua”, “liu”, “dehua”, “liu hua”, “hua”, “刘”, “德华”, “华”, “l德hua”]
1 | curl -XPOST 'http://localhost:9200/ik_plus_pinyin/keyword/_search?size=50&pretty' -d ' |
- 返回
1 | { |
版权声明
本文标题:78-ElasticSearch组合实现pinyin中文分词及自动补全
文章作者:盛领
发布时间:2018年10月29日 - 23:48:59
原始链接:http://blog.xiaoyuyu.net/post/dd5c815a.html
许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。
如您有任何商业合作或者授权方面的协商,请给我留言:sunsetxiao@126.com