跟著這篇走完,你會把搜索結果「翻頁」這件事配齊:淺翻頁用 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 關閉。
