【问题标题】:Azure Search. How to get result counts when having paginationAzure 搜索。分页时如何获取结果计数
【发布时间】:2018-08-20 11:06:31
【问题描述】:

假设我在 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 搜索的所有记录。

【问题讨论】:

  • 如果我理解正确,您可能希望在单个查询结果中查看聚合和记录。正确的?还是会发出 2 个单独的查询(一个用于分页数据,另一个用于聚合)?

标签: azure azure-cognitive-search


【解决方案1】:

如果您使用搜索索引客户端对象 (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;
     }

【讨论】:

    【解决方案2】:

    创建索引时,将status 字段设为facetablefilterable。然后使用$filter=status ne 'Booked' 发出关于状态的构面查询。这将为您提供除已预订以外的每个状态类别中的文档总数,而与分页无关。

    【讨论】:

    • 谢谢我最终做了facetable
    猜你喜欢
    • 1970-01-01
    • 2017-09-21
    • 2018-03-24
    • 2023-03-08
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    相关资源
    最近更新 更多