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 ...
随机推荐
- Pandas中DataFrame修改列名
Pandas中DataFrame修改列名:使用 rename df = pd.read_csv('I:/Papers/consumer/codeandpaper/TmallData/result01- ...
- eclipse 垃圾回收器,内存释放
http://zhangrong-0825-163-com.iteye.com/blog/7334071.如何在eclipse里修改web工程的访问路径,步骤如下: 点击web工程——>选择pr ...
- DBA_实践指南系列8_Oracle Erp R12数据维护模式Adadmin(案例)
2013-12-08 Created By BaoXinjian
- Linux调度器 - 进程优先级
一.前言 本文主要描述的是进程优先级这个概念.从用户空间来看,进程优先级就是nice value和scheduling priority,对应到内核,有静态优先级.realtime优先级.归一化优先级 ...
- Linux时间子系统(三) 用户空间接口函数
一.前言 从应用程序的角度看,内核需要提供的和时间相关的服务有三种: 1.和系统时间相关的服务.例如,在向数据库写入一条记录的时候,需要记录操作时间(何年何月何日何时). 2.让进程睡眠一段时间 3. ...
- #pragma pack
原文链接: http://www.cnblogs.com/s7vens/archive/2012/03/06/2382236.html pack 为 struct, union 和 class 等的成 ...
- python标准库介绍——5 re模块详解
== re 模块== "Some people, when confronted with a problem, think 'I know, I'll use regular expres ...
- mysql 5.6 grant授权的时候出现问题
mysql> grant select on huamu_licai.* to 'read'@'%' identified by password 'Abcd1234';ERROR 1827 ( ...
- 修改easyui的easyloader的默认css目录路径
easyloader默认情况下会使用js文件所在目录下的themes文件夹中的css,这里改成项目自定义的css文件夹. 首先找到: var m=src.match(/easyloader\.js(\ ...
- hdu 5289 Assignment(给一个数组,求有多少个区间,满足区间内的最大值和最小值之差小于k)
1.区间是一段的,不是断开的哟 2.代码是看着标程写的 3.枚举左端点,二分右端点流程: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L ...