对于索引可能最关系的就是分词了 一般对于es 来说默认的smartcn 但效果不是很好
一个是ik的,一个是mmseg的,下面分别介绍下两者的用法,其实都差不多的,先安装插件,命令行:
安装ik插件
plugin -install medcl/elasticsearch-analysis-ik/1.1.0
下载ik相关配置词典文件到config目录
cd config
wget http://github.com/downloads/medcl/elasticsearch-analysis-ik/ik.zip --no-check-certificate
unzip ik.zip
rm ik.zip
分词配置
ik分词配置,在elasticsearch.yml文件中加上
1
2
3
4
5
6
|
index:
analysis:
analyzer:
ik:
alias: [ik_analyzer]
type: org.elasticsearch.index.analysis.IkAnalyzerProvider
|
或
1
|
index.analysis.analyzer.ik.type : “ik”
|
安装mmseg插件:
bin/plugin -install medcl/elasticsearch-analysis-mmseg/1.1.0
下载相关配置词典文件到config目录
cd config
wget http://github.com/downloads/medcl/elasticsearch-analysis-mmseg/mmseg.zip --no-check-certificate
unzip mmseg.zip
rm mmseg.zip
mmseg分词配置,也是在在elasticsearch.yml文件中
1
2
3
4
5
6
|
index:
analysis:
analyzer:
mmseg:
alias: [news_analyzer, mmseg_analyzer]
type: org.elasticsearch.index.analysis.MMsegAnalyzerProvider
|
或
1
|
index.analysis.analyzer.default.type : "mmseg"
|
mmseg分词还有些更加个性化的参数设置如下
1
2
3
4
5
6
7
8
9
10
11
12
|
index:
analysis:
tokenizer:
mmseg_maxword:
type: mmseg
seg_type: "max_word"
mmseg_complex:
type: mmseg
seg_type: "complex"
mmseg_simple:
type: mmseg
seg_type: "simple"
|
这样配置完后插件安装完成,启动es就会加载插件。
定义mapping
在添加索引的mapping时就可以这样定义分词器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
{
"page":{
"properties":{
"title":{
"type":"string",
"indexAnalyzer":"ik",
"searchAnalyzer":"ik"
},
"content":{
"type":"string",
"indexAnalyzer":"ik",
"searchAnalyzer":"ik"
}
}
}
}
|
indexAnalyzer为索引时使用的分词器,searchAnalyzer为搜索时使用的分词器。
java mapping代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
XContentBuilder content = XContentFactory.jsonBuilder().startObject()
.startObject(
"page"
)
.startObject(
"properties"
)
.startObject(
"title"
)
.field(
"type"
,
"string"
)
.field(
"indexAnalyzer"
,
"ik"
)
.field(
"searchAnalyzer"
,
"ik"
)
.endObject()
.startObject(
"code"
)
.field(
"type"
,
"string"
)
.field(
"indexAnalyzer"
,
"ik"
)
.field(
"searchAnalyzer"
,
"ik"
)
.endObject()
.endObject()
.endObject()
.endObject()
|
测试分词可用调用下面api,注意indexname为索引名,随便指定一个索引就行了
http://localhost:9200/indexname/_analyze?analyzer=ik&text=测试elasticsearch分词器
附:
ik分词插件项目地址:https://github.com/medcl/elasticsearch-analysis-ik
mmseg分词插件项目地址:https://github.com/medcl/elasticsearch-analysis-mmseg
配置好的es版本,地址如下:https://github.com/medcl/elasticsearch-rtf
一、IK简介
IK Analyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包。从2006年12月推出1.0版开始, IKAnalyzer已经推出了4个大版本。最初,它是以开源项目Luence为应用主体的,结合词典分词和文法分析算法的中文分词组件。从3.0版本开 始,IK发展为面向Java的公用分词组件,独立于Lucene项目,同时提供了对Lucene的默认优化实现。在2012版本中,IK实现了简单的分词 歧义排除算法,标志着IK分词器从单纯的词典分词向模拟语义分词衍化。
IK Analyzer 2012特性:
1.采用了特有的“正向迭代最细粒度切分算法“,支持细粒度和智能分词两种切分模式;
2.在系统环境:Core2 i7 3.4G双核,4G内存,window 7 64位, Sun JDK 1.6_29 64位 普通pc环境测试,IK2012具有160万字/秒(3000KB/S)的高速处理能力。
3.2012版本的智能分词模式支持简单的分词排歧义处理和数量词合并输出。
4.采用了多子处理器分析模式,支持:英文字母、数字、中文词汇等分词处理,兼容韩文、日文字符
5.优化的词典存储,更小的内存占用。支持用户词典扩展定义。特别的,在2012版本,词典支持中文,英文,数字混合词语。
二、安装IK分词插件
假设读者已经安装好ES,如果没有的话,请参考ElasticSearch入门 —— 集群搭建。安装IK分词需要的资源可以从这里下载,整个安装过程需要三个步骤:
1、获取分词的依赖包
通过git clone https://github.com/medcl/elasticsearch-analysis-ik,下载分词器源码,然后进入下载目录,执行命令:mvn clean package,打包生成elasticsearch-analysis-ik-1.2.5.jar。将这个jar拷贝到ES_HOME/plugins/analysis-ik目录下面,如果没有该目录,则先创建该目录。
2、ik目录拷贝
将下载目录中的ik目录拷贝到ES_HOME/config目录下面。
3、分词器配置
打开ES_HOME/config/elasticsearch.yml文件,在文件最后加入如下内容:
index: analysis: analyzer: ik: alias: [ik_analyzer] type: org.elasticsearch.index.analysis.IkAnalyzerProvider ik_max_word: type: ik use_smart: false ik_smart: type: ik use_smart: true index.analysis.analyzer.default.type: ik
ok!插件安装已经完成,请重新启动ES,接下来测试ik分词效果啦!
三、ik分词测试
1、创建一个索引,名为index。
curl -XPUT http://localhost:9200/index
2、为索引index创建mapping。
curl -XPOST http://localhost:9200/index/fulltext/_mapping -d' { "fulltext": { "_all": { "analyzer": "ik" }, "properties": { "content": { "type" : "string", "boost" : 8.0, "term_vector" : "with_positions_offsets", "analyzer" : "ik", "include_in_all" : true } } } }'
3、测试
curl 'http://localhost:9200/index/_analyze?analyzer=ik&pretty=true' -d ' { "text":"世界如此之大" }'
显示结果如下:
{ "tokens" : [ { "token" : "text", "start_offset" : 4, "end_offset" : 8, "type" : "ENGLISH", "position" : 1 }, { "token" : "世界", "start_offset" : 11, "end_offset" : 13, "type" : "CN_WORD", "position" : 2 }, { "token" : "如此", "start_offset" : 13, "end_offset" : 15, "type" : "CN_WORD", "position" : 3 }, { "token" : "之大", "start_offset" : 15, "end_offset" : 17, "type" : "CN_WORD", "position" : 4 } ] }
总结:1.7.1 es 配置插件
1、复制ik文件夹到conf目录,
2、修改elasticsearch.yml。增加 index.analysis.analyzer.ik.type : "ik"
3. plugins 目录新建analysis-ik文件夹,把ik jar包放到这个文件夹里面
参考资料
https://github.com/medcl/elasticsearch-analysis-ik
欢迎转载,请注明出处:http://my.oschina.net/xiaohui249/blog/232784
http://blog.csdn.net/liuzhenfeng/article/details/39404435
http://my.oschina.net/sunzy/blog/195341