跟着这篇走完,你会把搜索结果「翻页」这件事配齐:浅翻页用 from + size、翻过 10000 条的上限后改用 search_after 游标、加 tiebreaker 唯一字段防止跳页、要翻页一致性再加 PIT,最后用 scroll 做全量导出。整个过程 6 步,每步一条验证命令。环境沿用从零跑通 OpenSearch 本地搜索服务:端口 9200、用户 admin、密码 Devfix@2026;你换了值替换掉就行。
先说清一个贯穿整篇的认知:OpenSearch 有四种翻页方式,各管一段场景,没有一种能通吃。from/size 简单但有 10000 条硬上限,search_after 能翻深但不能跳页,PIT 能保证翻页过程数据一致,scroll 只适合全量导出。这篇默认你已经跑通了本地搜索服务,Docker 启动、建索引、bulk 导入、curl 带 -sk -u admin:Devfix@2026 的写法全部复用,不再重复。

前置条件
| 项 | 要求 | 说明 |
|---|---|---|
| OpenSearch | 3.x(Docker latest 镜像) | 单节点,security 开启,HTTPS 端口 9200 |
| 前置文章 | 已跑通 opensearch-local-search | 复用容器、密码、curl 写法 |
| curl | 任意版本 | 全文用 curl 调 REST API |
先确认服务活着:
curl -sk -u admin:Devfix@2026 "https://localhost:9200"
返回里有 cluster_name 就满足条件。报连接失败说明容器没起来,回第一篇第二步。
第一步:建 movies 索引,灌 5 条电影(year 排序 + id 唯一字段)
深分页的两个关键字段先备好:year(可排序)、id(唯一值,做 tiebreaker)。建索引,id 用 keyword 类型:
curl -sk -u admin:Devfix@2026 -X PUT "https://localhost:9200/movies?pretty" \
-H "Content-Type: application/json" \
-d '{
"settings": { "index.number_of_shards": 1, "index.number_of_replicas": 0 },
"mappings": {
"properties": {
"id": { "type": "keyword" },
"title": { "type": "text" },
"year": { "type": "integer" },
"genre": { "type": "keyword" }
}
}
}'
curl -sk -u admin:Devfix@2026 -X POST "https://localhost:9200/movies/_bulk?pretty" \
-H "Content-Type: application/x-ndjson" \
--data-binary '{"index":{"_id":"1"}}
{"id":"1","title":"Let the Wind Rise","year":2013,"genre":"drama"}
{"index":{"_id":"2"}}
{"id":"2","title":"Gone with the Wind","year":1939,"genre":"drama"}
{"index":{"_id":"3"}}
{"id":"3","title":"The Rise of Machines","year":2024,"genre":"sci-fi"}
{"index":{"_id":"4"}}
{"id":"4","title":"Windmill Valley","year":2020,"genre":"drama"}
{"index":{"_id":"5"}}
{"id":"5","title":"The Martian","year":2015,"genre":"sci-fi"}
'
{
"errors" : false,
"items" : [ { "index" : { "_id" : "1", "status" : 201 } }, ... ]
}
errors 是 false 就进去了。id 字段是唯一的 keyword 值,第五步会用到它当 tiebreaker。
验证命令:
curl -sk -u admin:Devfix@2026 "https://localhost:9200/movies/_count?pretty"
{
"count" : 5,
"_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }
}
返回 "count": 5 就通过。
第二步:from + size 基本分页——浅翻页
最简单的是 from + size:from 是「跳过前几条」,size 是「返回几条」,翻第 N 页用 from = size * (N - 1)。先按 year 升序排,每页 2 条:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "from": 0, "size": 2, "sort": [ { "year": "asc" }, { "id": "asc" } ] }'
{
"hits" : {
"total" : { "value" : 5, "relation" : "eq" },
"hits" : [
{ "_id" : "2", "_source" : { "title" : "Gone with the Wind", "year" : 1939 } },
{ "_id" : "1", "_source" : { "title" : "Let the Wind Rise", "year" : 2013 } }
]
}
}
第 1 页拿到 1939、2013 两条(按 year 升序)。翻第 2 页:from = 2 * (2 - 1) = 2:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "from": 2, "size": 2, "sort": [ { "year": "asc" }, { "id": "asc" } ] }'
拿到 2015、2020 两条。from/size 就是这么简单——跳过前 N 条、返回后 M 条。
验证命令:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "from": 2, "size": 2, "sort": [ { "year": "asc" }, { "id": "asc" } ] }' \
| grep -o '"title" : "[A-Za-z ]*"'
能看到 The Martian、Windmill Valley 就通过。
第三步:from + size 触顶 max_result_window——为什么要深分页
from/size 的隐患是:from + size 的和不能超过 index.max_result_window(默认 10000)。因为 from 越大,每个分片要临时加载、排序的文档越多,翻到第 10 万条时内存和 CPU 代价会拖垮集群。本地只有 5 条演示不了 10000,我们把上限临时调到 3 来复现这个报错:
curl -sk -u admin:Devfix@2026 -X PUT "https://localhost:9200/movies/_settings?pretty" \
-H "Content-Type: application/json" \
-d '{ "index.max_result_window": 3 }'
{
"acknowledged" : true
}
现在 from + size 超过 3 就报错。from: 2, size: 2(和 = 4 > 3):
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "from": 2, "size": 2 }'
{
"error" : {
"type" : "search_phase_execution_exception",
"reason" : "Result window is too large, from + size must be less than or equal to: [3] but was [4]"
}
}
这就是 from/size 的硬天花板。生产上默认是 10000,翻到 10000 条之后就必须换路子——见常见问题第 1 项。注意这只是演示,先把上限调回来:
curl -sk -u admin:Devfix@2026 -X PUT "https://localhost:9200/movies/_settings?pretty" \
-H "Content-Type: application/json" \
-d '{ "index.max_result_window": 10000 }'
验证命令:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "from": 2, "size": 2 }' \
| grep -o 'Result window is too large'
能 grep 到 Result window is too large(在把上限调回 10000 之前执行)就通过。
第四步:search_after 深分页——游标式翻页(承重步)
这是全文承重的一步。突破 10000 上限的办法是 search_after:它不是「跳到第 N 页」,而是「从上一页最后一条的 sort 值继续往后翻」。每次请求带上上一页最后一条的排序值,引擎就从那个位置往后取,所以没有「跳过前 N 条」的开销,也就没有 10000 上限。
先跑第一页(size: 2,按 year 升序 + id 升序),记住最后一条的 sort 值:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "size": 2, "sort": [ { "year": "asc" }, { "id": "asc" } ] }'
{
"hits" : {
"hits" : [
{ "_id" : "2", "_source" : { "title" : "Gone with the Wind", "year" : 1939 }, "sort" : [ 1939, "2" ] },
{ "_id" : "1", "_source" : { "title" : "Let the Wind Rise", "year" : 2013 }, "sort" : [ 2013, "1" ] }
]
}
}
注意每条 hit 多了个 sort 数组([year, id])。翻下一页,把最后一条的 sort 值 [2013, "1"] 传给 search_after:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "size": 2, "sort": [ { "year": "asc" }, { "id": "asc" } ], "search_after": [ 2013, "1" ] }'
{
"hits" : {
"hits" : [
{ "_id" : "5", "_source" : { "title" : "The Martian", "year" : 2015 }, "sort" : [ 2015, "5" ] },
{ "_id" : "4", "_source" : { "title" : "Windmill Valley", "year" : 2020 }, "sort" : [ 2020, "4" ] }
]
}
}
拿到了 2013 之后的 2015、2020 两条。核心观察点:
- 游标式,不是跳页式。
search_after: [2013, "1"]说的是「从 year=2013、id=1 这条之后继续」,不是「跳到第 3 页」。 search_after时必须from为 0 或省略,且必须排序。- 只能一页页前进,不能跳页。这是它和
from/size的根本区别:from/size能直接跳第 10 页,search_after只能顺着翻。对「下一页/加载更多」的无限滚动场景正合适。
验证命令:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "size": 2, "sort": [ { "year": "asc" }, { "id": "asc" } ], "search_after": [ 2013, "1" ] }' \
| grep -o '"title" : "The Martian"'
能 grep 到 The Martian 就通过。
第五步:tiebreaker 唯一值字段——防止跳页/重复
search_after 依赖「稳定的排序顺序」。如果 sort 只按 year 排、而多个文档 year 相同,游标 search_after: [2013] 就不知道该从哪个同年的文档继续——页间会漏文档或重复文档。所以 sort 数组最后一个字段必须是唯一值,作为 tiebreaker(决胜字段):
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "size": 2, "sort": [ { "year": "asc" }, { "id": "asc" } ], "search_after": [ 2020, "4" ] }'
id 是唯一 keyword 字段,做 tiebreaker 后 [year, id] 能唯一定位「上次翻到哪」,不会漏不会重。官方特别提醒:别用 _id 当 tiebreaker——它没有 doc values(排序要把所有 id 加载进内存),且只在单索引内唯一。所以我们在第一步专门建了 id 字段。
对比一下「没 tiebreaker」的后果:只按 year 排,search_after: [2013] 无法唯一定位,翻页会跳或重——见常见问题第 2 项。
验证命令:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "size": 2, "sort": [ { "year": "asc" }, { "id": "asc" } ], "search_after": [ 2020, "4" ] }' \
| grep -o '"title" : "The Rise of Machines"'
能 grep 到 The Rise of Machines(最后一页最后一条)就通过。
第六步:PIT + search_after 保证翻页一致性 + 收场
search_after 是无状态的,翻页过程中如果有文档被索引或删除,页序会变、可能漏或重。要「翻页过程数据固定不变」,用 PIT(Point in Time)——它是一份时间点快照。先创建 PIT:
curl -sk -u admin:Devfix@2026 -X POST "https://localhost:9200/movies/_search/point_in_time?keep_alive=1m&pretty"
{
"pit_id" : "o9y7DDE...(一段 base64 串)"
}
拿 pit_id 搜索,翻页时带上它(注意走 GET /_search,不带索引名,因为 PIT 已经锁定了索引):
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "size": 2, "pit": { "id": "<你的 pit_id>", "keep_alive": "1m" }, "sort": [ { "year": "asc" }, { "id": "asc" } ], "search_after": [ 1939, "2" ] }'
PIT + search_after 是官方推荐的深分页首选——它操作在「冻结的时间快照」上,翻页前后一致,也能前进后退。PIT 也是「有状态」的,用完要删:
curl -sk -u admin:Devfix@2026 -X DELETE "https://localhost:9200/_search/point_in_time?pretty" \
-H "Content-Type: application/json" \
-d '{ "pit_id": "<你的 pit_id>" }'
{
"pits" : [ { "successful" : true, "pit_id" : "..." } ]
}
验证命令:
curl -sk -u admin:Devfix@2026 -X POST "https://localhost:9200/movies/_search/point_in_time?keep_alive=1m&pretty" \
| grep -o '"pit_id"'
能 grep 到 "pit_id" 就通过。
最后收场:演示索引用完删掉即可(全量导出场景用 scroll,见常见问题第 3 项):
curl -sk -u admin:Devfix@2026 -X DELETE "https://localhost:9200/movies?pretty"
{
"acknowledged" : true
}
核心概念
这张图把四种翻页方式对着场景摆清楚:浅翻页(低于 10000 条)用 from/size、深翻页(超过 10000、实时、下一页)用 search_after、要一致性快照加 PIT、全量导出用 scroll。每种各管一段,没有通吃的。

