一.自定义分析器
当内置分析器不能满足时,可以创建一个自定义分析器,自定义分析器(analyzer)由:
1)0或多个 charactcr filter 字符过滤器
2) 1个 tokenizer 分词器,将文本切分为分词
3)0或多个 token filter 令牌过滤器,是属于分词后再过滤
自定义配置参数如下
type | 分析器类型,接收内置分析器类型,可以使用custom(代表自定义分析器)或者省略此参数 |
tokenizer | 内置或自定义分词器 (必填) |
char_filter | 内置或自定义字符过滤器(character filters) 可选数组 |
filter | 内置或自定义令牌过滤器(token filter)可选数组 |
position_increment_gap | 间隙值,跨值访问,一般用于match_phrase短语查询检索数据,默认值100 |
示例1:自定义一个分析器
1)char_filter字符过滤器:使用html_strip去除html标签
2) tokenizer分词器:使用standard标准分词器
3)filter令牌过滤器: 使用 lowercase转小写,使用asciifolding转为ascii
定义分析器结构如下:
PUT my-index-000001
{"settings": {"analysis": {"analyzer": {"my_custom_analyzer": { #自定义一个分析器"type": "custom", "tokenizer": "standard", #使用标准分析词"char_filter": [ #使用char_filter字符过滤器"html_strip"],"filter": [ #使用filter过滤器"lowercase","asciifolding"]}}}}
}
使用自定义分析器分词
POST my-index-000001/_analyze
{"analyzer": "my_custom_analyzer","text": "Is this <b>déjà vu</b>?"
}
查看分词结果: [ is,this,deja,vu]
示例2:自定义一个复杂分析器,它结合了以下内容:
1)char_filter字符过滤器:自定义Mapping Character Filter映射:
:) 符号映射为 _happy_
:( 符号映射为 _sad_
2) tokenizer分词器:自定义Pattern 模式分词器
3) filter令牌过滤器: lowercase转小写,自定义过滤停用词
定义分析器结构:
PUT my-index-000001
{"settings": {"analysis": {"analyzer": {"my_custom_analyzer": { #自定义一个分析器"char_filter": ["emoticons" #自定义自符过滤器],"tokenizer": "punctuation", #自定义一个分词器"filter": ["lowercase","english_stop"]}},"tokenizer": { "punctuation": { #模式分词器的配置"type": "pattern","pattern": "[ .,!?]" #以里面符号来分词}},"char_filter": { #字符过滤器的配置映射"emoticons": { "type": "mapping","mappings": [":) => _happy_",":( => _sad_"]}},"filter": { #令牌过滤器的配置,过滤停用词"english_stop": { "type": "stop","stopwords": "_english_"}}}}
}
使用自定义的分析器分词
POST my-index-000001/_analyze
{"analyzer": "my_custom_analyzer","text": "I'm a :) person, and you?"
}
查看分词结果:[ i'm, _happy_, person,you]
参考官方资料:Create a custom analyzer