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 ...
随机推荐
- xml2-config not found.
在Ubuntu下接着安装php时候,在configure后,又出现错误提示:error: xml2-config not found. Please check your libxml2 instal ...
- Tomcat JNDI + spring配置
http://hi.baidu.com/lzpsky/item/f9a727ba823257eb4ec7fd27 一.简介 JNDI : Java Naming and Directory Inter ...
- Accounting_会计基础
会计基础 1.会计:是以货币为主要计量单位,反映和监督一个单位经济活动的一种经济管理工作. 2.会计核算职能:指以货币为主要计量单位,通过确认.记录.计算.报告等环节,对特定主体的经济活动进行记账.算 ...
- ios学习之旅--oc对象的关系
1.匿名对象:就是没有名字对象 1.匿名对象仅用一次 使用场景: 1.当我们仅仅要调用一个对象的某个方法一次的时候能够使用匿名对象 2.匿名对象能够作为函数的实际參数 #imp ...
- Python 字典 keys() 方法
描述 Python 字典 keys() 方法以列表形式(并非直接的列表,若要返回列表值还需调用list函数)返回字典中的所有的键. 语法 keys() 方法语法: D.keys() 参数 无. 返回值 ...
- Spring Boot热部署(springloader)
使用方式一 在pom.xml文件添加依赖包: <plugin> <groupId>org.springframework.boot</groupId> <ar ...
- NodeJS操作Redis实现消息的发布与订阅
首先先说一下流程: 1.保存数据到Redis,然后将member值publish到 chat频道(publish.js功能) 2.readRedis.js文件此前一直在监听chat频道,readRed ...
- Mongodb 与 Mongoose 的使用
目标 无明确目标 知识点 了解 mongodb (http://www.mongodb.org/ ) 学习 mongoose 的使用 (http://mongoosejs.com/ ) 课程内容 mo ...
- cocos2dx当节点存在缩放时要注意的问题
cocos2dx(所有引擎也均如此),如果一个节点存在缩放,一定不要忘了其局部空间里的单位长度也会发生变化.其子节点位移,局部空间转世界空间结果等都会受影响. 有时候我们想将父节点的缩放转移到子节点中 ...
- 可以动态增加系统的U盘启动器(基于grub)
前言:最近面试无果,就在宿舍看那本<30天自制操作系统>,里面使用的系统文件格式是img,要在真机上运行,就需要使用U盘进行启动,因为现在都没有软盘.而网上很多都是用软件写入U盘的.反正我 ...