-
ES分词器
分词器使用的两个情形:
1,Index time analysis. 创建或者更新文档时,会对文档进行分词
2,Search time analysis. 查询时,对查询语句分词
指定查询时使用哪个分词器的方式有:
- - 查询时通过analyzer指定分词器
GET test_index/_search
{
"query": {
"match": {
"name": {
"query": "li",
"analyzer": "standard"
}
}
}
}
- - 创建index mapping时指定查询分词search_analyzer
PUT test_index
{
"mappings": {
"doc": {
"properties": {
"title":{
"type": "text",
"analyzer": "whitespace",
"search_analyzer": "standard"
}
}
}
}
}
索引时分词是通过配置 Index mapping中的每个字段的参数analyzer指定的
# 不指定分词时,会使用默认的standard
PUT test_index
{
"mappings": {
"doc": {
"properties": {
"title":{
"type": "text",
"analyzer": "whitespace" #指定分词器,es内置有多种analyzer
}
}
}}}
注意:
明确字段是否需要分词,不需要分词的字段将type设置为keyword,可以节省空间和提高写性能。
ES内置的多种分析器analyzer
standard:分析器划分文本是通过词语来界定的,由Unicode文本分割算法定义。它删除大多数标点符号,将词语转换为小写(就是按照空格进行分词)
simple:分析器每当遇到不是字母的字符时,将文本分割为词语。它将所有词语转换为小写。
keyword:可以接受任何给定的文本,并输出与单个词语相同的文本
pattern:分析器使用正则表达式将文本拆分为词语,它支持小写和停止字
whitespace:(空白)分析器每当遇到任何空白字符时,都将文本划分为词语。它不会将词语转换为小写
第三方analyzer插件---中文分词(ik分词器)
es内置很多分词器,但是对中文分词并不友好,例如使用standard分词器对一句中文话进行分词,会分成一个字一个字的。这时可以使用第三方的插件,比如 ik、pinyin等
使用较多的ik分词也分两种分析器。ik_smart:尽可能少的进行中文分词。ik_max_word:尽可能多的进行中文分词。
custom:自定义分析器
测试
1.
POST _analyze
{
"analyzer": "standard",
"text": "this is Standard Tokenizer!!!!"
}
{
"tokens": [
{
"token": "this",
"start_offset": 0,
"end_offset": 4,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "is",
"start_offset": 5,
"end_offset": 7,
"type": "<ALPHANUM>",
"position": 1
},
{
"token": "standard",
"start_offset": 8,
"end_offset": 16,
"type": "<ALPHANUM>",
"position": 2
},
{
"token": "tokenizer",
"start_offset": 17,
"end_offset": 26,
"type": "<ALPHANUM>",
"position": 3
}
]
}
2.
POST _analyze
{
"analyzer": "standard",
"text": "功能进阶"
}
{
"tokens": [
{
"token": "功",
"start_offset": 0,
"end_offset": 1,