ElasticSearch 分词器_elasticsearch ik分词器
文章目录
一、安装中文分词插件
IK Analysis for Elasticsearch
是开源社区比较流行的中文分词插件
官网:https://github.com/medcl/elasticsearch-analysis-ik
本来想用这两种方法安装:
.\\bin\\elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.4.2/elasticsearch-analysis-ik-5.4.2.zipbin\\elasticsearch-plugin install file:///C:\\Users\\jk\\Desktop\\elasticsearch-analysis-ik-5.4.2.zip
但是却总是报错:
ERROR: `elasticsearch` directory is missing in the plugin zip
后来只能用了这种方法:在Elasticsearch安装目录下的文件夹plugins中新建文件夹ik,将elasticsearch-analysis-ik-5.4.2.zip
解压到这里即可,其实官网里已经说明了低于5.5.1版本的用解压的方式安装了:
参考:Elasticsearch5.x安装IK分词器以及使用
Linux安装7.14.1版本:
下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases
[hadoop@node01 ~]$ elasticsearch-7.14.1/bin/elasticsearch-plugin install file:///mnt/elasticsearch-analysis-ik-7.14.1.zip
ik_smart
:会做最粗粒度的拆分
ik_max_word
:会将文本做最细粒度的拆分。
测试1:ik_smart
GET /_analyze{ \"analyzer\": \"ik_smart\", \"text\":\"中华人民共和国\"}
结果:
{ \"tokens\": [ { \"token\": \"中华人民共和国\", \"start_offset\": 0, \"end_offset\": 7, \"type\": \"CN_WORD\", \"position\": 0 } ]}
测试2:ik_max_word
GET /_analyze{ \"analyzer\": \"ik_max_word\", \"text\":\"中华人民共和国\"}
结果:
{ \"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 } ]}
GET /_analyze{ \"analyzer\": \"ik_max_word\", \"text\":\"I love you\"}结果:{ \"tokens\" : [ { \"token\" : \"i\", \"start_offset\" : 0, \"end_offset\" : 1, \"type\" : \"ENGLISH\", \"position\" : 0 }, { \"token\" : \"love\", \"start_offset\" : 2, \"end_offset\" : 6, \"type\" : \"ENGLISH\", \"position\" : 1 }, { \"token\" : \"you\", \"start_offset\" : 7, \"end_offset\" : 10, \"type\" : \"ENGLISH\", \"position\" : 2 } ]}
参考:https://blog.csdn.net/wenxindiaolong061/article/details/82562450
二、es内置的分词器:
- standard analyzer
- simple analyzer
- whitespace analyzer
- language analyzer(特定的语言的分词器)
例句:Set the shape to semi-transparent by calling set_trans(5)
不同分词器的分词结果:
- standard analyzer:set, the, shape, to, semi, transparent, by, calling, set_trans, 5(默认的是standard)
- simple analyzer:set, the, shape, to, semi, transparent, by, calling, set, trans
- whitespace analyzer:Set, the, shape, to, semi-transparent, by, calling, set_trans(5)
- language analyzer(特定的语言的分词器,比如说,english,英语分词器):set, shape, semi, transpar, call, set_tran, 5
分词器测试:
GET /_analyze{ \"analyzer\": \"standard\", \"text\":\"The 2 QUICK Brown-Foxes jumped over the lazy dog’s bone.\"}
结果:
\"tokens\" : [ { \"token\" : \"the\", \"start_offset\" : 0, \"end_offset\" : 3, \"type\" : \"\", \"position\" : 0 }, { \"token\" : \"2\", \"start_offset\" : 4, \"end_offset\" : 5, \"type\" : \"\", \"position\" : 1 }, { \"token\" : \"quick\", \"start_offset\" : 6, \"end_offset\" : 11, \"type\" : \"\", \"position\" : 2 }, { \"token\" : \"brown\", \"start_offset\" : 12, \"end_offset\" : 17, \"type\" : \"\", \"position\" : 3 }, { \"token\" : \"foxes\", \"start_offset\" : 18, \"end_offset\" : 23, \"type\" : \"\", \"position\" : 4 }, { \"token\" : \"jumped\", \"start_offset\" : 24, \"end_offset\" : 30, \"type\" : \"\", \"position\" : 5 }, { \"token\" : \"over\", \"start_offset\" : 31, \"end_offset\" : 35, \"type\" : \"\", \"position\" : 6 }, { \"token\" : \"the\", \"start_offset\" : 36, \"end_offset\" : 39, \"type\" : \"\", \"position\" : 7 }, { \"token\" : \"lazy\", \"start_offset\" : 40, \"end_offset\" : 44, \"type\" : \"\", \"position\" : 8 }, { \"token\" : \"dog’s\", \"start_offset\" : 45, \"end_offset\" : 50, \"type\" : \"\", \"position\" : 9 }, { \"token\" : \"bone\", \"start_offset\" : 51, \"end_offset\" : 55, \"type\" : \"\", \"position\" : 10 } ]}
可以看出是按照空格、非字母的方式对输入的文本进行了转换,比如对 Java 做了转小写,对一些停用词也没有去掉,比如 in,其中 token 为分词结果;start_offset
为起始偏移;end_offset
为结束偏移;position
为分词位置。可配置项
COPY{ \"settings\": { \"analysis\": { \"analyzer\": { \"my_english_analyzer\": { \"type\": \"standard\", \"max_token_length\": 5, \"stopwords\": \"_english_\" } } } }}
不同的 Analyzer 会有不同的分词结果,内置的分词器有以下几种,基本上内置的 Analyzer 包括 Language Analyzers 在内,对中文的分词都不够友好,中文分词需要安装其它 Analyzer
中文分词器最简单的是ik分词器,还有jieba分词,哈工大分词器等
参考:
【9种】ElasticSearch分词器详解,一文get!!!| 博学谷狂野架构师
[转]中英文停止词表(stopword)
三、拼音插件安装以及(IK+pinyin使用)
有时在淘宝搜索商品的时候,会发现使用汉字、拼音、或者拼音混合汉字都会出来想要的搜索结果,其实是通过拼音搜索插件实现的。
地址:https://github.com/infinilabs/analysis-pinyin/releases
选择对应的版本,版本与 ES 版本一致,建议直接下载编译后的 zip 包;若是下载源码包,则需要自己编码打包 mvn clean package
生成 zip 包。联网安装:elasticsearch-7.14.1/bin/elasticsearch-plugin install https://get.infini.cloud/elasticsearch/analysis-pinyin/7.14.1
使用自定义拼音分词器创建索引:
PUT /medcl/ { \"settings\" : { \"analysis\" : { \"analyzer\" : { \"pinyin_analyzer\" : { \"tokenizer\" : \"my_pinyin\" } }, \"tokenizer\" : { \"my_pinyin\" : { \"type\" : \"pinyin\", \"keep_separate_first_letter\" : false, \"keep_full_pinyin\" : true, \"keep_original\" : true, \"limit_first_letter_length\" : 16, \"lowercase\" : true, \"remove_duplicated_term\" : true } } } }}
测试:
POST medcl/_analyze{ \"analyzer\": \"pinyin_analyzer\", \"text\": \"刘德华\"}
结果:
{ \"tokens\" : [ { \"token\" : \"liu\", \"start_offset\" : 0, \"end_offset\" : 0, \"type\" : \"word\", \"position\" : 0 }, { \"token\" : \"刘德华\", \"start_offset\" : 0, \"end_offset\" : 0, \"type\" : \"word\", \"position\" : 0 }, { \"token\" : \"ldh\", \"start_offset\" : 0, \"end_offset\" : 0, \"type\" : \"word\", \"position\" : 0 }, { \"token\" : \"de\", \"start_offset\" : 0, \"end_offset\" : 0, \"type\" : \"word\", \"position\" : 1 }, { \"token\" : \"hua\", \"start_offset\" : 0, \"end_offset\" : 0, \"type\" : \"word\", \"position\" : 2 } ]}
配置 IK + pinyin 分词配置
settings 设置:
curl -XPUT \"http://localhost:9200/medcl/\" -d\'{\"index\": {\"analysis\": {\"analyzer\": {\"default\": {\"tokenizer\": \"ik_max_word\"},\"pinyin_analyzer\": {\"tokenizer\": \"shopmall_pinyin\"}},\"tokenizer\": {\"shopmall_pinyin\": {\"keep_joined_full_pinyin\": \"true\",\"keep_first_letter\": \"true\",\"keep_separate_first_letter\": \"false\",\"lowercase\": \"true\",\"type\": \"pinyin\",\"limit_first_letter_length\": \"16\",\"keep_original\": \"true\",\"keep_full_pinyin\": \"true\"}}}}}\'
创建 mapping:
curl -XPOST http://localhost:9200/medcl/folks/_mapping -d\'{\"folks\": {\"properties\": {\"name\": {\"type\": \"text\",\"analyzer\": \"ik_max_word\",\"include_in_all\": true,\"fields\": {\"pinyin\": {\"type\": \"text\",\"analyzer\": \"pinyin_analyzer\"}}}}}}\'
添加测试文档:
curl -XPOST http://localhost:9200/medcl/folks/ -d\'{\"name\":\"刘德华\"}\'curl -XPOST http://localhost:9200/medcl/folks/ -d\'{\"name\":\"中华人民共和国国歌\"}\'
拼音分词效果:
curl -XPOST \"http://localhost:9200/medcl/folks/_search?q=name.pinyin:liu\" curl -XPOST \"http://localhost:9200/medcl/folks/_search?q=name.pinyin:de\" curl -XPOST \"http://localhost:9200/medcl/folks/_search?q=name.pinyin:hua\" curl -XPOST \"http://localhost:9200/medcl/folks/_search?q=name.pinyin:ldh\"
中文分词测试:
curl -XPOST \"http://localhost:9200/medcl/folks/_search?q=name:刘\" curl -XPOST \"http://localhost:9200/medcl/folks/_search?q=name:刘德\"
注意:用户输入搜索内容,根据正则匹配分成中文、拼音、中文+拼音、中文+拼音+数字+特殊符号等情况进行搜索,如下:
- 若是汉字搜索,没有搜索结果,转化为拼音再搜索一次,按拼音搜索还是无结果,则按模糊搜索再搜一次,若是还无结果,可考虑推荐
- 若是拼音搜索,没有搜索结果,则按模糊搜索再搜一次
- 若是汉字+拼音搜索,暂且按拼音处理
- 拼音、数字、特殊字符,暂且按拼音处理
参考:elasticsearch拼音插件安装以及(IK+pinyin使用)