elasticsearch安装ansj分词器
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分词器的更多相关文章
- 如何给Elasticsearch安装中文分词器IK
安装Elasticsearch安装中文分词器IK的步骤: 1. 停止elasticsearch 2.2的服务 2. 在以下地址下载对应的elasticsearch-analysis-ik插件安装包(版 ...
- elasticsearch安装中文分词器插件smartcn
原文:http://blog.java1234.com/blog/articles/373.html elasticsearch安装中文分词器插件smartcn elasticsearch默认分词器比 ...
- windows下elasticsearch安装ik分词器后无法启动
windows下elasticsearch安装ik分词器后启动报如下图错误: 然后百度说是elasticsearch路径有空格,一看果然我的路径有空格,然后重新换个路径就好了.
- ElasticSearch安装中文分词器IKAnalyzer
# ElasticSearch安装中文分词器IKAnalyzer 本篇主要讲解如何在ElasticSearch中安装中文分词器IKAnalyzer,拆分的每个词都是我们熟知的词语,从而建立词汇与文档 ...
- elasticsearch使用ansj分词器
目前elasticsearch的版本已经更新到7.0以上了,不过由于客户需要5.2.2版本的elasticsearch,所以还是需要安装的,并且安装上ansj分词器.在部署ES的时候,采用容器的方式进 ...
- ElasticSearch 安装中文分词器
1.安装中文分词器IK 下载地址:https://github.com/medcl/elasticsearch-analysis-ik 在线下载安装: elasticsearch-plugin.bat ...
- ElasticSearch安装中文分词器IK
1.安装IK分词器,下载对应版本的插件,elasticsearch-analysis-ik中文分词器的开发者一直进行维护的,对应着elasticsearch的版本,所以选择好自己的版本即可.IKAna ...
- elasticsearch安装ik分词器(极速版)
简介:下面讲有我已经打包并且编辑过的zip包,你可以在下面下载即可. 1.下载zip包.elasticsearch-analysis-ik-1.8.0.jar下面有附件链接[ik-安装包.zip],下 ...
- elasticsearch安装中文分词器
1. 分词器的安装 ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/rele ...
随机推荐
- Max Sum—hdu1003(简单DP) 标签: dp 2016-05-05 20:51 92人阅读 评论(0)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- Sensor
原理:http://blog.csdn.net/xiaolei05/article/details/18670161 1.Sensor Type 重力感应/加速度传感器 (G-Sensor ...
- 关于FPGA的一些小见解
Xilinx FPGA配置bit流文件 Xilinx FPGA的供电是采用USB作为电源,使用Verilog HDL或VHDL实现的逻辑电路通过Xilinx的综合工具生成bit流文件,通过Digile ...
- hdu 3030
这道题主要就是问你,长度为n的序列,有多少种上升的子序列 当前点的情况种数等于前面所有小于它的点的种数相加 + 1 1就是只有这一个点的时候的序列 那就是要多次查询前面比它小的点的种数的和 那么就是区 ...
- js-实现搜狐列表
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- [置顶] 获取网络数据中的数组显示成ListView的简单流程
首先说一下 这是我自己的个人笔记,如果想看看,不用看细节,可以看流程. 定义一个线程池 ExecutorService pool = Executors.newFixedThreadPool(15) ...
- spring boot mybatis sql打印到控制台
如何设置spring boot集成 mybatis 然后sql语句打印到控制台,方便调试: 设置方法: 在application.properties文件中添加: logging.level.com. ...
- 我的ecshop二次开发经验分享
https://jingyan.baidu.com/article/358570f65dbad2ce4724fcc7.html
- FastReport 打印模版页(TFrxReportpage)复制
遇到一个奇葩的需求.一般情况下我们打印单据,用FastReport设置打印格式,也就是就设一个模版页而己,就是一种单据格式.如果打印的单据数据多了就自动打印多页了,他们的格式是一样的.也就是读同一个模 ...
- Network in Network 2
<Network in Network>论文笔记 1.综述 这篇文章有两个很重要的观点: 1×1卷积的使用 文中提出使用mlpconv网络层替代传统的convolution层.mlp层实际 ...