在上一篇文章 “Elasticsearch:運用scroll接口對大量數據實現更好的分頁”,我們講述了如何運用scroll接口來對大量數據來進行有效地分頁。在那篇文章中,我們講述了兩種方法:
- from加上size的方法來進行分頁
- 運用scroll接口來進行分頁
對於大量的數據而言,我們盡量避免使用from+size這種方法。這里的原因是index.max_result_window的默認值是10K,也就是說from+size的最大值是1萬。搜索請求占用堆內存和時間與from+size成比例,這限制了內存。假如你想hit從990到1000,那么每個shard至少需要1000個文檔:
為了避免過度使得我們的cluster繁忙,通常Scroll接口被推薦作為深層次的scrolling,但是因為維護scroll上下文也是非常昂貴的,所以這種方法不推薦作為實時用戶請求。search_after參數通過提供實時cursor來解決此問題。 我們的想法是使用上一頁的結果來幫助檢索下一頁。
我們先輸入如下的文檔到twitter索引中:
POST _bulk
{ "index" : { "_index" : "twitter", "_id": 1} }
{"user":"雙榆樹-張三", "DOB":"1980-01-01", "message":"今兒天氣不錯啊,出去轉轉去","uid":2,"age":20,"city":"北京","province":"北京","country":"中國","address":"中國北京市海淀區","location":{"lat":"39.970718","lon":"116.325747"}}
{ "index" : { "_index" : "twitter", "_id": 2 }}
{"user":"東城區-老劉", "DOB":"1981-01-01", "message":"出發,下一站雲南!","uid":3,"age":30,"city":"北京","province":"北京","country":"中國","address":"中國北京市東城區台基廠三條3號","location":{"lat":"39.904313","lon":"116.412754"}}
{ "index" : { "_index" : "twitter", "_id": 3} }
{"user":"東城區-李四", "DOB":"1982-01-01", "message":"happy birthday!","uid":4,"age":30,"city":"北京","province":"北京","country":"中國","address":"中國北京市東城區","location":{"lat":"39.893801","lon":"116.408986"}}
{ "index" : { "_index" : "twitter", "_id": 4} }
{"user":"朝陽區-老賈","DOB":"1983-01-01", "message":"123,gogogo","uid":5,"age":35,"city":"北京","province":"北京","country":"中國","address":"中國北京市朝陽區建國門","location":{"lat":"39.718256","lon":"116.367910"}}
{ "index" : { "_index" : "twitter", "_id": 5} }
{"user":"朝陽區-老王","DOB":"1984-01-01", "message":"Happy BirthDay My Friend!","uid":6,"age":50,"city":"北京","province":"北京","country":"中國","address":"中國北京市朝陽區國貿","location":{"lat":"39.918256","lon":"116.467910"}}
{ "index" : { "_index" : "twitter", "_id": 6} }
{"user":"虹橋-老吳", "DOB":"1985-01-01", "message":"好友來了都今天我生日,好友來了,什么 birthday happy 就成!","uid":7,"age":90,"city":"上海","province":"上海","country":"中國","address":"中國上海市閔行區","location":{"lat":"31.175927","lon":"121.383328"}}
這里共有6個文檔。假設檢索第一頁的查詢如下所示:
GET twitter/_search
{
"size": 2,
"query": {
"match": {
"city": "北京"
}
},
"sort": [
{
"DOB": {
"order": "asc"
}
},
{
"user.keyword": {
"order": "asc"
}
}
]
}
顯示的結果為:
{
"took" : 29,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "twitter",
"_type" : "_doc",
"_id" : "1",
"_score" : null,
"_source" : {
"user" : "雙榆樹-張三",
"DOB" : "1980-01-01",
"message" : "今兒天氣不錯啊,出去轉轉去",
"uid" : 2,
"age" : 20,
"city" : "北京",
"province" : "北京",
"country" : "中國",
"address" : "中國北京市海淀區",
"location" : {
"lat" : "39.970718",
"lon" : "116.325747"
}
},
"sort" : [
315532800000,
"雙榆樹-張三"
]
},
{
"_index" : "twitter",
"_type" : "_doc",
"_id" : "2",
"_score" : null,
"_source" : {
"user" : "東城區-老劉",
"DOB" : "1981-01-01",
"message" : "出發,下一站雲南!",
"uid" : 3,
"age" : 30,
"city" : "北京",
"province" : "北京",
"country" : "中國",
"address" : "中國北京市東城區台基廠三條3號",
"location" : {
"lat" : "39.904313",
"lon" : "116.412754"
}
},
"sort" : [
347155200000,
"東城區-老劉"
]
}
]
}
}
上述請求的結果包括每個文檔的sort值數組。 這些sort值可以與search_after參數一起使用,以開始返回在這個結果列表之后的任何文檔。 例如,我們可以使用上一個文檔的sort值並將其傳遞給search_after以檢索下一頁結果:
GET twitter/_search
{
"size": 2,
"query": {
"match": {
"city": "北京"
}
},
"search_after": [
347155200000,
"東城區-老劉"
],
"sort": [
{
"DOB": {
"order": "asc"
}
},
{
"user.keyword": {
"order": "asc"
}
}
]
}
在這里在search_after中,我們把上一個搜索結果的sort值放進來。 顯示的結果為:
{
"took" : 47,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "twitter",
"_type" : "_doc",
"_id" : "3",
"_score" : null,
"_source" : {
"user" : "東城區-李四",
"DOB" : "1982-01-01",
"message" : "happy birthday!",
"uid" : 4,
"age" : 30,
"city" : "北京",
"province" : "北京",
"country" : "中國",
"address" : "中國北京市東城區",
"location" : {
"lat" : "39.893801",
"lon" : "116.408986"
}
},
"sort" : [
378691200000,
"東城區-李四"
]
},
{
"_index" : "twitter",
"_type" : "_doc",
"_id" : "4",
"_score" : null,
"_source" : {
"user" : "朝陽區-老賈",
"DOB" : "1983-01-01",
"message" : "123,gogogo",
"uid" : 5,
"age" : 35,
"city" : "北京",
"province" : "北京",
"country" : "中國",
"address" : "中國北京市朝陽區建國門",
"location" : {
"lat" : "39.718256",
"lon" : "116.367910"
}
},
"sort" : [
410227200000,
"朝陽區-老賈"
]
}
]
}
}
注意:當我們使用search_after時,from值必須設置為0或者-1。
search_after不是自由跳轉到隨機頁面而是並行scroll多個查詢的解決方案。 它與scroll API非常相似,但與它不同,search_after參數是無狀態的,它始終針對最新版本的搜索器進行解析。 因此,排序順序可能會在步行期間發生變化,具體取決於索引的更新和刪除。