使用 Elasticsearch ik分词实现同义词搜索(转)

1、首先需要安装好Elasticsearch 和elasticsearch-analysis-ik分词器

2、配置ik同义词

 

Elasticsearch 自带一个名为 synonym 的同义词 filter。为了能让 IK 和 synonym 同时工作,我们需要定义新的 analyzer,用 IK 做 tokenizer,synonym 做 filter。听上去很复杂,实际上要做的只是加一段配置。

打开 /config/elasticsearch.yml 文件,加入以下配置:

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. index:  
  2.   analysis:  
  3.     analyzer:  
  4.       ik_syno:  
  5.           type: custom  
  6.           tokenizer: ik_max_word  
  7.           filter: [my_synonym_filter]  
  8.       ik_syno_smart:  
  9.           type: custom  
  10.           tokenizer: ik_smart  
  11.           filter: [my_synonym_filter]  
  12.     filter:  
  13.       my_synonym_filter:  
  14.           type: synonym  
  15.           synonyms_path: analysis/synonym.txt  

以上配置定义了 ik_syno 和 ik_syno_smart 这两个新的 analyzer,分别对应 IK 的 ik_max_word 和 ik_smart 两种分词策略。根据 IK 的文档,二者区别如下:

  • ik_max_word:会将文本做最细粒度的拆分,例如「中华人民共和国国歌」会被拆分为「中华人民共和国、中华人民、中华、华人、人民共和国、人民、人、民、共和国、共和、和、国国、国歌」,会穷尽各种可能的组合;
  • ik_smart:会将文本做最粗粒度的拆分,例如「中华人民共和国国歌」会被拆分为「中华人民共和国、国歌」;

ik_syno 和 ik_syno_smart 都会使用 synonym filter 实现同义词转换。

3、创建/config/analysis/synonym.txt 文件,输入一些同义词并存为 utf-8 格式。例如

到此同义词配置已经完成,重启ES即可,搜索时指定分词为ik_syno或ik_syno_smart。

创建Mapping映射。执行curl命令如下

 

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. curl -XPOST  http://192.168.1.99:9200/goodsindex/goods/_mapping -d'{  
  2.   "goods": {  
  3.     "_all": {  
  4.       "enabled": true,  
  5.       "analyzer": "ik_max_word",  
  6.       "search_analyzer": "ik_max_word",  
  7.       "term_vector": "no",  
  8.       "store": "false"  
  9.     },  
  10.     "properties": {  
  11.       "title": {  
  12.         "type": "string",  
  13.         "term_vector": "with_positions_offsets",  
  14.         "analyzer": "ik_syno",  
  15.         "search_analyzer": "ik_syno"  
  16.       },  
  17.       "content": {  
  18.         "type": "string",  
  19.         "term_vector": "with_positions_offsets",  
  20.         "analyzer": "ik_syno",  
  21.         "search_analyzer": "ik_syno"  
  22.       },  
  23.       "tags": {  
  24.         "type": "string",  
  25.         "term_vector": "no",  
  26.         "analyzer": "ik_syno",  
  27.         "search_analyzer": "ik_syno"  
  28.       },  
  29.       "slug": {  
  30.         "type": "string",  
  31.         "term_vector": "no"  
  32.       },  
  33.       "update_date": {  
  34.         "type": "date",  
  35.         "term_vector": "no",  
  36.         "index": "no"  
  37.       }  
  38.     }  
  39.   }  
  40. }'  

以上代码为 test 索引下的 article 类型指定了字段特征: title 、 content 和 tags 字段使用 ik_syno 做为 analyzer,说明它使用 ik_max_word 做为分词,并且应用 synonym 同义词策略; slug 字段没有指定 analyzer,说明它使用默认分词;而 update_date 字段则不会被索引。

posted @ 2016-08-06 18:27  sandea  阅读(4762)  评论(0编辑  收藏  举报