ElasticSearch自定义分析器-集成结巴分词插件
关于结巴分词 ElasticSearch 插件:
https://github.com/huaban/elasticsearch-analysis-jieba
该插件由huaban开发。支持Elastic Search 版本<=2.3.5。
结巴分词分析器
结巴分词插件提供3个分析器:jieba_index、jieba_search和jieba_other。
- jieba_index: 用于索引分词,分词粒度较细;
- jieba_search: 用于查询分词,分词粒度较粗;
- jieba_other: 全角转半角、大写转小写、字符分词;
使用jieba_index或jieba_search分析器,可以实现基本的分词效果。
以下是最小配置示例:
{
"mappings": {
"test": {
"_all": {
"enabled": false
},
"properties": {
"name": {
"type": "string",
"analyzer": "jieba_index",
"search_analyzer": "jieba_index"
}
}
}
}
}
在生产化境中,因为业务的需要,需要考虑实现以下功能:
- 支持同义词;
- 支持字符过滤器;
结巴插件提供的分析器jieba_index、jieba_search无法实现以上功能。
自定义分析器
当jieba_index、jieba_search分析器不满足生成环境的需求时,我们可以使用自定义分析器来解决以上问题。
分析器是由字符过滤器,分词器,词元过滤器组成的。
一个分词器允许包含多个字符过滤器+一个分词器+多个词元过滤器。
因业务的需求,我们需要使用映射字符过滤器来实现分词前某些字符串的替换操作。如将用户输入的c#替换为csharp,c++替换为cplus。
下面逐一介绍分析器各个组成部分。
1. 映射字符过滤器Mapping Char Filter
这个是Elastic Search内置的映射字符过滤器,位于settings –> analysis -> char_filter下:
PUT /my_index
{
"settings": {
"analysis": {
"char_filter": {
"mapping_filter": {
"type": "mapping",
"mappings": [
"c# => csharp",
"c++ => cplus"
]
}
}
}
}
}
也可以通过文件载入字符映射表。
PUT /my_index
{
"settings": {
"analysis": {
"char_filter": {
"mapping_filter": {
"type": "mapping",
"mappings_path": "mappings.txt"
}
}
}
}
}
文件默认存放config目录下,即config/ mappings.txt。
2. 结巴分词词元过滤器JiebaTokenFilter
JiebaTokenFilter接受一个SegMode参数,该参数有两个可选值:Index和Search。
我们预先定义两个词元过滤器:jieba_index_filter和jieba_search_filter。
PUT /my_index
{
"settings": {
"analysis": {
"filter": {
"jieba_index_filter": {
"type": "jieba",
"seg_mode": "index"
},
"jieba_search_filter": {
"type": "jieba",
"seg_mode": "search"
}
}
}
}
}
这两个词元过滤器将分别用于索引分析器和查询分析器。
3. stop 停用词词元过滤器
因分词词元过滤器JiebaTokenFilter并不处理停用词。因此我们在自定义分析器时,需要定义停用词词元过滤器来处理停用词。
Elastic Search提供了停用词词元过滤器,我们可以这样来定义:
PUT /my_index
{
"settings": {
"analysis": {
"filter": {
"stop_filter": {
"type": "stop",
"stopwords": ["and", "is", "the"]
}
}
}
}
}
也可以通过文件载入停用词列表
PUT /my_index
{
"settings": {
"analysis": {
"filter": {
"stop_filter": {
"type": "stop",
"stopwords_path": "stopwords.txt"
}
}
}
}
}
文件默认存放config目录下,即config/ stopwords.txt。
4. synonym 同义词词元过滤器
我们使用ElasticSearch内置同义词词元过滤器来实现同义词的功能。
PUT /my_index
{
"settings": {
"analysis": {
"filter": {
"synonym_filter": {
"type": "synonym",
"stopwords": [
"中文,汉语,汉字"
]
}
}
}
}
}
如果同义词量比较大时,推荐使用文件的方式载入同义词库。
PUT /my_index
{
"settings": {
"analysis": {
"filter": {
"synonym_filter ": {
"type": "synonym",
"stopwords_path": "synonyms.txt"
}
}
}
}
}
5. 重新定义分析器jieba_index和jieba_search
Elastic Search支持多级分词,我们使用whitespace分词作为分词器;并在词元过滤器加入定义好的Jiebie分词词元过滤器:jieba_index_filter和jieba_search_filter。
PUT /my_index
{
"settings": {
"analysis": {
"analyzer": {
"jieba_index": {
"char_filter": [
"mapping_filter"
],
"tokenizer": "whitespace",
"filter": [
"jieba_index_filter",
"stop_filter",
"synonym_filter"
]
},
"jieba_search": {
"char_filter": [
"mapping_filter"
],
"tokenizer": "whitespace",
"filter": [
"jieba_search_filter",
"stop_filter",
"synonym_filter"
]
}
}
}
}
}
注意,上面分析器的命名依然使用jieba_index和jieba_search,以便覆盖结巴分词插件提供的分析器。
当存在多个同名的分析器时,Elastic Search会优先使用索引配置中定义的分析器。
这样在代码调用层面便无需再更改。
下面是完整的配置:
PUT /my_index
{
"settings": {
"analysis": {
"char_filter": {
"mapping_filter": {
"type": "mapping",
"mappings_path": "mappings.txt"
}
}
"filter": {
"synonym_filter ": {
"type": "synonym",
"stopwords_path": "synonyms.txt"
},
"stop_filter": {
"type": "stop",
"stopwords_path": "stopwords.txt"
},
"jieba_index_filter": {
"type": "jieba",
"seg_mode": "index"
},
"jieba_search_filter": {
"type": "jieba",
"seg_mode": "search"
}
}
"analyzer": {
"jieba_index": {
"char_filter": [
"mapping_filter"
],
"tokenizer": "whitespace",
"filter": [
"jieba_index_filter",
"stop_filter",
"synonym_filter"
]
},
"jieba_search": {
"char_filter": [
"mapping_filter"
],
"tokenizer": "whitespace",
"filter": [
"jieba_search_filter",
"stop_filter",
"synonym_filter"
]
}
}
}
}
}
参考资料:
https://www.elastic.co/guide/en/elasticsearch/reference/2.3/index.html
http://www.tuicool.com/articles/eUJJ3qF
ElasticSearch自定义分析器-集成结巴分词插件的更多相关文章
- 在ElasticSearch中使用 IK 中文分词插件
我这里集成好了一个自带IK的版本,下载即用, https://github.com/xlb378917466/elasticsearch5.2.include_IK 添加了IK插件意味着你可以使用ik ...
- Elasticsearch自定义分析器
关于分析器 ES中默认使用的是标准分析器(standard analyzer).如果需要对某个字段使用其他分析器,可以在映射中该字段下说明.例如: PUT /my_index { "mapp ...
- Simple: SQLite3 中文结巴分词插件
一年前开发 simple 分词器,实现了微信在两篇文章中描述的,基于 SQLite 支持中文和拼音的搜索方案.具体背景参见这篇文章.项目发布后受到了一些朋友的关注,后续也发布了一些改进,提升了项目易用 ...
- python jieba分词(结巴分词)、提取词,加载词,修改词频,定义词库 -转载
转载请注明出处 “结巴”中文分词:做最好的 Python 中文分词组件,分词模块jieba,它是python比较好用的分词模块, 支持中文简体,繁体分词,还支持自定义词库. jieba的分词,提取关 ...
- elasticsearch之分词插件使用
elasticsearch对英文会拆成单个单词,对中文会拆分成单个字.下面来看看是不是这样. 首先测试一下英文: GET /blog/_analyze { "text": &quo ...
- Elasticsearch 自定义多个分析器
分析器(Analyzer) Elasticsearch 无论是内置分析器还是自定义分析器,都由三部分组成:字符过滤器(Character Filters).分词器(Tokenizer).词元过滤器(T ...
- ElasticSearch(三) ElasticSearch中文分词插件IK的安装
正因为Elasticsearch 内置的分词器对中文不友好,会把中文分成单个字来进行全文检索,所以我们需要借助中文分词插件来解决这个问题. 一.安装maven管理工具 Elasticsearch 要使 ...
- Elasticsearch如何安装中文分词插件ik
elasticsearch-analysis-ik 是一款中文的分词插件,支持自定义词库. 安装步骤: 1.到github网站下载源代码,网站地址为:https://github.com/medcl/ ...
- Windows10安装Elasticsearch IK分词插件
安装插件 cmd切换到Elasticsearch安装目录下 C:\Users\Administrator>D: D:\>cd D:\Program Files\Elastic\Elasti ...
随机推荐
- 解决IIS动态内容压缩的问题
转:http://www.cnblogs.com/cmt/archive/2013/03/10/iis-dynamic-dompression-mime.html
- .NET CORE WebApi Post跨域提交
参考博客:https://www.cnblogs.com/chongyao/p/8652743.html 搭建一个 .NET CORE 的 WebApi 想前后端分离, 于是为了简单做了个demo站点 ...
- Asp.net 用户控件和自定义控件注册
在ASPX页中注册用户控件的方法 <%@ Register Src="ListPicker.ascx" TagName="ListPicker" Tag ...
- Tomcat7 catalina.out 日志切割
安装步骤例如以下: 下载(最新版本号) # wget http://cronolog.org/download/cronolog-1.6.2.tar.gz 假设下载不了,直接网上查找,ftp ...
- STM32 可编程电压监测器(PVD)实现数据掉电保存
STM32内部有一个完整的上电复位和掉电复位电路,当供电电压达到2v时系统即能正常工作. STM32内部自带PVD功能,用于对MCU供电电压VDD进行监控.通过电源控制寄存器中的PLS[2:0]位可以 ...
- 强制删除一个Windows服务
一个挂起的服务如下图所示,该服务相关的所有按钮都被禁用,包括启动.停止.暂停和恢复. 要停止这个服务,首先记住这个服务的名称,在这里是 ‘EntropySoftCFS’. 然后打开命令行窗口,运行 s ...
- Disable Oracle Automatic Jobs
By default, Oracle will run some maintance jobs every night. If you don't want to run those jobs, yo ...
- [svc][op]ssh交互yes问题解决-expect
Expect是Unix系统中用来进行自动化控制和测试的软件工具C67默认未安装:使用需要安装: yum install expect -ywhich expect #查看安装路径 核心命令: [roo ...
- [sh]sed 4个功能
[root@lanny test]# cat test.txt test liyao lanny 经典博文: http://oldboy.blog.51cto.com/2561410/949365 h ...
- android即时消息处理机制
在android端做即时消息的时候.遇到的坑点是怎么保证消息即时性,又不耗电.为什么这么说呢? 原因是假设要保证消息即时性.通常有两种机制pull或者push. pull定时轮询机制,比較浪 ...