折叠处理器
2.12 版本引入
折叠响应处理器会丢弃结果集中与前一个文档在特定字段上具有相同值的匹配项。这类似于在搜索请求中传递 collapse
参数,但响应处理器是在从所有分片获取数据后应用于响应的。折叠响应处理器可以与 rescore
搜索请求参数结合使用,也可以在重排序响应处理器之后应用。
使用折叠响应处理器可能会导致返回的结果少于 size
个,因为匹配项是从一个大小已经小于或等于 size
的集合中丢弃的。为了增加返回 size
个匹配项的可能性,请使用 oversample
请求处理器和 truncate_hits
响应处理器,如本示例所示。
请求正文字段
下表列出了所有请求字段。
字段 | 数据类型 | 描述 |
---|---|---|
字段 | 字符串 | 将从每个返回的搜索匹配项中读取其值的字段。搜索响应中仅返回每个给定字段值的第一个匹配项。必需。 |
context_prefix | 字符串 | 可用于从特定范围读取 original_size 变量,以避免冲突。可选。 |
标签 | 字符串 | 处理器的标识符。可选。 |
描述 | 字符串 | 处理器的描述。可选。 |
忽略失败 | 布尔型 | 如果为 true ,OpenSearch 将忽略此处理器的任何失败,并继续运行搜索管道中的其余处理器。可选。默认为 false 。 |
示例
以下示例演示了如何使用带有折叠处理器的搜索管道。
设置
创建包含用于折叠字段的多个文档
POST /_bulk
{ "create":{"_index":"my_index","_id":1}}
{ "title" : "document 1", "color":"blue" }
{ "create":{"_index":"my_index","_id":2}}
{ "title" : "document 2", "color":"blue" }
{ "create":{"_index":"my_index","_id":3}}
{ "title" : "document 3", "color":"red" }
{ "create":{"_index":"my_index","_id":4}}
{ "title" : "document 4", "color":"red" }
{ "create":{"_index":"my_index","_id":5}}
{ "title" : "document 5", "color":"yellow" }
{ "create":{"_index":"my_index","_id":6}}
{ "title" : "document 6", "color":"yellow" }
{ "create":{"_index":"my_index","_id":7}}
{ "title" : "document 7", "color":"orange" }
{ "create":{"_index":"my_index","_id":8}}
{ "title" : "document 8", "color":"orange" }
{ "create":{"_index":"my_index","_id":9}}
{ "title" : "document 9", "color":"green" }
{ "create":{"_index":"my_index","_id":10}}
{ "title" : "document 10", "color":"green" }
创建一个仅在 color
字段上折叠的管道
PUT /_search/pipeline/collapse_pipeline
{
"response_processors": [
{
"collapse" : {
"field": "color"
}
}
]
}
使用搜索管道
在此示例中,您在 color
字段上进行折叠之前请求了前三个文档。由于前两个文档具有相同的 color
,第二个文档被丢弃,请求返回第一个和第三个文档。
POST /my_index/_search?search_pipeline=collapse_pipeline
{
"size": 3
}
响应
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 10,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "my_index",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"title" : "document 1",
"color" : "blue"
}
},
{
"_index" : "my_index",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"title" : "document 3",
"color" : "red"
}
}
]
},
"profile" : {
"shards" : [ ]
}
}