Elasticsearch的SearchAfter

2022-12-09 17:04:14 來源:51CTO博客


(相關資料圖)

Elasticsearch使用Search After深度分頁,分頁的方式是上一頁的最后一條數據sort里面的值來確定下一頁的位置,在分頁請求的過程中,有索引數據的增刪,會實時的反映到游標上。每一頁的數據依賴于上一頁最后一條數據,所以不能跳頁請求;

使用search_after必須要設置from=0;

最后一條數據里拿到sort屬性的值傳入到search_after;

curl 第一次請求:

index/type/_search?pretty -d{  "size": 10,  "query": {    "match": {      "age": "男"    }  },  "sort": [{    "_uid": {      "order": "desc"    }  }]}

返回:

{  "took": 28,  "timed_out": false,  "_shards": {    "total": 5,    "successful": 5,    "skipped": 0,    "failed": 0  },  "hits": {    "total": 286,    "max_score": null,    "hits": [{      "_index": "user_member",      "_type": "member_itu",      "_id": "123465",      "_score": null,      "_source": {        "userId": "123456",        "name": "123",        "updateTime": "2022-12-09 13:05"      },      "sort": [        "123465"      ]    }]  }}

curl 分頁第二次請求:

index/type/_search?pretty -d{  "size": 10,  "search_after": ["123456"],  "query": {    "match": {      "age": "男"    }  },  "sort": [{    "_uid": {      "order": "desc"    }  }]}

分頁返回:

{  "took": 28,  "timed_out": false,  "_shards": {    "total": 5,    "successful": 5,    "skipped": 0,    "failed": 0  },  "hits": {    "total": 286,    "max_score": null,    "hits": [{      "_index": "user_member",      "_type": "member_itu",      "_id": "123457",      "_score": null,      "_source": {        "userId": "123457",        "name": "124",        "updateTime": "2022-12-09 14:05"      },      "sort": [        "123457"      ]    }]  }}

標簽: 最后一條

上一篇:視點!opensips管理界面opensips-cp搭建
下一篇:全球短訊!SRv6基本原理