分析 API
1.0 版本引入分析 API 允许你执行文本分析,这是将非结构化文本转换为针对搜索优化的单个词元(通常是单词)的过程。
分析 API 分析文本字符串并返回生成的词元。
NOTE
如果你使用安全插件,则必须拥有 manage index 权限。如果你只想分析文本,则必须拥有 manage cluster 权限。
路径和 HTTP 方法
GET /_analyze
GET /{index}/_analyze
POST /_analyze
POST /{index}/_analyzeNOTE
虽然你可以使用 GET 和 POST 请求发送分析请求,但两者有重要区别。GET 请求会导致数据被缓存在索引中,以便下次请求数据时能够更快地检索。POST 请求发送一个尚不存在的字符串到分析器,以便与索引中已有的数据进行比较。POST 请求不会被缓存。
路径参数
你可以在请求中包含以下可选路径参数。
| 参数 | 数据类型 | 描述 |
|---|---|---|
| index | String | 用于派生分析器的索引。 |
查询参数
你可以在请求中包含以下可选查询参数。
| 字段 | 数据类型 | 描述 |
|---|---|---|
| analyzer | String | 应用于 text 字段的分析器名称。分析器可以是内置的或在索引中配置的。如果未指定 analyzer,分析 API 将使用 field 字段映射中定义的分析器。如果未指定 field 字段,分析 API 将使用索引的默认分析器。如果未指定索引或索引没有默认分析器,分析 API 将使用标准分析器。 |
| attributes | 字符串数组 | 用于过滤 explain 字段输出的词元属性数组。 |
| char_filter | 字符串数组 | 在 tokenizer 字段之前预处理字符的字符过滤器数组。 |
| explain | Boolean | 如果为 true,则响应中包含词元属性和附加详细信息。默认为 false。 |
| field | String | 用于派生分析器的字段。 如果指定 field,还必须指定 index 路径参数。如果指定 analyzer 字段,它将覆盖 field 的值。如果不指定 field,分析 API 将使用索引的默认分析器。如果不指定 index 字段,或索引没有默认分析器,分析 API 将使用标准分析器。 |
| filter | 字符串数组 | 在 tokenizer 字段之后应用的词元过滤器数组。 |
| normalizer | String | 用于将文本转换为单个词元的规范化器。 |
| tokenizer | String | 用于将 text 字段转换为词元的分词器。 |
以下查询参数是必需的。
| 字段 | 数据类型 | 描述 |
|---|---|---|
| text | String 或字符串数组 | 要分析的文本。如果提供字符串数组,文本将作为多值字段进行分析。 |
示例请求
分析文本字符串数组
当你向 text 字段传递字符串数组时,它将作为多值字段进行分析。
GET /_analyze
{
"analyzer" : "standard",
"text" : ["first array element", "second array element"]
}上述请求返回以下字段:
{
"tokens" : [
{
"token" : "first",
"start_offset" : 0,
"end_offset" : 5,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "array",
"start_offset" : 6,
"end_offset" : 11,
"type" : "<ALPHANUM>",
"position" : 1
},
{
"token" : "element",
"start_offset" : 12,
"end_offset" : 19,
"type" : "<ALPHANUM>",
"position" : 2
},
{
"token" : "second",
"start_offset" : 20,
"end_offset" : 26,
"type" : "<ALPHANUM>",
"position" : 3
},
{
"token" : "array",
"start_offset" : 27,
"end_offset" : 32,
"type" : "<ALPHANUM>",
"position" : 4
},
{
"token" : "element",
"start_offset" : 33,
"end_offset" : 40,
"type" : "<ALPHANUM>",
"position" : 5
}
]
}应用内置分析器
如果省略 index 路径参数,你可以将任何内置分析器应用于文本字符串。
以下请求使用 standard 内置分析器分析文本:
GET /_analyze
{
"analyzer" : "standard",
"text" : "OpenSearch text analysis"
}上述请求返回以下字段:
{
"tokens" : [
{
"token" : "opensearch",
"start_offset" : 0,
"end_offset" : 10,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "text",
"start_offset" : 11,
"end_offset" : 15,
"type" : "<ALPHANUM>",
"position" : 1
},
{
"token" : "analysis",
"start_offset" : 16,
"end_offset" : 24,
"type" : "<ALPHANUM>",
"position" : 2
}
]
}应用自定义分析器
你可以创建自己的分析器并在分析请求中指定它。
在此场景中,已创建了一个自定义分析器 lowercase_ascii_folding 并与 books2 索引关联。该分析器将文本转换为小写并将非 ASCII 字符转换为 ASCII。
以下请求将自定义分析器应用于提供的文本:
GET /books2/_analyze
{
"analyzer": "lowercase_ascii_folding",
"text" : "Le garçon m'a SUIVI."
}上述请求返回以下字段:
{
"tokens" : [
{
"token" : "le",
"start_offset" : 0,
"end_offset" : 2,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "garcon",
"start_offset" : 3,
"end_offset" : 9,
"type" : "<ALPHANUM>",
"position" : 1
},
{
"token" : "m'a",
"start_offset" : 10,
"end_offset" : 13,
"type" : "<ALPHANUM>",
"position" : 2
},
{
"token" : "suivi",
"start_offset" : 14,
"end_offset" : 19,
"type" : "<ALPHANUM>",
"position" : 3
}
]
}应用自定义临时分析器
你可以从分词器、词元过滤器或字符过滤器构建自定义临时分析器。使用 filter 参数指定词元过滤器。
以下请求使用 uppercase 字符过滤器将文本转换为大写:
GET /_analyze
{
"tokenizer" : "keyword",
"filter" : ["uppercase"],
"text" : "OpenSearch filter"
}上述请求返回以下字段:
{
"tokens" : [
{
"token" : "OPENSEARCH FILTER",
"start_offset" : 0,
"end_offset" : 17,
"type" : "word",
"position" : 0
}
]
}以下请求使用 html_strip 过滤器从文本中删除 HTML 字符:
GET /_analyze
{
"tokenizer" : "keyword",
"filter" : ["lowercase"],
"char_filter" : ["html_strip"],
"text" : "<b>Leave</b> right now!"
}上述请求返回以下字段:
{
"tokens" : [
{
"token" : "leave right now!",
"start_offset" : 3,
"end_offset" : 23,
"type" : "word",
"position" : 0
}
]
}你可以使用数组组合过滤器。
以下请求组合了 lowercase 转换和 stop 过滤器,后者删除 stopwords 数组中的单词:
GET /_analyze
{
"tokenizer" : "whitespace",
"filter" : ["lowercase", {"type": "stop", "stopwords": [ "to", "in"]}],
"text" : "how to train your dog in five steps"
}上述请求返回以下字段:
{
"tokens" : [
{
"token" : "how",
"start_offset" : 0,
"end_offset" : 3,
"type" : "word",
"position" : 0
},
{
"token" : "train",
"start_offset" : 7,
"end_offset" : 12,
"type" : "word",
"position" : 2
},
{
"token" : "your",
"start_offset" : 13,
"end_offset" : 17,
"type" : "word",
"position" : 3
},
{
"token" : "dog",
"start_offset" : 18,
"end_offset" : 21,
"type" : "word",
"position" : 4
},
{
"token" : "five",
"start_offset" : 25,
"end_offset" : 29,
"type" : "word",
"position" : 6
},
{
"token" : "steps",
"start_offset" : 30,
"end_offset" : 35,
"type" : "word",
"position" : 7
}
]
}指定索引
你可以使用索引的默认分析器分析文本,或者指定不同的分析器。
以下请求使用与 books 索引关联的默认分析器分析提供的文本:
GET /books/_analyze
{
"text" : "OpenSearch analyze test"
}上述请求返回以下字段:
"tokens" : [
{
"token" : "opensearch",
"start_offset" : 0,
"end_offset" : 10,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "analyze",
"start_offset" : 11,
"end_offset" : 18,
"type" : "<ALPHANUM>",
"position" : 1
},
{
"token" : "test",
"start_offset" : 19,
"end_offset" : 23,
"type" : "<ALPHANUM>",
"position" : 2
}
]
}以下请求使用 keyword 分析器分析提供的文本,该分析器将整个文本值作为单个词元返回:
GET /books/_analyze
{
"analyzer" : "keyword",
"text" : "OpenSearch analyze test"
}上述请求返回以下字段:
{
"tokens" : [
{
"token" : "OpenSearch analyze test",
"start_offset" : 0,
"end_offset" : 23,
"type" : "word",
"position" : 0
}
]
}从索引字段派生分析器
你可以传递文本和索引中的字段。API 会查找该字段的分析器并使用它来分析文本。
如果映射不存在,API 将使用标准分析器,该分析器将所有文本转换为小写并基于空格进行分词。
以下请求导致分析基于 name 的映射进行:
GET /books2/_analyze
{
"field" : "name",
"text" : "OpenSearch analyze test"
}上述请求返回以下字段:
{
"tokens" : [
{
"token" : "opensearch",
"start_offset" : 0,
"end_offset" : 10,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "analyze",
"start_offset" : 11,
"end_offset" : 18,
"type" : "<ALPHANUM>",
"position" : 1
},
{
"token" : "test",
"start_offset" : 19,
"end_offset" : 23,
"type" : "<ALPHANUM>",
"position" : 2
}
]
}指定规范化器
你可以使用与索引关联的规范化器代替关键字字段。规范化器会导致分析变化产生单个词元。
在此示例中,books2 索引包含一个名为 to_lower_fold_ascii 的规范化器,该规范化器将文本转换为小写并将非 ASCII 文本转换为 ASCII。
以下请求将 to_lower_fold_ascii 应用于文本:
GET /books2/_analyze
{
"normalizer" : "to_lower_fold_ascii",
"text" : "C'est le garçon qui m'a suivi."
}上述请求返回以下字段:
{
"tokens" : [
{
"token" : "c'est le garcon qui m'a suivi.",
"start_offset" : 0,
"end_offset" : 30,
"type" : "word",
"position" : 0
}
]
}你可以使用词元过滤器和字符过滤器创建自定义临时规范化器。
以下请求使用 uppercase 字符过滤器将给定文本转换为全大写:
GET /_analyze
{
"filter" : ["uppercase"],
"text" : "That is the boy who followed me."
}上述请求返回以下字段:
{
"tokens" : [
{
"token" : "THAT IS THE BOY WHO FOLLOWED ME.",
"start_offset" : 0,
"end_offset" : 32,
"type" : "word",
"position" : 0
}
]
}获取词元详细信息
你可以通过将 explain 属性设置为 true 来获取所有词元的附加详细信息。
以下请求提供了与 standard 分词器一起使用的 reverse 过滤器的详细词元信息:
GET /_analyze
{
"tokenizer" : "standard",
"filter" : ["reverse"],
"text" : "OpenSearch analyze test",
"explain" : true,
"attributes" : ["keyword"]
}上述请求返回以下字段:
{
"detail" : {
"custom_analyzer" : true,
"charfilters" : [ ],
"tokenizer" : {
"name" : "standard",
"tokens" : [
{
"token" : "OpenSearch",
"start_offset" : 0,
"end_offset" : 10,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "analyze",
"start_offset" : 11,
"end_offset" : 18,
"type" : "<ALPHANUM>",
"position" : 1
},
{
"token" : "test",
"start_offset" : 19,
"end_offset" : 23,
"type" : "<ALPHANUM>",
"position" : 2
}
]
},
"tokenfilters" : [
{
"name" : "reverse",
"tokens" : [
{
"token" : "hcraeSnepO",
"start_offset" : 0,
"end_offset" : 10,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "ezylana",
"start_offset" : 11,
"end_offset" : 18,
"type" : "<ALPHANUM>",
"position" : 1
},
{
"token" : "tset",
"start_offset" : 19,
"end_offset" : 23,
"type" : "<ALPHANUM>",
"position" : 2
}
]
}
]
}
}设置词元限制
你可以设置生成词元数量的限制。设置较低的值可减少节点的内存使用。默认值为 10000。
以下请求将词元限制为四个:
PUT /books2
{
"settings" : {
"index.analyze.max_token_count" : 4
}
}NOTE
上述请求是索引 API 而不是分析 API。有关其他详细信息,请参阅动态索引级别索引设置。
响应字段
文本分析端点返回以下响应字段。
| 字段 | 数据类型 | 描述 |
|---|---|---|
| tokens | 数组 | 从 text 派生的词元数组。请参阅词元对象。 |
| detail | Object | 有关分析和每个词元的详细信息。仅在请求词元详细信息时包含。请参阅详细信息对象。 |
词元对象
| 字段 | 数据类型 | 描述 |
|---|---|---|
| token | String | 词元的文本。 |
| start_offset | Integer | 词元在原始文本字符串中的起始位置。偏移量从零开始。 |
| end_offset | Integer | 词元在原始文本字符串中的结束位置。 |
| type | String | 词元的分类:<ALPHANUM>、<NUM> 等。类型通常由分词器设置,但某些过滤器会定义自己的类型。例如,同义词过滤器定义 <SYNONYM> 类型。 |
| position | Integer | 词元在 tokens 数组中的位置。 |
详细信息对象
| 字段 | 数据类型 | 描述 |
|---|---|---|
| custom_analyzer | Boolean | 应用于文本的分析器是自定义的还是内置的。 |
| charfilters | 数组 | 应用于文本的字符过滤器列表。 |
| tokenizer | Object | 应用于文本的分词器名称和词元列表*,包含应用词元过滤器之前的内容。 |
| tokenfilters | 数组 | 应用于文本的词元过滤器列表。每个词元过滤器包括过滤器名称和词元列表*,包含应用过滤器之后的内容。词元过滤器按请求中指定的顺序列出。 |
NOTE
有关词元字段描述,请参阅词元对象。