4:重启es,注意es中的每个节点都要进行上述配置。
自定义分词器
1:创建一个dic文件,编码格式必须为utf-8无BOM格式,每个词一行多个词需要换行。
2:将自定义的dic文件上传到/usr/local/elasticsearch-1.4.4/config/custom目录下
3:修改ik的配置文件/usr/local/elasticsearch-1.4.4/config/IKAnalyzer.cfg.xml,在其中指定自定义的dic文件。
4:重启es
analyzer
分词器使用的两个情形:
1,Index time analysis. 创建或者更新文档时,会对文档进行分词
2,Search time analysis. 查询时,对查询语句分词
指定查询时使用哪个分词器的方式有:
- 查询时通过analyzer指定分词器
- 创建index mapping时指定search_analyzer
索引时分词是通过配置 Index mapping中的每个字段的参数analyzer指定的。
# 不指定分词时,会使用默认的standard
PUT test_index
{
"mappings": {
"doc": {
"properties": {
"title":{
"type": "text",
"analyzer": "whitespace" #指定分词器,es内置有多种analyzer
}
}
}}}
注意:
明确字段是否需要分词,不需要分词的字段将type设置为keyword,可以节省空间和提高写性能。
_analyzer api
GET _analyze
{
"analyzer": "standard",
"text": "this is a test"
}
# 可以查看text的内容使用standard分词后的结果
设置analyzer
PUT test
{
"settings": {
"analysis": { #自定义分词器
"analyzer": { # 关键字
"my\_analyzer":{ # 自定义的分词器
"type":"standard", #分词器类型standard
"stopwords":"\_english\_" #standard分词器的参数,默认的stopwords是\\_none\_
}
}
}
},
"mappings": {
"doc":{
"properties": {
"my\_text":{
"type": "text",
"analyzer": "standard", # my_text字段使用standard分词器
"fields": {
"english":{ # my_text.english字段使用上面自定义得my_analyzer分词器
"type": "text",
"analyzer": "my\_analyzer"
}}}}}}}
POST test/_analyze
{
"field": "my\_text", # my_text字段使用的是standard分词器
"text": ["The test message."]
}
-------------->[the,test,message]
POST test/_analyze
{
"field": "my\_text.english", #my_text.english使用的是my_analyzer分词器
"text": ["The test message."]
}
------------>[test,message]
ES内置了很多种analyzer。比如:
standard 由以下组成
- tokenizer:Standard Tokenizer
- token filter:Standard Token Filter,Lower Case Token Filter,Stop Token
Filter
analyzer API测试 :
POST _analyze
{
"analyzer": "standard",
"text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}
whitespace 空格为分隔符
POST _analyze
{
"analyzer": "whitespace",
"text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}
--> [ The,2,QUICK,Brown-Foxes,jumped,over,the,lazy,dog's,bone. ]
simple
POST _analyze
{
"analyzer": "simple",
"text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}
---> [ the, quick, brown, foxes, jumped, over, the, lazy, dog, s, bone ]
**stop 默认stopwords用_english_ **
POST _analyze
{
"analyzer": "stop",
"text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}
-->[ quick, brown, foxes, jumped, over, lazy, dog, s, bone ]
可选参数:
# stopwords
# stopwords\_path
keyword 不分词的
POST _analyze
{
"analyzer": "keyword",
"text": ["The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."]
}
得到 "token": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone." 一条完整的语句
第三方analyzer插件—中文分词(ik分词器)
es内置很多分词器,但是对中文分词并不友好,例如使用standard分词器对一句中文话进行分词,会分成一个字一个字的。这时可以使用第三方的Analyzer插件,比如 ik、pinyin等。这里以ik为例
1,首先安装插件,重启es:
# bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.3.0/elasticsearch-analysis-ik-6.3.0.zip
# /etc/init.d/elasticsearch restart
2,使用示例:
GET _analyze
{
"analyzer": "ik\_max\_word",
"text": "你好吗?我有一句话要对你说呀。"
}
参考:github.com/medcl/elast…
还可以用内置的 character filter, tokenizer, token filter 组装一个analyzer(custom analyzer)
博客园首页新随笔联系管理订阅订阅随笔- 65 文章- 0 评论- 6
es的分词器analyzer
analyzer
分词器使用的两个情形:
1,Index time analysis. 创建或者更新文档时,会对文档进行分词
2,Search time analysis. 查询时,对查询语句分词
指定查询时使用哪个分词器的方式有:
- 查询时通过analyzer指定分词器
View Code
- 创建index mapping时指定search_analyzer
View Code
索引时分词是通过配置 Index mapping中的每个字段的参数analyzer指定的
按 Ctrl+C 复制代码
不指定分词时,会使用默认的standard
PUT test_index
{
“mappings”: {
“doc”: {
“properties”: {
“title”:{
“type”: “text”,
“analyzer”: “whitespace” #指定分词器,es内置有多种analyzer
}
}
}}}
按 Ctrl+C 复制代码
注意:
明确字段是否需要分词,不需要分词的字段将type设置为keyword,可以节省空间和提高写性能。
_analyzer api
按 Ctrl+C 复制代码
GET _analyze
{
“analyzer”: “standard”,
“text”: “this is a test”
}
可以查看text的内容使用standard分词后的结果
按 Ctrl+C 复制代码
View Code
设置analyzer
按 Ctrl+C 复制代码
PUT test
{
“settings”: {
“analysis”: { #自定义分词器
“analyzer”: { # 关键字
“my_analyzer”:{ # 自定义的分词器
“type”:“standard”, #分词器类型standard
“stopwords”:“english” #standard分词器的参数,默认的stopwords是_none_
}
}
}
},
“mappings”: {
“doc”:{
“properties”: {
“my_text”:{
“type”: “text”,
“analyzer”: “standard”, # my_text字段使用standard分词器
“fields”: {
“english”:{ # my_text.english字段使用上面自定义得my_analyzer分词器
“type”: “text”,
“analyzer”: “my_analyzer”
}}}}}}}
POST test/_analyze
{
“field”: “my_text”, # my_text字段使用的是standard分词器
“text”: [“The test message.”]
}
-------------->[the,test,message]
POST test/_analyze
{
“field”: “my_text.english”, #my_text.english使用的是my_analyzer分词器
“text”: [“The test message.”]
}
------------>[test,message]
按 Ctrl+C 复制代码
ES内置了很多种analyzer。比如:
standard 由以下组成
tokenizer:Standard Tokenizer
token filter:Standard Token Filter,Lower Case Token Filter,Stop Token Filter
按 Ctrl+C 复制代码
analyzer API测试 :
POST _analyze
{
“analyzer”: “standard”,
“text”: “The 2 QUICK Brown-Foxes jumped over the lazy dog’s bone.”
}
按 Ctrl+C 复制代码
得到结果:
View Code
whitespace 空格为分隔符
按 Ctrl+C 复制代码
POST _analyze
{
“analyzer”: “whitespace”,
“text”: “The 2 QUICK Brown-Foxes jumped over the lazy dog’s bone.”
}
–> [ The,2,QUICK,Brown-Foxes,jumped,over,the,lazy,dog’s,bone. ]
按 Ctrl+C 复制代码
simple
按 Ctrl+C 复制代码
POST analyze
{
“analyzer”: “simple”,
“text”: “The 2 QUICK Brown-Foxes jumped over the lazy dog’s bone.”
}
—> [ the, quick, brown, foxes, jumped, over, the, lazy, dog, s, bone ]
按 Ctrl+C 复制代码
stop 默认stopwords用_english
按 Ctrl+C 复制代码
POST _analyze
{
“analyzer”: “stop”,
“text”: “The 2 QUICK Brown-Foxes jumped over the lazy dog’s bone.”
}
–>[ quick, brown, foxes, jumped, over, lazy, dog, s, bone ]
可选参数:
stopwords
stopwords_path
按 Ctrl+C 复制代码
keyword 不分词的
按 Ctrl+C 复制代码
POST _analyze
{
“analyzer”: “keyword”,
“text”: [“The 2 QUICK Brown-Foxes jumped over the lazy dog’s bone.”]
}
得到 “token”: “The 2 QUICK Brown-Foxes jumped over the lazy dog’s bone.” 一条完整的语句
按 Ctrl+C 复制代码
第三方analyzer插件—中文分词(ik分词器)
es内置很多分词器,但是对中文分词并不友好,例如使用standard分词器对一句中文话进行分词,会分成一个字一个字的。这时可以使用第三方的Analyzer插件,比如 ik、pinyin等。这里以ik为例
1,首先安装插件,重启es:
按 Ctrl+C 复制代码
bin/elasticsearch-plugin install github.com/medcl/elast…
/etc/init.d/elasticsearch restart
按 Ctrl+C 复制代码
2,使用示例:
按 Ctrl+C 复制代码
GET _analyze
{
“analyzer”: “ik_max_word”,
“text”: “你好吗?我有一句话要对你说呀。”
}
按 Ctrl+C 复制代码
分词结果
参考:github.com/medcl/elast…
还可以用内置的 character filter, tokenizer, token filter 组装一个analyzer(custom analyzer)
custom 定制analyzer,由以下几部分组成
- 0个或多个e character filters
- 1个tokenizer
- 0个或多个 token filters
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
了解详情》docs.qq.com/doc/DSlVlZExWQ0FRSE9H