《Elastic Stack 实战手册》——三、产品能力——3.4.入门篇——3.4.2.Elasticsearch基础应用——3.4.2.17.Text analysis, settings 及 mappings——3.4.2.17.4.Analyzers / Custom analyzers(5) https://developer.aliyun.com/article/1229771
配置项
pattern 匹配分隔符的正则表达式,默认是 \W+ 。
flags Java 中正则表达式匹配标记属性,多个标记属性使用 | 竖线分割。
GET _analyze { "tokenizer": { "type": "pattern", "pattern":"(-\\d,)" }, "text": "one-1,two-2,three-3" #1 } #Response [ one, two, three-3 ]
#1 切分文本的分隔符为正则匹配到的连接符、数字和逗号。
Char Group Tokenizer
char_group 通过指定的字符集当作分隔符对文本内容进行切分,适用于简单切分的场景效率高于使用 pattern 正则匹配的方式。
GET _analyze { "tokenizer": { "type": "char_group", "tokenize_on_chars": [ "-" ] }, "text": "a6cd-87b8-4356-932d-c4b13" } #Response [ a6cd, 87b8, 4356, 932d, c4b13 ]
配置项
tokenize_on_chars 指定切分文本的分隔符列表,支持指定单个字符或以下字符组:
l letter 字符
l digit 数字
l whitespace 空格或换行符
l punctuation 标点符号
l symbol 标志符号
GET _analyze { "tokenizer": { "type": "char_group", "tokenize_on_chars": [ "whitespace", "-", ":" ] }, "text": "Spider-Man: Far From Home " } #Response [ Spider, Man, Far, From, Home ]
Path Tokenizer
path_hierarchy 对文件目录切分,根据路径分隔符将每一层目录都切分为一个单词。
GET _analyze { "tokenizer": { "type": "path_hierarchy" }, "text": "/home/data/readme.txt" } #Response [ /home, /home/data, /home/data/readme.txt ]
配置项
delimiter 目录分隔符,默认 / 。
replacement 切分后的目录分隔符,和 delimiter 保持一致。
reverse 是否目录倒序从下往上输出,默认为 false。
skip 忽略目录数,默认为0,保留全部。
GET _analyze { "tokenizer": { "type": "path_hierarchy", "delimiter":"/", "reverse":false, "replacement":"-" }, "text": "one/two/three/read.txt" } #Response [ one, one-two, one-two-three, one-two-three-read.txt ]
Token filter
ASCII folding token filter
asciifolding 会尝试将文本中的非英文字母,数字和符号转换为基本的 ASCII 字符(前127位),常用欧系和拉丁语言。
GET /_analyze { "tokenizer" : "standard", "filter" : ["asciifolding"], "text" : "açaí à la carte" } #Response [ acai, a, la, carte ]
CJK bigram token filter
cjk_bigram 针对中文,日文和韩文字符处理,默认会按照二元语法由相邻两两文字组成为一个词语来处理。内置分词器 cjk 中包含这个过滤器。
GET /_analyze { "tokenizer": "standard", "filter": [ "cjk_bigram" ], "text": "一条测试文本" } #Response [ 一条, 条测, 测试, 试文, 文本 ]
Conditional token filter
condition 通过编写 painless 脚本过滤符合条件的文本,只有满足条件的文本执行指定的过滤器。
GET /_analyze { "tokenizer": "standard", "filter": [ { "type": "condition", "filter": [ "reverse" #1 ], "script": { "source": "token.getTerm().length() < 4" #2 } } ], "text": "Hey,Also for search!" } #Response [ yeH, Also, rof, search ]
#1 符合脚本设定条件的单词进行反转。
#2 当单词长度小于4的情况下执行过滤器。
script(必填)定义用于过滤单词的 painless 脚本,满足条件的单词会执行 filter 中定义的过滤器,其它不匹配的单词不进行任何处理。
Fingerprint token filter
fingerprint 过滤器会对文本内容生成一个指纹信息做为单词存储。生成过程包括以下三步:
1、切分后的单词按照字母排序
2、删除相同的单词
3、通过连接符将单词拼接为一个字符串
GET /_analyze { "tokenizer": "standard", "filter": [ { "type": "fingerprint" } ], "text": "A good cook could cook cookies?" } #Response [ "A cook cookies could good" ]
《Elastic Stack 实战手册》——三、产品能力——3.4.入门篇——3.4.2.Elasticsearch基础应用——3.4.2.17.Text analysis, settings 及 mappings——3.4.2.17.4.Analyzers / Custom analyzers(7) https://developer.aliyun.com/article/1229768