ElasticSearch中文分词器-IK分词器的使用


IK分词器的使用#

首先我们通过Postman发送GET请求查询分词效果

Copy
GET http://localhost:9200/_analyze { "text":"农业银行" } 

得到如下结果,可以发现es的默认分词器无法识别中文中农业银行这样的词汇,而是简单的将每个字拆完分为一个词,这显然不符合我们的使用要求。

Copy
{
    "tokens": [ { "token": "农", "start_offset": 0, "end_offset": 1, "type": "<IDEOGRAPHIC>", "position": 0 }, { "token": "业", "start_offset": 1, "end_offset": 2, "type": "<IDEOGRAPHIC>", "position": 1 }, { "token": "银", "start_offset": 2, "end_offset": 3, "type": "<IDEOGRAPHIC>", "position": 2 }, { "token": "行", "start_offset": 3, "end_offset": 4, "type": "<IDEOGRAPHIC>", "position": 3 } ] } 

首先我们访问 https://github.com/medcl/elasticsearch-analysis-ik/releases 下载与es对应版本的中文分词器。将解压后的后的文件夹放入es根目录下的plugins目录下,重启es即可使用。

我们这次加入新的参数"analyzer":"ik_max_word"

  • k_max_word:会将文本做最细粒度的拆分,例如「中华人民共和国国歌」会被拆分为「中华人民共和国、中华人民、中华、华人、人民共和国、人民、人、民、共和国、共和、和、国国、国歌」,会穷尽各种可能的组合
  • ik_smart:会将文本做最粗粒度的拆分,例如「中华人民共和国国歌」会被拆分为「中华人民共和国、国歌」
Copy
GET http://localhost:9200/_analyze { "analyzer":"ik_max_word", "text":"农业银行" } 

得到如下结果

Copy
{
    "tokens": [ { "token": "农业银行", "start_offset": 0, "end_offset": 4, "type": "CN_WORD", "position": 0 }, { "token": "农业", "start_offset": 0, "end_offset": 2, "type": "CN_WORD", "position": 1 }, { "token": "银行", "start_offset": 2, "end_offset": 4, "type": "CN_WORD", "position": 2 } ] } 

百度搜索中每天都会收录新的词汇,es中也可以进行扩展词汇。

我们首先查询弗雷尔卓德字段

Copy
GET http://localhost:9200/_analyze { "analyzer":"ik_max_word", "text":"弗雷尔卓德" } 

仅仅可以得到每个字的分词结果,我们需要做的就是使分词器识别到弗雷尔卓德也是一个词语。

Copy
{
    "tokens": [ { "token": "弗", "start_offset": 0, "end_offset": 1, "type": "CN_CHAR", "position": 0 }, { "token": "雷", "start_offset": 1, "end_offset": 2, "type": "CN_CHAR", "position": 1 }, { "token": "尔", "start_offset": 2, "end_offset": 3, "type": "CN_CHAR", "position": 2 }, { "token": "卓", "start_offset": 3, "end_offset": 4, "type": "CN_CHAR", "position": 3 }, { "token": "德", "start_offset": 4, "end_offset": 5, "type": "CN_CHAR", "position": 4 } ] } 

首先进入es根目录中的plugins文件夹下的ik文件夹,进入config目录,创建custom.dic文件,写入弗雷尔卓德。同时打开IKAnalyzer.cfg文件,将新建的custom.dic配置其中,重启es。

Copy
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>IK Analyzer 扩展配置</comment> <!--用户可以在这里配置自己的扩展字典 --> <entry key="ext_dict">custom.doc</entry> <!--用户可以在这里配置自己的扩展停止词字典--> <entry key="ext_stopwords"></entry> <!--用户可以在这里配置远程扩展字典 --> <!-- <entry key="remote_ext_dict">words_location</entry> --> <!--用户可以在这里配置远程扩展停止词字典--> <!-- <entry key="remote_ext_stopwords">words_location</entry> --> </properties> 

再次查询发现es的分词器可以识别到弗雷尔卓德词汇

Copy
{
    "tokens": [ { "token": "弗雷尔卓德", "start_offset": 0, "end_offset": 5, "type": "CN_WORD", "position": 0 }, { "token": "弗雷尔", "start_offset": 0, "end_offset": 3, "type": "CN_WORD", "position": 1 }, { "token": "卓", "start_offset": 3, "end_offset": 4, "type": "CN_CHAR", "position": 2 }, { "token": "德", "start_offset": 4, "end_offset": 5, "type": "CN_CHAR", "position": 3 } ] }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM