ELK下es的分词器analyzer
转载链接 :es的分词器analyzerhttps://www.cnblogs.com/xiaobaozi-95/p/9328948.html
中文分词器
在lunix下执行下列命令,可以看到本来应该按照中文”北京大学”来查询结果es将其分拆为”北”,”京”,”大”,”学”四个汉字,这显然不符合我的预期。这是因为Es默认的是英文分词器我需要为其配置中文分词器。
#curl HTTP://192.168.79.131:9200/shb01/_analyze?pretty=true -d'{"text":"北京大学"}'
Es整合ik不直接用ik官网的工具包,需要将ik工具包封装成es插件才行,这个已经有人封装好了可以在github上下载elasticsearch-analysis-ik
1:在github上下载ik插件源码
https://github.com/medcl/elasticsearch-analysis-ik
2:下载后解压缩在根目录下使用maven对其进行编译。
编译后把target/release目录下的elasticsearch-analysis-ik-1.3.0.zip上传到/usr/local/elasticsearch-1.4.4/plugins/analysis-ik目录下然后使用unzip解压。
把下载的ik插件中config目录下的文件拷贝到/usr/local/elasticsearch-1.4.4/config目录下,这些文件时ik的配置文件,custom是自定义词库文件。
3:修改elasticsearch.yml文件,把ik分词器设置为es的默认分词器
index.analysis.analyzer.default.type:ik
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": "你好吗?我有一句话要对你说呀。"
}
参考:https://github.com/medcl/elasticsearch-analysis-ik
还可以用内置的 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 https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.3.0/elasticsearch-analysis-ik-6.3.0.zip
/etc/init.d/elasticsearch restart
按 Ctrl+C 复制代码
2,使用示例:
按 Ctrl+C 复制代码
GET _analyze
{
“analyzer”: “ik_max_word”,
“text”: “你好吗?我有一句话要对你说呀。”
}
按 Ctrl+C 复制代码
分词结果
参考:https://github.com/medcl/elasticsearch-analysis-ik
还可以用内置的 character filter, tokenizer, token filter 组装一个analyzer(custom analyzer)
custom 定制analyzer,由以下几部分组成
- 0个或多个e character filters
- 1个tokenizer
- 0个或多个 token filters
自定义分词器
自定义分词需要在索引的配置中设定,如下所示 :
PUT test_index
{"settings": {"analysis": { # 分词设置,可以自定义"char_filter": {}, #char_filter 关键字"tokenizer": {}, #tokenizer 关键字"filter": {}, #filter 关键字"analyzer": {} #analyzer 关键字}}
}
character filter 在tokenizer之前对原始文本进行处理,比如增加,删除,替换字符等
会影响后续tokenizer解析的position和offset信息
html strip 除去html标签和转换html实体
参数:escaped_tags不删除的标签mapping 映射类型,以下参数必须二选一
mappings 指定一组映射,每个映射格式为 key=>value
mappings_path 绝对路径或者相对于config路径 key=>value
PUT t_index
{"settings": {"analysis": {"analyzer": { #关键字"my_analyzer":{ #自定义分词器"tokenizer":"standard","char_filter":"my_char_filter" }},"char_filter": { #关键字"my_char_filter":{ #自定义char_filter"type":"mapping", "mappings":[ #指明映射关系":)=>happy",":(=>sad"]}}}}}
POST t_index/_analyze
{"analyzer": "my_analyzer","text": ["i am so :)"]
}
得到 [i,am,so,happy]
pattern replace
- pattern参数 正则
- replacement 替换字符串 可以使用$1…$9
- flags 正则标志
tokenizer 将原始文档按照一定规则切分为单词
standard
参数:max_token_length,最大token长度,默认是255
letter 非字母时分成多个terms
lowcase 跟letter tokenizer一样 ,同时将字母转化成小写
whitespace 按照空白字符分成多个terms
参数:max_token_length
keyword 空操作,输出完全相同的文本
参数:buffer_size,单词一个term读入缓冲区的长度,默认256
token filter 针对tokenizer 输出的单词进行增删改等操作
lowercase 将输出的单词转化成小写
stop 从token流中删除stop words
参数有:
# stopwords 要使用的stopwords, 默认_english_
# stopwords_path
# ignore_case 设置为true则为小写,默认false
# remove_trailing
PUT t_index
{"settings": {"analysis": {"analyzer": {"my_analyzer":{"type":"custom","tokenizer":"standard","filter":"my_filter"}},"filter": {"my_filter":{"type":"stop","stopwords":["and","or","not"]}}}}
}
POST t_index/_analyze
{"analyzer": "my_analyzer","text": ["lucky and happy not sad"]
}
-------------->
[lucky,happy,sad]
https://www.cnblogs.com/xiaobaozi-95/p/9328948.html
Elasticsearch安装ik分词器 : https://www.jianshu.com/p/e29ab446cfa6
ELK下es的分词器analyzer相关推荐
- ELK系列(十)、ES中文分词器IK插件安装和配置远程词库热加载
简介 IK Analyzer是一个开源的,基于Java语言开发的轻量级的中文分词工具包: 最初,它是以开源项目Luence 为应用主体的,结合词典分词和文法分析算法的中文分词组件:从 3.0 版本开始 ...
- 分布式系列教程(34) -Linux下安装ik分词器
1.引言 因为Elasticsearch中默认的标准分词器分词器对中文分词不是很友好,会将中文词语拆分成一个一个中文的汉字,因此引入中文分词器-es-ik插件. 例如使用传统的分词器,可以看到把中文分 ...
- Lucene.net(4.8.0)+PanGu分词器 问题记录一 分词器Analyzer的构造和内部成员ReuseStategy
前言:目前自己在做使用Lucene.net和PanGu分词实现全文检索的工作,不过自己是把别人做好的项目进行迁移.因为项目整体要迁移到ASP.NET Core 2.0版本,而Lucene使用的版本是3 ...
- Linux下安装ik分词器
Linux下安装ik分词器 首先下载ik分词器 下载地址: https://github.com/medcl/elasticsearch-analysis-ik/releases 一定要点进来 下载z ...
- es自定义分词器和分词规则
1.analysis 和 analyzer analysis是指把全文本转换成一系列单词(term/token)的过程,也叫分词. analysis是通过分词器analyzer来实现的 2.分词 St ...
- ElasticSearch 自定义分词器Analyzer示例
一.前提概述 接下来定义一个index,并在该index中使用自定义分词器. 假设在ES中有这么一个index,用来存储用户在什么时间调用了哪个接口,调用的入参是什么的这么一个信息(即服务网关日志), ...
- Elasticsearch下安装ik分词器
安装ik分词器(必须安装maven) 上传相应jar包 解压到相应目录 unzip elasticsearch-analysis-ik-master.zip(zip包) cp -r elasticse ...
- docker使用小记——docker安装es+ik分词器+拼音分词器+kibana
一.docker安装:Windows Docker 安装 | 菜鸟教程 二.docker换镜像源 修改或新增 /etc/docker/daemon.json vi /etc/docker/daemon ...
- 【Elasticsearch】es IK分词器的安装
1.概述 [Elasticsearch]es 7.8.0 唐诗三百首写入 Elasticsearch 会发生什么 之前我们创建索引,查询数据,都是使用的默认的分词器,分词效果不太理想,会把text的字 ...
最新文章
- 2.STM32中对Key_GPIO_Config()函数的理解(自定义)之轮询控制按键LED
- BZOJ1996:[HNOI2010]CHORUS 合唱队(区间DP)
- 均薪连续三年过万,北上深人才需求大 !| 5G 人才报告
- Linux下开源邮件系统Postfix+Extmail+Extman环境部署
- 使用ColorUI组件
- java怎么部署_java项目服务器如何部署?项目服务器的部署步骤
- python第三方库官方文档汇总
- 如何给澳洲路局写信refound罚金,遇到交通罚款怎么办
- 项目报错-Some file crunching failed, see logs for details
- A - Round decimals
- ArcToolbox工具名英汉对应
- 静夜思 | 你的眼界,决定了你发现美好的能力
- 微盟电子商城网络交易系统——Day01【项目介绍、项目环境搭建、快速搭建后台管理系统】
- 太赞了!图解SQL基础知识,菜鸟也能看懂的SQL文章!
- js 判断是否为 Android IOS IPAD IPHONE 等移动设备访问
- [转载]interp1
- 有苦有乐的算法 --- 判断一颗二叉树是否是完全二叉树、是否是平衡二叉树、是否是搜索二叉树
- 免费下载电子书!618大促背后前端代码如何智能生成?
- Android不明原因崩溃,不断重启解决办法记录
- 老鼠试药——二进制算法
热门文章
- 归并有效排序算法matlab,科学网—[用MATLAB写算法]之排序算法2)归并排序merge sort - 徐勇刚的博文...
- 电子计算机说明文作文,关于电脑说明文作文(精选3篇)
- 滨州学院计算机自荐考试题型,滨州学院期末考试试卷标准样式.doc
- PowerDesigner 中将Comment(注释)及Name(名称)内容互相COPY的VBS代码
- Flex:MyReport报表引擎2.7.3.0新功能——甘特图
- 马云:电商之王还想怎样(转)
- vba把图片转成二进制_70多岁日本老人用Excel画画,我用VBA将图片转成标注图
- pandas读取Excel文件
- 【宇润日常疯测-004】JS 遍历数组如何快!快!快!
- WinAPI: SetTextJustification - 设置两端对齐