假设我在 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/