一、内置分词器
es内置分词器对中文很不友好,处理方式为:一个字一个词。
es的内置分词器:
- Standard Analyzer:默认分词器,按词切分,小写处理
- Simple Analyzer:按照非字母切分(符号被过滤),小写处理
- Stop Analyzer:停用词过滤(the,a,is),小写处理
- Whitespace Analyzer:按照空格切分,不转小写
- Keyword Analyzer:直接将输入当做输出,不分词
- Patter Analyzer:正则表达式,默认 \W+(非字符分割)
- Language:提供了30多种常见语言的分词器
# 默认标准分词器
GET _analyze
{
"analyzer": "standard",
"text": ["我爱北京天安门!","it is so beautiful?"]
}
二、安装中文分词器:IK
IKAnalyzer是一个开源的、基于java开发的轻量级中文分词工具包
下载:
- 在iterm2中进入elasticsearch-7.6.2路径
- 输入下面命令来下载ik(命令中的两个7.6.2对应的es的版本号)
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.6.2/elasticsearch-analysis-ik-7.6.2.zip
其他版本的下载地址链接为:https://github.com/medcl/elasticsearch-analysis-ik/releases?after=v7.9.1
三、使用IK分词器
IK分词器有两种分词模式:ik_max_word和ik_smart模式
- ik_max_word模式会对文本进行最细粒度的拆分, 比如将华为手机分为华为、手、手机
- ik_smart模式是粗粒度的,将华为手机分为华为、手机。
# 默认标准分词器
GET _analyze
{
"analyzer": "standard",
"text": ["我爱北京天安门!","it is so beautiful?"]
}
# ik分词器,粗粒度
GET _analyze
{
"analyzer": "ik_smart",
"text": ["我爱北京天安门!","it is so beautiful?"]
}
# ik分词器,细粒度
GET _analyze
{
"analyzer": "ik_max_word",
"text": ["我爱北京天安门!","it is so beautiful?"]
}