1、如何使用es進行分頁搜尋的文法
size,from
GET test_index/test_type/_search?size=10&from=0
分頁的上機實驗
GET test_index/test_type/_search
運作結果
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "8",
"_score": 1,
"_source": {
"num": 1,
"tags": []
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "2",
"_score": 1,
"_source": {
"test_field": "test2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "6",
"_score": 1,
"_source": {
"test_field1": "test1",
"test_field2": "test123"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_score": 1,
"_source": {
"test_field": "test123"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "3",
"_score": 1,
"_source": {
"test_field": "test3"
}
}
]
}
}
我們發現有5條資料,現在我們假設把這5條資料分成2頁,每頁3條資料,來實驗分頁搜尋的效果。
第一頁:
GET test_index/test_type/_search?from=0&size=3
運作結果
{
"took": 21,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "8",
"_score": 1,
"_source": {
"num": 1,
"tags": []
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "2",
"_score": 1,
"_source": {
"test_field": "test2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "6",
"_score": 1,
"_source": {
"test_field1": "test1",
"test_field2": "test123"
}
}
]
}
}
第二頁
GET test_index/test_type/_search?from=3&size=3
運作結果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_score": 1,
"_source": {
"test_field": "test123"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "3",
"_score": 1,
"_source": {
"test_field": "test3"
}
}
]
}
}
第一頁:id=8,2,6
第二頁:id=1,3
2、什麼是deep paging問題,為什麼會産生這個問題,它的底層原理是什麼?
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0TPR50MVpWT4FEVOBDOsJGcohVYsR2MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL2AjM4UDM1kDM0ETOwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)