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

问题描述 投票:0回答:2

根据这个文件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

}

它应该返回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
},

但它只返回前100名,而不是分页偏移到100

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

我应该设置什么东西?

azure azure-search
2个回答
1
投票

关于如何实现分页的简短答案(来自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还介绍了如何实现分页,以及@odata.nextLink@search.nextPageParameters的真正目的(也包含在this related question中)。

引用API参考:

继续部分搜索响应

有时Azure搜索无法在单个搜索响应中返回所有请求的结果。这可能由于不同的原因而发生,例如当查询通过不指定$top或指定$top的值太大而请求太多文档时。在这种情况下,Azure搜索将在响应正文中包含@odata.nextLink注释,如果是POST请求,还包括@search.nextPageParameters。您可以使用这些注释的值来制定另一个搜索请求,以获取搜索响应的下一部分。这称为原始搜索请求的延续,注释通常称为延续令牌。有关这些注释语法的详细信息以及它们在响应正文中的显示位置,请参阅下面的“响应”中的示例。

Azure搜索可能返回延续令牌的原因是特定于实现的,并且可能会发生变化。强大的客户端应始终准备好处理返回的文档少于预期的情况,并包含继续令牌以继续检索文档。另请注意,您必须使用与原始请求相同的HTTP方法才能继续。例如,如果您发送了GET请求,则您发送的任何继续请求也必须使用GET(同样也适用于POST)。

注意

@odata.nextLink@search.nextPageParameters的目的是保护服务免受请求过多结果的查询,而不是提供分页的一般机制。如果您想翻阅结果,请同时使用$top$skip。例如,如果你想要大小为10的页面,你的第一个请求应该有$top=10$skip=0,第二个请求应该有$top=10$skip=10,第三个请求应该有$top=10$skip=20,依此类推。


-2
投票

以下示例是不带顶部的Get方法。

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还是get,onces都添加$ top =#@ odata.nextLink将不会显示在结果中。

最后,我想出来虽然@ odata.nextLink或@ search.nextPageParameters不会显示。但是分页工作正常,我必须自己算一下。

© www.soinside.com 2019 - 2024. All rights reserved.