completion字段类型
Completion 字段类型通过一个补全建议器提供自动补全功能。该补全建议器是一种前缀建议器,因此它仅匹配文本的开头部分。它会创建一个内存中的数据结构,从而提供更快的查询速度,但也会导致内存使用量增加。在使用此功能之前,您需要将所有可能的补全项列表上传到索引中。
示例
创建一个包含完成字段的映射:
PUT chess_store
{
"mappings": {
"properties": {
"suggestions": {
"type": "completion"
},
"product": {
"type": "keyword"
}
}
}
}
将搜索提示索引到UDB-SX:
PUT chess_store/_doc/1
{
"suggestions": {
"input": ["Books on openings", "Books on endgames"],
"weight" : 10
}
}
参数
以下表格列出了completion字段接受的参数。
| 参数 | 描述 |
|---|---|
| input | 一个可能的完成列表,作为字符串或字符串数组。不能包含\u0000(null)、\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" ]
}
查询completion字段类型
查询completion字段类型,指定要搜索的前缀以及要查找建议的字段名称。
查询以“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 端点不支持源过滤。
以下表格列出了模糊补全建议查询所接受的参数。所有参数都是可选的。
| 参数 | 描述 |
|---|---|
| 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
}
]
}
}
]
}
]
}
}