获取 Azure 搜索中的实际匹配数

Get actual count of matches in Azure Search

Azure 搜索 returns 一次最多 1,000 个结果。对于客户端上的分页,我想要 总计数 的匹配项,以便能够在底部显示正确数量的分页按钮,并能够告诉用户如何有很多结果。但是,如果有超过一千个,我如何获得实际数量?我只知道有 至少 1,000 场比赛。

我需要能够从 SDK 中执行此操作。

如果您想获得索引中的文档总数,您可以做的一件事是设置 IncludeTotalResultCount to true in your search parameters. Once you do that when you execute the query, you will see the count of total documents in an index in Count 属性 个搜索结果。

这是一个示例代码:

        var credentials = new SearchCredentials("account-key (query or admin key)");
        var indexClient = new SearchIndexClient("account-name", "index-name", credentials);
        var searchParameters = new SearchParameters()
        {
            QueryType = QueryType.Full,
            IncludeTotalResultCount = true
        };
        var searchResults = await indexClient.Documents.SearchAsync("*", searchParameters);
        Console.WriteLine("Total documents in index (approx) = " + searchResults.Count.GetValueOrDefault());//Prints the total number of documents in the index

请注意:

  • 此计数为近似值。
  • 获取计数是一项昂贵的操作,因此在实现分页时,您应该只在第一个请求时执行此操作。

对于使用 POST API 的 REST 客户端,只需将 "count": "true" 添加到负载中。您在 @odata.count.

中得到计数

参考:https://docs.microsoft.com/en-us/rest/api/searchservice/search-documents