如何在 Azure 搜索中使用 top 和 skip 来纠正分页?

How to correct paging by using top and skip in Azure Search?

根据这篇文档Total hits and Page Counts,如果我想实现分页,我应该在查询中结合使用 skip 和 top。

以下是我的POST查询

{
  "count": true ,
  "search": "apple",
  "searchFields": "content",
  "select": "itemID, content",
  "searchMode": "all",
  "queryType":"full",
  "skip": 100,
  "top":100

}

它应该 return 类似于从 100 --> 200

{
"@odata.context": "https://example.search.windows.net/indexes('example-index-')/$metadata#docs(*)",
"@odata.count": 1611,
"@search.nextPageParameters": {
    "count": true,
    "search": "apple",
    "searchFields": "content",
    "select": "itemID, content",
    "searchMode": "all",
    "queryType": "full",
    "skip": 200
},

但它只是 return 前 100 个,而不是分页偏移到 100

 "@odata.context": "https://example.search.windows.net/indexes('example-index')/$metadata#docs(*)",
"@odata.count": 1611,

有什么我应该设置的吗?

关于如何实现分页的简短回答(来自 this article):将 top 设置为您想要的页面大小,然后在每次请求时将 skip 增加该页面大小。这是一个使用 GET:

的 REST API 的示例
GET /indexes/onlineCatalog/docs?search=*$top=15&$skip=0
GET /indexes/onlineCatalog/docs?search=*$top=15&$skip=15
GET /indexes/onlineCatalog/docs?search=*$top=15&$skip=30

REST API documentation also covers how to implement paging, as well as the true purpose of @odata.nextLink and @search.nextPageParameters (also covered in this related question).

引自 API 参考:

Continuation of Partial Search Responses

Sometimes Azure Search can't return all the requested results in a single Search response. This can happen for different reasons, such as when the query requests too many documents by not specifying $top or specifying a value for $top that is too large. In such cases, Azure Search will include the @odata.nextLink annotation in the response body, and also @search.nextPageParameters if it was a POST request. You can use the values of these annotations to formulate another Search request to get the next part of the search response. This is called a continuation of the original Search request, and the annotations are generally called continuation tokens. See the example in Response below for details on the syntax of these annotations and where they appear in the response body.

The reasons why Azure Search might return continuation tokens are implementation-specific and subject to change. Robust clients should always be ready to handle cases where fewer documents than expected are returned and a continuation token is included to continue retrieving documents. Also note that you must use the same HTTP method as the original request in order to continue. For example, if you sent a GET request, any continuation requests you send must also use GET (and likewise for POST).

Note

The purpose of @odata.nextLink and @search.nextPageParameters is to protect the service from queries that request too many results, not to provide a general mechanism for paging. If you want to page through results, use $top and $skip together. For example, if you want pages of size 10, your first request should have $top=10 and $skip=0, the second request should have $top=10 and $skip=10, the third request should have $top=10 and $skip=20, and so on.

下面的例子是没有top的Get方法。

获取方法

https://example.search.windows.net/indexes('example-content-index-zh')/docs?api-version=2017-11-11&search=2018&$count=true&$skip=150&select=itemID

结果:

{
"@odata.context": "https://example.search.windows.net/indexes('example-index-zh')/$metadata#docs(*)",
"@odata.count": 133063,
"value": [ //skip value ],
"@odata.nextLink": "https://example.search.windows.net/indexes('example-content-index-zh')/docs?api-version=2017-11-11&search=2018&$count=true&$skip=150"
} 

无论我用post还是获取,一旦我添加$top=# @odata.nextLink 不会显示在结果中。

终于弄明白了,虽然@odata.nextLink或者@search.nextPageParameters都不会显示。但是寻呼正在工作,我必须数一下自己。