在Elasticsearch中,我們可以通過size和from來對我們的結果來進行分頁。但是對於數據量很大的索引,這是有效的嗎?Scroll API可用於從單個搜索請求中檢索大量結果(甚至所有結果),這與在傳統數據庫上使用cursor的方式非常相似。Scroll不是用於實時用戶請求,而是用於處理大量數據,例如,用於處理大量數據。 為了將一個索引的內容重新索引到具有不同配置的新索引中。
為了說明問題,我們今天先創建一個叫做twitter的Index:
POST _bulk
{ "index" : { "_index" : "twitter", "_id": 1} }
{"user":"雙榆樹-張三","message":"今兒天氣不錯啊,出去轉轉去","uid":2,"age":20,"city":"北京","province":"北京","country":"中國","address":"中國北京市海淀區","location":{"lat":"39.970718","lon":"116.325747"}}
{ "index" : { "_index" : "twitter", "_id": 2 }}
{"user":"東城區-老劉","message":"出發,下一站雲南!","uid":3,"age":30,"city":"北京","province":"北京","country":"中國","address":"中國北京市東城區台基廠三條3號","location":{"lat":"39.904313","lon":"116.412754"}}
{ "index" : { "_index" : "twitter", "_id": 3} }
{"user":"東城區-李四","message":"happy birthday!","uid":4,"age":30,"city":"北京","province":"北京","country":"中國","address":"中國北京市東城區","location":{"lat":"39.893801","lon":"116.408986"}}
{ "index" : { "_index" : "twitter", "_id": 4} }
{"user":"朝陽區-老賈","message":"123,gogogo","uid":5,"age":35,"city":"北京","province":"北京","country":"中國","address":"中國北京市朝陽區建國門","location":{"lat":"39.718256","lon":"116.367910"}}
{ "index" : { "_index" : "twitter", "_id": 5} }
{"user":"朝陽區-老王","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":"虹橋-老吳","message":"好友來了都今天我生日,好友來了,什么 birthday happy 就成!","uid":7,"age":90,"city":"上海","province":"上海","country":"中國","address":"中國上海市閔行區","location":{"lat":"31.175927","lon":"121.383328"}}
在上面,我們創建了6個文檔。這些文檔的數量雖然不是很多,但是我們想為了說明問題的方便。在實際的使用中,我們可能有成百上千的文檔。
下面,我們通過size和from的方法來進行分頁。假如我們把sizs設置為2,那么,我們可以通過如下寫的方法來進行分頁。
GET twitter/_search?size=2&from=0
GET twitter/_search?size=2&from=2
GET twitter/_search?size=2&from=4
這樣,我們每次可以得到2個文檔,從而對我們的Index進行分頁。我們可以得到這些數據並在自己的頁面上或應用里進行展示。通常這樣的每個請求返回的上線是10K。如果超過這個上限的話,這樣的方法將不再適合。
上面的這種方法,對於小量的數據是可行的,但是對於大量的數據,而且我們需要進行sort時,這個有可能變得力不從心,比如:
GET twitter/_search
{
"query": {
"match": {
"city": "北京"
}
},
"from": 2,
"size": 2,
"sort": [
{
"user.keyword": {
"order": "desc"
}
}
]
}
你可以想象當你更深入地進行分頁時,它會變得多么低效。 例如,如果更改mapping並希望將所有現有數據重新索引到新索引中,您可能沒有足夠的內存來對所有結果進行排序以返回最后一頁的數據。
對於這種應用場景,你可以使用scan搜索類型。我們可以這么做:
1. 使用scroll來返回一個初始的搜索,並返回一個scroll ID
GET twitter/_search?scroll=1m
{
"query": {
"match": {
"city": "北京"
}
},
"size": 2
}
這里的scroll=1m,表明Elasticsearch允許等待的時間是1分鍾。如果在一分鍾之內,接下來的scroll請求沒有到達的話,那么當前的請求的上下文將會丟失。
返回的結果是:
{
"_scroll_id" : "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAFh8WWUdCVlRMUllRb3UzMkdqb0IxVnZNUQ==",
"took" : 31,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 0.48232412,
"hits" : [
{
"_index" : "twitter",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.48232412,
"_source" : {
"user" : "雙榆樹-張三",
"message" : "今兒天氣不錯啊,出去轉轉去",
"uid" : 2,
"age" : 20,
"city" : "北京",
"province" : "北京",
"country" : "中國",
"address" : "中國北京市海淀區",
"location" : {
"lat" : "39.970718",
"lon" : "116.325747"
}
}
},
{
"_index" : "twitter",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.48232412,
"_source" : {
"user" : "東城區-老劉",
"message" : "出發,下一站雲南!",
"uid" : 3,
"age" : 30,
"city" : "北京",
"province" : "北京",
"country" : "中國",
"address" : "中國北京市東城區台基廠三條3號",
"location" : {
"lat" : "39.904313",
"lon" : "116.412754"
}
}
}
]
}
}
在這里,我們可以看到一個返回的_scroll_id。這個_scroll_id將會被用於接下來的請求。
2. 使用_scroll_id,再次請求
利用上次請求返回來的_scroll_id,再次請求以獲得下一個page的信息:
GET _search/scroll
{
"scroll": "1m",
"scroll_id":"DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAHC8WWUdCVlRMUllRb3UzMkdqb0IxVnZNUQ=="
}
在這里必須指出的是:
- 這里填寫的scroll_id是上一個請求返回的值
- 這個scroll_id的有效期是我們在第一次搜索時定義的1m,也就是1分鍾。如果超過了,這個就沒有用
運行后返回的結果是:
{
"_scroll_id" : "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAHS0WWUdCVlRMUllRb3UzMkdqb0IxVnZNUQ==",
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 0.48232412,
"hits" : [
{
"_index" : "twitter",
"_type" : "_doc",
"_id" : "3",
"_score" : 0.48232412,
"_source" : {
"user" : "東城區-李四",
"message" : "happy birthday!",
"uid" : 4,
"age" : 30,
"city" : "北京",
"province" : "北京",
"country" : "中國",
"address" : "中國北京市東城區",
"location" : {
"lat" : "39.893801",
"lon" : "116.408986"
}
}
},
{
"_index" : "twitter",
"_type" : "_doc",
"_id" : "4",
"_score" : 0.48232412,
"_source" : {
"user" : "朝陽區-老賈",
"message" : "123,gogogo",
"uid" : 5,
"age" : 35,
"city" : "北京",
"province" : "北京",
"country" : "中國",
"address" : "中國北京市朝陽區建國門",
"location" : {
"lat" : "39.718256",
"lon" : "116.367910"
}
}
}
]
}
}
顯然這次返回的是2個文檔。我們需要再次使用同樣的辦法來得到最后一個page的結果:
{
"_scroll_id" : "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAADeoWeno1UkF2RWZRd202VW1HQXRlOWFUdw==",
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 0.48232412,
"hits" : [
{
"_index" : "twitter",
"_type" : "_doc",
"_id" : "5",
"_score" : 0.48232412,
"_source" : {
"user" : "朝陽區-老王",
"message" : "Happy BirthDay My Friend!",
"uid" : 6,
"age" : 50,
"city" : "北京",
"province" : "北京",
"country" : "中國",
"address" : "中國北京市朝陽區國貿",
"location" : {
"lat" : "39.918256",
"lon" : "116.467910"
}
}
}
]
}
}
顯然,這次返回的結果只有一個數值,比我們請求的page大小2要小。如果我們利用返回的_scroll_id再次請求時,我們可以看返回的結果是:
{
"_scroll_id" : "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAADeoWeno1UkF2RWZRd202VW1HQXRlOWFUdw==",
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 0.48232412,
"hits" : [ ]
}
}
這次是一個結果都沒有。
如果完成此過程,則需要清理上下文,因為上下文在超時之前仍會占用計算資源。 如下面的屏幕快照所示,您可以使用scroll_id參數在DELETE API中指定一個或多個上下文:
DELTE_search/scroll
{
"scroll_id":"DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAHC8WWUdCVlRMUllRb3UzMkdqb0IxVnZNUQ=="
}
參考:
【1】Elasticsearch Scroll
【2】https://www.elastic.co/guide/en/elasticsearch/reference/7.4/search-request-body.html#request-body-search-scroll
