Get actual count of matches in Azure Search

瘦欲@ 提交于 2019-12-11 16:09:54

问题


Azure Search returns a maximum of 1,000 results at a time. For paging on the client, I want the total count of matches in order to be able to display the correct number of paging buttons at the bottom and in order to be able to tell the user how many results there are. However, if there are over a thousand, how do I get the actual count? All I know is that there were at least 1,000 matches.

I need to be able to do this from within the SDK.


回答1:


If you want to get total number of documents in an index, one thing you could do is set 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 property of search results.

Here's a sample code for that:

        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

Please note that:

  • This count will be approximate.
  • Getting the count is an expensive operation so you should only do it with the very first request when implementing pagination.


来源:https://stackoverflow.com/questions/47083116/get-actual-count-of-matches-in-azure-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!