Elasticsearch - 分析
在搜索操作期间处理查询时,分析模块会分析任何索引中的内容。此模块由分析器、标记器、标记过滤器和字符过滤器组成。如果未定义分析器,则默认情况下内置的分析器、标记、过滤器和标记器将注册到分析模块。
在下面的示例中,我们使用标准分析器,当未指定其他分析器时使用该分析器。它将根据语法分析句子并生成句子中使用的单词。
POST _analyze
{
"analyzer": "standard",
"text": "Today's weather is beautiful"
}
运行上述代码后,我们得到如下所示的响应 −
{
"tokens" : [
{
"token" : "today's",
"start_offset" : 0,
"end_offset" : 7,
"type" : "",
"position" : 0
},
{
"token" : "weather",
"start_offset" : 8,
"end_offset" : 15,
"type" : "",
"position" : 1
},
{
"token" : "is",
"start_offset" : 16,
"end_offset" : 18,
"type" : "",
"position" : 2
},
{
"token" : "beautiful",
"start_offset" : 19,
"end_offset" : 28,
"type" : "",
"position" : 3
}
]
}
配置标准分析器
我们可以使用各种参数配置标准分析器,以满足我们的自定义要求。
在下面的示例中,我们将标准分析器的 max_token_length 配置为 5。
为此,我们首先使用具有 max_length_token 参数的分析器创建一个索引。
PUT index_4_analysis
{
"settings": {
"analysis": {
"analyzer": {
"my_english_analyzer": {
"type": "standard",
"max_token_length": 5,
"stopwords": "_english_"
}
}
}
}
}
接下来,我们将分析器应用于如下所示的文本。请注意,标记 is 没有出现,因为它在开头有两个空格,在结尾有两个空格。对于单词"is",其开头有一个空格,结尾也有一个空格。将它们全部加在一起,它就变成了 4 个带空格的字母,但这并不能使其成为一个单词。至少在开头或结尾应该有一个非空格字符,才能使其成为一个要计算的单词。
POST index_4_analysis/_analyze
{
"analyzer": "my_english_analyzer",
"text": "Today's weather is beautiful"
}
运行上述代码后,我们得到如下所示的响应 −
{
"tokens" : [
{
"token" : "today",
"start_offset" : 0,
"end_offset" : 5,
"type" : "",
"position" : 0
},
{
"token" : "s",
"start_offset" : 6,
"end_offset" : 7,
"type" : "",
"position" : 1
},
{
"token" : "weath",
"start_offset" : 8,
"end_offset" : 13,
"type" : "",
"position" : 2
},
{
"token" : "er",
"start_offset" : 13,
"end_offset" : 15,
"type" : "",
"position" : 3
},
{
"token" : "beaut",
"start_offset" : 19,
"end_offset" : 24,
"type" : "",
"position" : 5
},
{
"token" : "iful",
"start_offset" : 24,
"end_offset" : 28,
"type" : "",
"position" : 6
}
]
}
各种分析器列表及其说明如下表所示 −
| S.No | 分析器和说明 |
|---|---|
| 1 |
Standard analyzer (standard) 可以为此分析器设置停用词和 max_token_length 设置。默认情况下,停用词列表为空,max_token_length为255。 |
| 2 |
Simple analyzer (simple) 此分析器由小写标记器组成。 |
| 3 |
Whitespace analyzer (whitespace) 此分析器由空白标记器组成。 |
| 4 |
Stop analyzer (stop) 可以配置停用词和 stopwords_path。默认情况下,停用词初始化为英语停用词,stopwords_path 包含带有停用词的文本文件的路径。 |
标记器
标记器用于从 Elasticsearch 中的文本生成标记。可以通过考虑空格或其他标点符号将文本分解为标记。Elasticsearch 有大量内置标记器,可用于自定义分析器。
下面显示了一个标记器示例,它在遇到非字母字符时将文本分解为术语,但它也会将所有术语小写 −
POST _analyze
{
"tokenizer": "lowercase",
"text": "It Was a Beautiful Weather 5 Days ago."
}
运行上述代码后,我们得到如下所示的响应 −
{
"tokens" : [
{
"token" : "it",
"start_offset" : 0,
"end_offset" : 2,
"type" : "word",
"position" : 0
},
{
"token" : "was",
"start_offset" : 3,
"end_offset" : 6,
"type" : "word",
"position" : 1
},
{
"token" : "a",
"start_offset" : 7,
"end_offset" : 8,
"type" : "word",
"position" : 2
},
{
"token" : "beautiful",
"start_offset" : 9,
"end_offset" : 18,
"type" : "word",
"position" : 3
},
{
"token" : "weather",
"start_offset" : 19,
"end_offset" : 26,
"type" : "word",
"position" : 4
},
{
"token" : "days",
"start_offset" : 29,
"end_offset" : 33,
"type" : "word",
"position" : 5
},
{
"token" : "ago",
"start_offset" : 34,
"end_offset" : 37,
"type" : "word",
"position" : 6
}
]
}
下表列出了 Tokenizer 及其说明 −
| S.No | Tokenizer 和说明 |
|---|---|
| 1 |
Standard tokenizer (standard) 这是基于语法的标记器,并且可以为此标记器配置max_token_length。 |
| 2 |
Edge NGram tokenizer (edgeNGram) 可以为此标记器设置 min_gram、max_gram、token_chars 等设置。 |
| 3 |
Keyword tokenizer (keyword) 这会生成整个输入作为输出,并且可以为此设置 buffer_size。 |
| 4 |
Letter tokenizer (letter) 这将捕获整个单词,直到遇到非字母。 |

