Azure 搜索。分页时如何获取结果计数

标签 azure azure-cognitive-search

假设我在 Azure 搜索集合中有以下架构,包含 100 条记录。

{
"id": '1',
"Status" : "Available",
"name" : "demo 1"
},
{
"id": '2',
"Status" : "Available",
"name" : "demo 1"
},
{
"id": '3',
"Status" : "Removed",
"name" : "demo 1"
},
{
"id": '4',
"Status" : "Booked",
"name" : "demo 4"
}

在“我的状态”字段中,我可以有三个不同的值, “已预订”、“有空”、“已删除”。

现在,我使用 Azure 搜索中的 Skip 和 Top 通过分页获取数据。

但是,就像在 ElasticSearch 聚合功能中一样,Azure 搜索中是否有一种方法可以获取状态为“已预订”、“可用”或“未删除”等的站点总数。 因为我无法在客户端进行计数,因为我的记录数量有限,而不是来自 Azure 搜索的所有记录。

最佳答案

如果您使用搜索索引客户端对象 (Microsoft.Azure.Search.SearchIndexClient) 来搜索文档,您可以提供一个对象作为参数 (Microsoft.Azure.Search .Models.SearchParameters),包含属性 IncludeTotalResultCount。将此属性设置为 true 并调用传递参数对象的方法 Documents.Search(...) 后,您的响应对象 (Microsoft.Azure.Search.DocumentSearchResult)将包含属性 Count 的值,考虑到过滤器的总计数,无论分页选择了多少个项目。

SearchParameters searchParameter = new SearchParameters
    {
        Filter = "organizationId eq '1'",
        Skip = 0,
        Top = 20,
        IncludeTotalResultCount = true
     };
     using (var client = new SearchIndexClient(...)
     {
        DocumentSearchResult response = client.Documents.Search("*", searchParameter);

        //pagination items
        var collection = response.Results.ToArray();

        //total items
        var counter = response.Count;
     }

关于Azure 搜索。分页时如何获取结果计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43516872/

相关文章:

azure - 仅具有内部终结点的 Azure WebRole 上的 HttpClient 是否可以调用外部 URL?

c# - 使用特殊字符时,Azure 搜索 SDK 无法进行部分搜索

azure - 将项目上传到 Azure 认知搜索索引时出现 'Request Entity Too Large' 错误 - 如何解决?

Azure 认知搜索 - 语义搜索配置错误

sql - 索引 View 及其自己的 Azure 搜索索引器行版本

c - 检测 Azure Linux VM 中的临时磁盘

从分配的用户中删除后,Azure AD 用户不再被停用

azure - 刷新 Azure 搜索索引

azure - 如何在 Xamarin UWP 应用中检索 Toast 通知消息?

python - 以 URI 文件形式访问注册数据集时出现 HTTP 错误 409 (Azure SDKv2)