分词就是将一段文本按照一定的规则切分成以一个一个的关键字的过程
ElasticSearch的分词器(Analyzer)一般由三种组件构成:
1、character filter 字符过滤器:
在一段文本分词之前,先进行预处理,最常见的就是【过滤html标签】
例如:hello --> hello,I & you --> I and you
2、tokenizers 分词器:
默认情况下,英文分词根据空格将单词分开;中文分词按单字隔开,也可以采用机器学习算法来分词
3、Token filters Token过滤器:
将切分的单词进行加工,大小写转换,去掉停用词(例如“a”、“and”、“the”等等 ),加入同义词(例如同义词像“jump”和“leap”)
三者顺序:Character Filters—>Tokenizer—>Token Filter
三者个数:Character Filters(0个或多个) + Tokenizer + Token Filters(0个或多个)
创建索引时设置分词器
PUT /my_index
{
"mappings": {
"properties": {
"title":{
"type": "text",
"analyzer": "standard" #显示指定分词器
}
}
}
}
注意:按照什么分词就会去掉什么 !!!!!
标准分词器(standard analyzer)默认分词器
GET _analyze
{
"analyzer": "standard",
"text": "hello world,I'm Chinese1我爱中国,我是中国人。"
}
#分词的结果
hello|world|i'm|chinese1|我|爱|中|国|我|是|中|国|人
简单分词器(simple analyzer)
GET _analyze
{
"analyzer": "simple",
"text": "hello world,I'm Chinese1我爱中国,我是中国人。"
}
#分词的结果
hello|world|i|m|chinese|我爱中国|我是中国人
空白分词器(whitespace analyzer)
GET _analyze
{
"analyzer": "whitespace",
"text": "hello world,I'm Chinese1我爱中国,我是中国人。"
}
#分词的结果
“hello|world,I'm|Chinese1我爱中国,我是中国人。
停止分词器(stop analyzer)
GET _analyze
{
"analyzer": "stop",
"text" : "so whenever we visit a Chinese family she tells me to buy them a gift.hello1我爱中国,我是中国人。"
}
#分词的结果
so|whenever|we|visit|Chinese|family|she|tells|me|buy|them|gift|hello|我爱中国|我是中国人
正则表达式分词器(pattern analyzer)
\w包括英文字母、阿拉伯数字、_,\W是任意一个非\w字符,中文字符也算\W。
+表示一个及以上,就是说多个非\w字符算作一处。
语言分词器(language analyzer)
english、chinese的效果都一样:在空格、符号、英文介词|冠词 处切,中文切割为一个一个的汉字。
其他分词器
在ES中支持中文分词器非常多 如 smartCN、IK 等,推荐的就是 IK分词器。
IK分词器有两种粒度划分:
POST /_analyze
{
"text":"中华民族共和国国歌",
"analyzer":"ik_smart"
}
{
"tokens" : [
{
"token" : "中华民族",
"start_offset" : 0,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "共和国",
"start_offset" : 4,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "国歌",
"start_offset" : 7,
"end_offset" : 9,
"type" : "CN_WORD",
"position" : 2
}
]
}
POST /_analyze
{
"text":"中华民族共和国国歌",
"analyzer":"ik_max_word"
}
{
"tokens" : [
{
"token" : "中华民族",
"start_offset" : 0,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "中华",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "民族",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "共和国",
"start_offset" : 4,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 3
},
{
"token" : "共和",
"start_offset" : 4,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 4
},
{
"token" : "国",
"start_offset" : 6,
"end_offset" : 7,
"type" : "CN_CHAR",
"position" : 5
},
{
"token" : "国歌",
"start_offset" : 7,
"end_offset" : 9,
"type" : "CN_WORD",
"position" : 6
}
]
}
扩展词和关键词
设置扩展词典和停用词典在es容器中的config目录下的IKAnalyzer.cfg.xml中
DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 扩展配置comment>
<entry key="ext_dict">ext_dict.dicentry>
<entry key="ext_stopwords">ext_stopword.dicentry>
properties>
在es容器中config目录下中创建ext_dict.dic文件 编码一定要为UTF-8才能生效
vim ext_dict.dic 加入扩展词即可
在es容器中config目录中创建ext_stopword.dic文件
vim ext_stopword.dic 加入停用词即可
重启es生效
es本身也提供了一些常用的扩展词典和停用词典可以直接使用:

配置自定义远程扩展字典和停用词典参考