How to use Microsoft.Azure.Search SearchContinuationToken
Asked Answered
M

2

9

Can someone point me in the right direction of how to implement this?

So far I can query my index and get a response

SearchIndexClient indexClient = service.Indexes.GetClient(indexName);
 SearchParameters sp = new SearchParameters()
        {
            Facets = new string[] { "Brand", "Price,values:10|25|100|500|1000|2500" },
            Filter = AzureUtils.BuildFilter(brand, priceFrom, priceTo),
            OrderBy = new string[] { "" },
            IncludeTotalResultCount = true,
            Top = 9,
            SearchMode = SearchMode.Any
        };
        DocumentSearchResponse<MyClass> response = response = indexClient.Documents.Search<MyClass>(searchText, sp);

When the result comes back the response.ContinuationToken is null

How can I get my index to return a value for the response.ContinuationToken property.

Also, how do I can implement this to fetch the next 9 results?

Thanks

Mandatory answered 20/11, 2015 at 12:26 Comment(2)
What is the total result count returned in the response?Implant
The result is greater than 0. The total varies based on my search query but is usually between 1 and 50Mandatory
M
10

I found my answer here - Link

This property will be null unless you request more results than the page size. This can happen either when you do not specify Top and there are more than 50 results (default page size is 50), or when you specify a value greater than 1000 for Top and there are more than 1000 results (maximum page size is 1000). In either case, you can pass the value of this property to the ContinueSearchAsync method to retrieve the next page of results.

I hope that it helps someone else if they get stuck like me

Mandatory answered 21/11, 2015 at 9:44 Comment(1)
This answer whilst matching the documentation doesn't help, OP and myself had the same understanding as explained here, but when no continuation token is returned when we know there are more results we come right back to OP's question. It looks like continuation token is only provided when the internal page size limit is reached or when your page size is greater than the default of 50.Dichroism
H
10

TL;DR version: The safest assumption is that the presence or absence of a continuation token in a search response is completely out of your control.

The SearchContinuationToken is not meant for paging of results. That's what SearchParameters.Top and SearchParameters.Skip are for. Instead, you can think of the continuation token as a mechanism to resume a search that couldn't be completed in a single request for potentially arbitrary reasons. The docs on MSDN mention page size as the reason, but this should not be understood to be part of the contract of ContinueSearch. In principle there could be other reasons why Azure Search hands back a continuation token. No matter the reason, if you want to write a robust client you should be prepared to handle continuation tokens, regardless of Top, your result count, or any other factor.

Hammons answered 26/11, 2015 at 1:10 Comment(2)
Bruce, thanks so much for this. I was wondering the same thing and thought that continuation token could be used for paging. Thanks for clarifying!Alkmaar
I think this indierectly answers my question, that is: What is the best way to determine whether or not you need to continue your search? Answer: Whenever there is a continuation token. I understand now that paging is a different topic and for that you should always use .Top and .Skip and ContinuationToken. If not paging, you should always use ContinuationToken. Thank you for the clarification.Mcpeak

© 2022 - 2024 — McMap. All rights reserved.