| 方式 | 怎么用 | 上限 | 适用场景 | 代价 |
|---|---|---|---|---|
from + size |
from = size * (N-1) |
from + size ≤ 10000 |
浅分页、能跳页 | 越深越慢 |
search_after |
上一页最后 sort 值做游标 |
无 10000 上限 | 深分页、无限滚动 | 不能跳页,要 tiebreaker |
PIT + search_after |
快照 + 游标 | 无 10000 上限 | 翻页要一致性 | 占资源,用完要删 |
scroll |
_search/scroll 翻批 |
任意条数 | 全量导出、批处理 | 有状态,用完要关 |
要点回顾
from + size有 10000 上限——max_result_window默认 10000,翻深了报Result window is too large,这是浅分页的天花板。search_after是游标不是跳页——拿上一页最后一条的sort值继续翻,突破 10000 上限,但只能一页页前进。sort最后字段要唯一(tiebreaker)——否则同值文档顺序不稳,翻页会漏或重;别用_id当 tiebreaker。- 要一致性加 PIT、全量导出用 scroll——PIT 是时间快照,scroll 是有状态批处理,两者用完都要释放。
常见问题
from+size 翻页报「result window is too large」
现象:from + size 超过 10000,报 Result window is too large。
原因:from + size 不能超过 index.max_result_window(默认 10000),是 from/size 深度分页的硬上限。
search_after 翻页跳页或重复
现象:用 search_after 翻页,有的文档被跳过、有的重复出现。
原因:sort 数组最后一个字段不是唯一值(缺 tiebreaker),同排序值的文档顺序不稳定,页间就漏或重。
scroll 用完没关,磁盘和堆越占越多
现象:scroll 全量导出后没关闭,节点磁盘、堆内存持续上涨。
原因:scroll 保持搜索上下文(快照)占用资源,锁住旧 segment 无法合并;用完必须 DELETE _search/scroll 关闭。
