本文共 2509 字,大约阅读时间需要 8 分钟。
一个 tokenizer(分词器)接收一个字符流,将之割为独立的 tokens(词元,通常是独立的单词),然后输出 tokens流。
例如,whitespace tokenizer遇到空白字符时分割文。它会将文本 "Quick brown fox!“ 分割为 [Quick, brown, fox]。该 tokenizer(分词器)还负责记录各个term(词条)的顺序或 position 位置(用于 phrase短语和 word proximity 词近邻查询),以及term(词条)所代表的原始word(单词)的 start(起始)和end(结束)的 character offsets(字符偏移量)(用于高亮显示搜索的内容)。
ElasticSearch 提供了很多内置的分词器,可以用来构建 custom analyzers(自定义分词器)
一、分词预览查询
POST _analyze { "analyzer": "standard", "text": "Note however that storage is optimized based on the actual values that are stored" } 返回结果{
"tokens" : [ { "token" : "note", "start_offset" : 0, "end_offset" : 4, "type" : "<ALPHANUM>", "position" : 0 }, { "token" : "however", "start_offset" : 5, "end_offset" : 12, "type" : "<ALPHANUM>", "position" : 1 }, { "token" : "that", "start_offset" : 13, "end_offset" : 17, "type" : "<ALPHANUM>", "position" : 2 }, { "token" : "storage", "start_offset" : 18, "end_offset" : 25, "type" : "<ALPHANUM>", "position" : 3 }, ... 但是如果是中文场景下就会出现一些问题:POST _analyze
{ "analyzer": "standard", "text": "更改其他索引的字段的映射" } 返回结果如下,可见将每一个汉字进行了分割,明显不符合实际情况。{
"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 }, ... 二、安装 IK分词器 在es目录下的plugins目录下创建一个新文件夹,命名为ik,然后把上面的压缩包中的内容解压到该目录中。把解压出来的内容放到es/plugins/ik中。之后,需要重新启动es。
再次测试:
POST _analyze
{ "analyzer": "ik_smart", "text": "你是列文虎克吗" } 结果:{
"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" : 5, "type" : "CN_WORD", "position" : 3 }, { "token" : "克", "start_offset" : 5, "end_offset" : 6, "type" : "CN_CHAR", "position" : 4 }, { "token" : "吗", "start_offset" : 6, "end_offset" : 7, "type" : "CN_CHAR", "position" : 5 } ] } 三、自定义分词 从上面的例子中可以看到 列文虎克 被拆开了,因为ik分词器依旧不支持部分内容,我们可以自定义分词词库在elasticsearch-5.6.8\plugins\ik\config下
新增一个z_SelfAdd.dic文件,在里面加上新的单词,保存为UTF-8
然后在当前目录下的IKAnalyzer.cfg.xml配置文件中下加上<entry key="ext_dict">z_SelfAdd.dic</entry>
将刚才命名的文件加入
重启就生效了
———————————————— 版权声明:本文为CSDN博主「北鹤M」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/qq_20051535/article/details/113251848转载地址:https://blog.csdn.net/sinat_17775997/article/details/116118601 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!