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分词器的更多相关文章

  1. 如何给Elasticsearch安装中文分词器IK

    安装Elasticsearch安装中文分词器IK的步骤: 1. 停止elasticsearch 2.2的服务 2. 在以下地址下载对应的elasticsearch-analysis-ik插件安装包(版 ...

  2. elasticsearch安装中文分词器插件smartcn

    原文:http://blog.java1234.com/blog/articles/373.html elasticsearch安装中文分词器插件smartcn elasticsearch默认分词器比 ...

  3. windows下elasticsearch安装ik分词器后无法启动

    windows下elasticsearch安装ik分词器后启动报如下图错误: 然后百度说是elasticsearch路径有空格,一看果然我的路径有空格,然后重新换个路径就好了.

  4. ElasticSearch安装中文分词器IKAnalyzer

    # ElasticSearch安装中文分词器IKAnalyzer  本篇主要讲解如何在ElasticSearch中安装中文分词器IKAnalyzer,拆分的每个词都是我们熟知的词语,从而建立词汇与文档 ...

  5. elasticsearch使用ansj分词器

    目前elasticsearch的版本已经更新到7.0以上了,不过由于客户需要5.2.2版本的elasticsearch,所以还是需要安装的,并且安装上ansj分词器.在部署ES的时候,采用容器的方式进 ...

  6. ElasticSearch 安装中文分词器

    1.安装中文分词器IK 下载地址:https://github.com/medcl/elasticsearch-analysis-ik 在线下载安装: elasticsearch-plugin.bat ...

  7. ElasticSearch安装中文分词器IK

    1.安装IK分词器,下载对应版本的插件,elasticsearch-analysis-ik中文分词器的开发者一直进行维护的,对应着elasticsearch的版本,所以选择好自己的版本即可.IKAna ...

  8. elasticsearch安装ik分词器(极速版)

    简介:下面讲有我已经打包并且编辑过的zip包,你可以在下面下载即可. 1.下载zip包.elasticsearch-analysis-ik-1.8.0.jar下面有附件链接[ik-安装包.zip],下 ...

  9. elasticsearch安装中文分词器

    1. 分词器的安装 ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/rele ...

随机推荐

  1. 微软新一代Surface,该怎么看?

    近日,微软在美国纽约发布了其全新一代产品——Surface 2和Surface Pro 2.如果留意微软官方商城的话,可以看到该产品现已全面开放预购.那么,这样一款产品到底怎么样?让我们来一个横向的对 ...

  2. 《mysql必知必会》学习_第9章_20180731_欢

    第九章,用正则表达式进行搜索. P52 select prod_name from products where prod_name regexp '1000' order by prod_name; ...

  3. MapReduce编程之wordcount

    实践 MapReduce编程之wordcount import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Fi ...

  4. Tarjan求缩点化强连通图

    Describe: 求一个有向图加多少条边可以变成一个强连通图 Solution: Tarjan缩点染色后,判断出度和入度,所有点的出度 = 0 的和 和 入度 = 0 的和的最大值即为所求. 缩点染 ...

  5. Python自动化开发 - 装饰器

    本节内容 一.装饰器导引 1.函数对象特性 2.扩展业务功能需求 3.各种解决方案 二.装饰器解析 1.装饰器基本概念 2.无参装饰器解析 一.装饰器导引 1.函数对象特性 #### 第一波 #### ...

  6. 在CentOS上安装GITLAB

    为什么要用gitlab? 方便地管理项目,设置用户权限. 参考 https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md 步 ...

  7. spring注解方式 idea报could not autowire

    删除项目的iml文件,然后mvn重新导入 reimport

  8. C#调用haskell遭遇Attempted to read or write protected memory

    1. Haskell的代码如下: 上面的代码中readMarkdown与writeHtmlString是pandoc中的函数,newString的作用是将String转换为IO CString. 2. ...

  9. ASP.Net Core 2.2 MVC入门到基本使用系列 (二)

    本教程会对基本的.Net Core 进行一个大概的且不会太深入的讲解, 在您看完本系列之后, 能基本甚至熟练的使用.Net Core进行Web开发, 感受到.Net Core的魅力. 本教程知识点大体 ...

  10. Python中super()的用法

    参考链接:https://www.cnblogs.com/shengulong/p/7892266.html super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是 ...