elasticsearch安装ansj分词器

Posted by 冷眼樵夫 on 12-19,2019

1、概述

elasticsearch用于搜索引擎,需要设置一些分词器来优化索引。常用的有ik_max_word: 会将文本做最细粒度的拆分、ik_smart: 会做最粗粒度的拆分、ansj等。

ik下载地址: https://github.com/medcl/elasticsearch-analysis-ik/releases

ansj下载地址:https://github.com/NLPchina/elasticsearch-analysis-ansj

安装的时候一定要安装相对应的版本,并且解压完成后一定要报安装包移到其他目录或直接删除,plugins目录下只能包含分词器的目录。否则启动会报错,尤其是docker环境,如果没能映射plugins目录的话,就只能重新创建容器了。

本文以5.2版本为例,讲解安装ansj分词,并将其设置为默认分词器。注意5.x版本以后不再支持在elasticsearch.yml里面设置默认分词器,只能通过API的方式进行设置。

Since elasticsearch 5.x index level settings can NOT be set on the nodes
configuration like the elasticsearch.yaml, in system properties or command line
arguments.In order to upgrade all indices the settings must be updated via the
/${index}/_settings API. Unless all settings are dynamic all indices must be closed
in order to apply the upgradeIndices created in the future should use index templates
to set default values.

2、安装

#./bin/elasticsearch-plugin install https://github.com/NLPchina/elasticsearch-analysis-ansj/releases/download/v5.2.2/elasticsearch-analysis-ansj-5.2.2.0-release.zip

然后进行解压:

#unzip elasticsearch-analysis-ansj-5.2.2.0-release.zip && rm -rf elasticsearch-analysis-ansj-5.2.2.0-release.zip

#mv elasticsearch elasticsearch-analysis-ansj

重启服务,加载分词器设置。

设置为默认得分词器:

curl -XPUT 'http://localhost:9200/_all/_settings?preserve_existing=true' -d '{
"index.analysis.analyzer.default.type" : "index_ansj",
"index.analysis.analyzer.default_search.type" : "query_ansj"
}'

出现如下报错:
{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Can't update non dynamic settings [[index.analysis.analyzer.default_search.type]] for open indices

不支持动态设置,indecis处于开启状态,需要先关闭,在进行设置,设置完成后在打开。这种通过API设置的方式不需要重启elsatisearch。线上的集群最好不要重启,加载索引的时间会很久并且会引发一些错误。

curl -XPOST 'localhost:9200/_all/_close'

curl -XPUT 'http://localhost:9200/_all/_settings?preserve_existing=true' -d '{
"index.analysis.analyzer.default.type" : "index_ansj",
"index.analysis.analyzer.default_search.type" : "query_ansj"
}'

curl -XPOST 'localhost:9200/_all/_open'

注:6.x版本后执行put命令:
6.x版本以后修改或写入数据到es,都要使用-H'Content-Type: application/json'。参考地址:
https://www.elastic.co/blog/strict-content-type-checking-for-elasticsearch-rest-requests

curl -XPUT -H'Content-Type: application/json' 'http://localhost:9200/_all/_settings?preserve_existing=true' -d '{
"index.analysis.analyzer.default.type" : "index_ansj",
"index.analysis.analyzer.default_search.type" : "query_ansj"
}'

3、设置停用词,stopwords:

stopwords用来在搜索时被过滤掉。如设置stopwords为“老”,则在搜索时“老师”,只能搜索出“师”。
本文据一个去除空格的例子:
修改elasticsearch-analysis-ansj的配置文件:

cat ansj.cfg.yml

全局变量配置方式一
ansj:

#默认参数配置
isNameRecognition: true #开启姓名识别
isNumRecognition: true #开启数字识别
isQuantifierRecognition: true #是否数字和量词合并
isRealName: false #是否保留真实词语,建议保留false

#用户自定词典配置
dic: file://usr/share/elasticsearch/plugins/elasticsearch-analysis-ansj/default.dic #也可以写成 file://default.dic , 如果未配置dic,则此词典默认加载

#http方式加载

#dic_d1: http://xxx/xx.dic

#jar中文件加载

#dic_d2: jar://org.ansj.dic.DicReader|/dic2.dic

#从数据库中加载

#dic_d3: jdbc:mysql://xxxx:3306/ttt?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull|username|password|select name as name,nature,freq from dic where type=1

#从自定义类中加载,YourClas extends PathToStream

#dic_d3: class://xxx.xxx.YourClas|ohterparam

#过滤词典配置

#stop: http,file,jar,class,jdbc 都支持

#stop_key1: ...
stop: file://usr/share/elasticsearch/plugins/elasticsearch-analysis-ansj/stop.dic

#歧义词典配置

#ambiguity: http,file,jar,class,jdbc 都支持

#ambiguity_key1: ...

#同义词词典配置

#synonyms: http,file,jar,class,jdbc 都支持

#synonyms_key1: ...

#全局变量配置方式二 通过配置文件的方式配置,优先级高于es本身的配置

#ansj_config: ansj_library.properties #http,file,jar,class,jdbc 都支持,格式参见ansj_library.properties

#配置自定义分词器
index:
analysis:
tokenizer :
my_dic :
type : dic_ansj
dic: dic
stop: stop
ambiguity: ambiguity
synonyms: synonyms
isNameRecognition: true
isNumRecognition: true
isQuantifierRecognition: true
isRealName: false

analyzer:
my_dic:
type: custom
tokenizer: my_dic

添加stop: file://usr/share/elasticsearch/plugins/elasticsearch-analysis-ansj/stop.dic这样一行内容,然后在相应的位置创建stop.dic文件,字符编码为utf-8。
想要过滤空格需要使用正则表达式,编辑器将制表符翻译成空格,所以过滤空格的语法为:\s+[tab]regex,其中[tab]代表按一下tab键。即在stop.dic文件里面\s+和regex之间需要按一个tab键代表过滤空格。

#cat stop.dic
\s+ regex


0评论