混合查询结果分页
2.19 版本引入
您可以通过在混合查询子句中使用 pagination_depth
参数以及标准的 from
和 size
参数,对混合查询结果应用分页。pagination_depth
参数定义了每个子查询可以从每个分片检索的最大搜索结果数。例如,将 pagination_depth
设置为 50
,表示每个分片每个子查询最多可以有 50 个结果保留在内存中。
要浏览结果,请使用 from
和 size
参数
from
:指定您希望从哪个文档编号开始显示结果。默认值为0
。size
:指定每页返回的结果数量。默认值为10
。
例如,要从第 20 个文档开始显示 10 个文档,请指定 from: 20
和 size: 10
。有关分页的更多信息,请参阅 结果分页。
pagination_depth 对混合搜索结果的影响
更改 pagination_depth
会影响在应用任何排名、过滤或分页调整之前检索到的底层搜索结果集。这是因为 pagination_depth
决定了每个子查询从每个分片检索到的结果数量,这最终可能在归一化后改变结果顺序。为确保分页一致性,在页面之间导航时请保持 pagination_depth
值不变。
默认情况下,没有分页的混合搜索使用 from + size
公式检索结果,其中 from
始终为 0
。
要启用更深层分页,请增加 pagination_depth
值。然后,您可以使用 from
和 size
参数浏览结果。请注意,更深层分页可能会影响搜索性能,因为检索和处理更多结果需要额外的计算资源。
以下示例显示了一个搜索请求,其配置为 from: 0
、size: 5
和 pagination_depth: 10
。这意味着在应用分页之前,bool
和 term
查询将从每个分片中检索最多 10 个搜索结果。
GET /my-nlp-index/_search?search_pipeline=nlp-search-pipeline
{
"size": 5,
"query": {
"hybrid": {
"pagination_depth":10,
"queries": [
{
"term": {
"category": "permission"
}
},
{
"bool": {
"should": [
{
"term": {
"category": "editor"
}
},
{
"term": {
"category": "statement"
}
}
]
}
}
]
}
}
}
响应包含前五个结果
{
"hits": {
"total": {
"value": 6,
"relation": "eq"
},
"max_score": 0.5,
"hits": [
{
"_index": "my-nlp-index",
"_id": "d3eXlZQBJkWerFzHv4eV",
"_score": 0.5,
"_source": {
"category": "permission",
"doc_keyword": "workable",
"doc_index": 4976,
"doc_price": 100
}
},
{
"_index": "my-nlp-index",
"_id": "eneXlZQBJkWerFzHv4eW",
"_score": 0.5,
"_source": {
"category": "editor",
"doc_index": 9871,
"doc_price": 30
}
},
{
"_index": "my-nlp-index",
"_id": "e3eXlZQBJkWerFzHv4eW",
"_score": 0.5,
"_source": {
"category": "statement",
"doc_keyword": "entire",
"doc_index": 8242,
"doc_price": 350
}
},
{
"_index": "my-nlp-index",
"_id": "fHeXlZQBJkWerFzHv4eW",
"_score": 0.24999997,
"_source": {
"category": "statement",
"doc_keyword": "idea",
"doc_index": 5212,
"doc_price": 200
}
},
{
"_index": "index-test",
"_id": "fXeXlZQBJkWerFzHv4eW",
"_score": 5.0E-4,
"_source": {
"category": "editor",
"doc_keyword": "bubble",
"doc_index": 1298,
"doc_price": 130
}
}
]
}
}
以下搜索请求配置为 from: 6
、size: 5
和 pagination_depth: 10
。pagination_depth
保持不变,以确保分页基于相同的搜索结果集。
GET /my-nlp-index/_search?search_pipeline=nlp-search-pipeline
{
"size":5,
"from":6,
"query": {
"hybrid": {
"pagination_depth":10,
"queries": [
{
"term": {
"category": "permission"
}
},
{
"bool": {
"should": [
{
"term": {
"category": "editor"
}
},
{
"term": {
"category": "statement"
}
}
]
}
}
]
}
}
}
响应排除了前五项并显示了剩余结果。
{
"hits": {
"total": {
"value": 6,
"relation": "eq"
},
"max_score": 0.5,
"hits": [
{
"_index": "index-test",
"_id": "fneXlZQBJkWerFzHv4eW",
"_score": 5.0E-4,
"_source": {
"category": "editor",
"doc_keyword": "bubble",
"doc_index": 521,
"doc_price": 75
}
}
]
}
}