elasticsearch安装ansj分词器

摘要:
文本将以最细粒度的方式进行拆分和iked_smart:preserve_existing=true'-d'{“index.analysis.analysis.default.type”:“index.analyzer.default_search.type”

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"
}'

##设置停用词,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



参考地址:
https://github.com/NLPchina/elasticsearch-analysis-ansj
http://pathbox.github.io/work/2017/09/13/elasticsearch-5.5.2-install-and-config.html
https://stackoverflow.com/questions/19758335/error-when-trying-to-update-the-settings/24414375
https://www.elastic.co/blog/strict-content-type-checking-for-elasticsearch-rest-requests

免责声明:文章转载自《elasticsearch安装ansj分词器》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇algorand:可扩展的拜占庭协议C/C++语言参考手册下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

简单搭建 @vue-cli3.0 及常用sass使用

  1,在安装了Node.js后使用其中自带的包管理工具npm。或者使用淘宝镜像cnpm(这里不做说明) 1-1,下载vue3.0脚手架(如果之前装vue-cli3x之前的版本,先卸载 npm uninstall vue-cli -g) npm install -g @vue/cli 1-2,下载sass npm install node-sass -...

Web service 超过了最大请求长度

Web service的默认的请求长度是4M 当内容超过了4M会报错 System.Web.Services.Protocols.SoapException: 运行配置文件中指定的扩展时出现异常。 ---> System.Web.HttpException: 超过了最大请求长度。 在 System.Web.HttpRequest.GetEnti...

eslint在webstorm中有错误警告

1. 报错Missing space before function parentheses的问题   解决:在代码目录中,打开.eslint文件,并在rules中添加如下一行代码即可:      "space-before-function-paren": 0 2. 报错eslint: missing semicolon   解决:在rules中添加  ...

26.怎样在Swift中定义宏?

  Swift 中没有宏定义,苹果建议使用let 或者 get 属性来替代宏定义值。虽然没有#define,但我们仍然可以使用 #if 并配合编译的配置来完成条件编译。下面会列出Swift项目开发中的一些常用宏定义,并提供源码。 1.常用字体宏定义 import Foundation import UIKit /// 系统普通字体 var gof_Sy...

17个常用代码整理

本文由会员诸神的黄昏曲分享 1.判断邮箱格式是否正确的代码 //利用正则表达式验证 -(BOOL)isValidateEmail:(NSString *)email { NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}"; NSPredicate *emailT...

qt动画入门

Qt-4.6新增了Animation Framework(动画框架),让我们可以方便的写一些生动的程序。不必像曾经的版本号一样,全部的控件都枯燥的呆在伟大光荣的QLayout里,或许它们可以唱个歌,跳个舞。     所谓动画就是在一个时间段内的不同一时候间点有不同的状态。仅仅要定义好这样状态。实现动画就是水到渠成的事情。当然做这件事情,最好用的就是状态...