适用于不断下一页这样的查询
我这里用的releaseTimestamp倒序。
{
"query": {
"range": {
"releaseTime": {
"gte": "2019-09-20 00:00:00",
"lte": "2019-09-21 00:00:00"
}
}
},
"search_after":[ 1568995200000],
"_source": ["title","releaseTime"],
"sort": [
{
"releaseTimestamp": {
"order": "desc"
}
}
],
"size": 5
}
得到3条结果:
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "test_news",
"_type": "_doc",
"_id": "45a88261c9975aa6e0eb996c369c32de",
"_score": null,
"_source": {
"releaseTime": "2019-09-20 23:59:48",
"title": "岭南大学与五邑大学将开展居家养老联合研究-新华网"
},
"sort": [
1568995190935
]
},
{
"_index": "test_news",
"_type": "_doc",
"_id": "3ba4cc4d5ea82ff6da48109c070cb265",
"_score": null,
"_source": {
"releaseTime": "2019-09-20 23:59:48",
"title": "岭南大学与五邑大学将开展居家养老联合研究"
},
"sort": [
1568995188633
]
},
{
"_index": "test_news",
"_type": "_doc",
"_id": "45a88261c9975aa6e0eb996c369c32de",
"_score": null,
"_source": {
"releaseTime": "2019-09-20 23:59:48",
"title": "岭南大学与五邑大学将开展居家养老联合研究-新华网"
},
"sort": [
1568995185201
]
}
]
}
接下来用第二条数据的sort值 放到search_after中再次查询:
{
"query": {
"range": {
"releaseTime": {
"gte": "2019-09-20 00:00:00",
"lte": "2019-09-21 00:00:00"
}
}
},
"search_after":[ 1568995188633],
"_source": ["title","releaseTime"],
"sort": [
{
"releaseTimestamp": {
"order": "desc"
}
}
],
"size": 5
}
结果会得到上次查询的第二条之后的数据:
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "test_news",
"_type": "_doc",
"_id": "45a88261c9975aa6e0eb996c369c32de",
"_score": null,
"_source": {
"releaseTime": "2019-09-20 23:59:48",
"title": "岭南大学与五邑大学将开展居家养老联合研究-新华网"
},
"sort": [
1568995185201
]
}
]
}
如果是多个字段排序的话,search_after值的顺序要与我们排序条件的顺序一致
还有使用search_after时 from设置为0,-1或者直接不加from
使用search_after 进行分页 相比from&size的方式要更加高效,而且在不断有新数据入库的时候仅仅使用from和size分页会有重复的情况
相比使用scroll分页,search_after可以进行实时的查询
不过search_after不适合跳跃式的分页