标准分析器
standard
分析器是 OpenSearch 中用于通用全文搜索的内置默认分析器。它旨在通过有效地将文本分解为可搜索的术语来提供一致的、与语言无关的文本处理。
standard
分析器执行以下操作:
- 分词:使用
standard
分词器,该分词器根据 Unicode 文本分段规则将文本分成单词,处理空格、标点符号和常见分隔符。 - 小写转换:应用
lowercase
词元过滤器将所有词元转换为小写,确保无论输入大小写如何都能一致匹配。
这种组合使 standard
分析器成为索引各种自然语言内容的理想选择,无需进行语言特定的自定义。
示例:使用标准分析器创建索引
您可以在创建索引时将 standard
分析器分配给文本字段:
PUT /my_standard_index
{
"mappings": {
"properties": {
"my_field": {
"type": "text",
"analyzer": "standard"
}
}
}
}
参数
standard
分析器支持以下可选参数。
参数 | 数据类型 | 默认值 | 描述 |
---|---|---|---|
max_token_length | 整数 | 255 | 词元在被分割之前的最大长度。 |
停用词 | 字符串或字符串列表 | 无 | 停用词列表或 针对某种语言的预定义停用词集,用于在分析期间移除。例如,_english_ 。 |
停用词路径 | 字符串 | 无 | 包含在分析期间使用的停用词文件的路径。 |
只能使用 stopwords
或 stopwords_path
参数之一。如果两者都使用,则不会返回错误,但只有 stopwords
参数会生效。
示例:带参数的分析器
以下示例创建一个 products
索引并配置 max_token_length
和 stopwords
参数:
PUT /animals
{
"settings": {
"analysis": {
"analyzer": {
"my_manual_stopwords_analyzer": {
"type": "standard",
"max_token_length": 10,
"stopwords": [
"the", "is", "and", "but", "an", "a", "it"
]
}
}
}
}
}
使用以下 _analyze
API 请求查看 my_manual_stopwords_analyzer
如何处理文本:
POST /animals/_analyze
{
"analyzer": "my_manual_stopwords_analyzer",
"text": "The Turtle is Large but it is Slow"
}
返回的词元:
- 已按空格分割。
- 已转换为小写。
- 已移除停用词。
{
"tokens": [
{
"token": "turtle",
"start_offset": 4,
"end_offset": 10,
"type": "<ALPHANUM>",
"position": 1
},
{
"token": "large",
"start_offset": 14,
"end_offset": 19,
"type": "<ALPHANUM>",
"position": 3
},
{
"token": "slow",
"start_offset": 30,
"end_offset": 34,
"type": "<ALPHANUM>",
"position": 7
}
]
}