自动完成字段类型
1.0 版引入
自动完成字段类型通过自动完成建议器提供自动完成功能。自动完成建议器是一个前缀建议器,因此它只匹配文本的开头。自动完成建议器创建一个内存数据结构,这提供了更快的查找速度,但会导致内存使用量增加。在使用此功能之前,您需要将所有可能的自动完成列表上传到索引中。
示例
创建带有自动完成字段的映射
PUT chess_store
{
"mappings": {
"properties": {
"suggestions": {
"type": "completion"
},
"product": {
"type": "keyword"
}
}
}
}
映射参数
completion
字段类型支持以下映射参数。
参数 | 描述 |
---|---|
分析器 | 指定输入文本的索引时分析器。默认为 simple 。请参阅索引分析器。 |
search_analyzer | 定义搜索时使用的分析器。默认为 analyzer 的值。请参阅搜索分析器。 |
preserve_separators | 如果为 true (默认值),则保留空格或标点符号等分隔符。如果设置为 false ,则允许像 queensg 这样的查询匹配“Queen’s Gambit”这样的建议。 |
preserve_position_increments | 如果为 true (默认值),则维护已分析标记的位置增量。将其设置为 false 可以匹配像“The Sicilian Defense”这样的建议,当输入 s 时,因为它会跳过像“The”这样的停用词。或者,您可以在不更改分析器的情况下,将“Sicilian Defense”和“The Sicilian Defense”作为单独的输入进行索引。 |
max_input_length | 限制每个输入字符串的长度。默认为 50 个 UTF-16 码点。这仅在索引时适用,以防止大输入膨胀底层数据结构。大多数前缀自动完成在此限制内效果良好。 |
示例映射
PUT chess_store
{
"mappings": {
"properties": {
"suggestions": {
"type": "completion",
"analyzer": "standard"
},
"product": {
"type": "keyword"
}
}
}
}
将建议索引到 OpenSearch 中
PUT chess_store/_doc/1
{
"suggestions": {
"input": ["Books on openings", "Books on endgames"],
"weight" : 10
}
}
参数
下表列出了自动完成字段接受的参数。
参数 | 描述 |
---|---|
input | 作为字符串或字符串数组的可能自动完成列表。不能包含 \u0000 (空)、\u001f (信息分隔符一)或 \u001e (信息分隔符二)。必需。 |
weight | 用于对建议进行排名的正整数或正整数字符串。可选。 |
可以按如下方式索引多个建议
PUT chess_store/_doc/2
{
"suggestions": [
{
"input": "Chess set",
"weight": 20
},
{
"input": "Chess pieces",
"weight": 10
},
{
"input": "Chess board",
"weight": 5
}
]
}
作为替代方案,您可以使用以下简写符号(请注意,在此符号中不能提供 weight
参数)
PUT chess_store/_doc/3
{
"suggestions" : [ "Chess clock", "Chess timer" ]
}
查询自动完成字段类型
要查询自动完成字段类型,请指定您要搜索的前缀以及要查找建议的字段名称。
查询索引中以单词“chess”开头的建议
GET chess_store/_search
{
"suggest": {
"product-suggestions": {
"prefix": "chess",
"completion": {
"field": "suggestions"
}
}
}
}
响应包含自动完成建议
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"suggest" : {
"product-suggestions" : [
{
"text" : "chess",
"offset" : 0,
"length" : 5,
"options" : [
{
"text" : "Chess set",
"_index" : "chess_store",
"_type" : "_doc",
"_id" : "2",
"_score" : 20.0,
"_source" : {
"suggestions" : [
{
"input" : "Chess set",
"weight" : 20
},
{
"input" : "Chess pieces",
"weight" : 10
},
{
"input" : "Chess board",
"weight" : 5
}
]
}
},
{
"text" : "Chess clock",
"_index" : "chess_store",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"suggestions" : [
"Chess clock",
"Chess timer"
]
}
}
]
}
]
}
}
在响应中,_score
字段包含在索引时设置的 weight
参数的值。text
字段填充了建议的 input
参数。
默认情况下,响应包含整个文档,包括 _source
字段,这可能会影响性能。要仅返回 suggestions
字段,您可以在 _source
参数中指定。您还可以通过指定 size
参数来限制返回建议的数量。
GET chess_store/_search
{
"_source": "suggestions",
"suggest": {
"product-suggestions": {
"prefix": "chess",
"completion": {
"field": "suggestions",
"size" : 3
}
}
}
}
响应包含建议
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"suggest" : {
"product-suggestions" : [
{
"text" : "chess",
"offset" : 0,
"length" : 5,
"options" : [
{
"text" : "Chess set",
"_index" : "chess_store",
"_type" : "_doc",
"_id" : "2",
"_score" : 20.0,
"_source" : {
"suggestions" : [
{
"input" : "Chess set",
"weight" : 20
},
{
"input" : "Chess pieces",
"weight" : 10
},
{
"input" : "Chess board",
"weight" : 5
}
]
}
},
{
"text" : "Chess clock",
"_index" : "chess_store",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"suggestions" : [
"Chess clock",
"Chess timer"
]
}
}
]
}
]
}
}
要利用源过滤,请在 _search
端点上使用建议功能。_suggest
端点不支持源过滤。
自动完成查询参数
下表列出了自动完成建议器查询接受的参数。
参数 | 描述 |
---|---|
field | 指定要运行查询的字段的字符串。必需。 |
size | 指定返回建议的最大数量的整数。可选。默认为 5。 |
skip_duplicates | 指定是否跳过重复建议的布尔值。可选。默认为 false 。 |
模糊自动完成查询
为了允许模糊匹配,您可以为自动完成查询指定 fuzziness
参数。在这种情况下,即使用户输入错误的搜索词,自动完成查询仍会返回结果。此外,匹配查询的前缀越长,文档的分数越高。
GET chess_store/_search
{
"suggest": {
"product-suggestions": {
"prefix": "chesc",
"completion": {
"field": "suggestions",
"size" : 3,
"fuzzy" : {
"fuzziness" : "AUTO"
}
}
}
}
}
要使用所有默认模糊选项,请指定 "fuzzy": {}
或 "fuzzy": true
。
下表列出了 fuzzy
自动完成建议器查询接受的参数。所有参数均为可选。
参数 | 描述 |
---|---|
fuzziness | 模糊度可以设置为以下之一 1. 一个整数,指定此编辑允许的最大 Damerau-Levenshtein 距离。 2. AUTO :0-2 个字符的字符串必须完全匹配,3-5 个字符的字符串允许 1 次编辑,5 个以上字符的字符串允许 2 次编辑。默认为 AUTO 。 |
min_length | 一个整数,指定输入必须达到的最小长度才能开始返回建议。如果搜索词短于 min_length ,则不返回任何建议。默认为 3。 |
prefix_length | 一个整数,指定匹配前缀必须达到的最小长度才能开始返回建议。如果 prefix_length 的前缀不匹配,但搜索词仍在 Damerau-Levenshtein 距离内,则不返回任何建议。默认为 1。 |
transpositions | 一个布尔值,指定是否将换位(相邻字符的互换)计为一次编辑而不是两次。示例:建议的 input 参数为 abcde ,fuzziness 为 1。如果 transpositions 设置为 true ,则 abdce 将匹配,但如果 transpositions 设置为 false ,则 abdce 将不匹配。默认为 true 。 |
unicode_aware | 一个布尔值,指定在测量编辑距离、换位和长度时是否使用 Unicode 码点。如果将 unicode_aware 设置为 true ,则测量速度会变慢。默认为 false ,在这种情况下,距离以字节为单位测量。 |
正则表达式查询
您可以使用正则表达式来定义自动完成建议器查询的前缀。
例如,要搜索以“a”开头且后面包含“d”的字符串,请使用以下查询
GET chess_store/_search
{
"suggest": {
"product-suggestions": {
"regex": "a.*d",
"completion": {
"field": "suggestions"
}
}
}
}
响应匹配字符串“abcde”
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"suggest" : {
"product-suggestions" : [
{
"text" : "a.*d",
"offset" : 0,
"length" : 4,
"options" : [
{
"text" : "abcde",
"_index" : "chess_store",
"_type" : "_doc",
"_id" : "2",
"_score" : 20.0,
"_source" : {
"suggestions" : [
{
"input" : "abcde",
"weight" : 20
}
]
}
}
]
}
]
}
}