-Sorting

To sort search results based on different criteria, we first have to configure valid values in Workbench → Data Configuration → Sorting/Pagination as described in https://hawksearch.atlassian.net/wiki/spaces/HSKB/pages/1330315288/Pagination+Sorting#Sorting. After that, we can retrieve the sort options by looking at the Sorting property of the SearchResponse object.

Example (selecting the configured sort options):

public class SelectListItem { public string Text { get; set; } public string Value { get; set; } public bool Selected { get; set; } } //SearchClient _client var searchResponse = _client.Search<HawksearchProductItem>(searchRequest); var sortOptions = searchResponse.Sorting.Items.Select(x => new SelectListItem { Text = x.Label, Value = x.Value, Selected = x.Selected }),

 

We have 2 ways to sort our results (just like we have 2 ways to make a search request):

 

  1. Using only Optimizely.Hawksearch.Client

  • Set the SortBy property directly in the SearchRequest object.

 

  1. Using both

Optimizely.Hawksearch.Client and Optimizely.Hawksearch.Connector

  • Create and populate a SearchQuery object

  • Set the SortOrder property of the SearchQuery object

  • Build a SearchRequest object by passing the created SearchQuery object to a SearchRequestBuilder and its Fluent API

 

Example:

var query = new SearchQuery { Keyword = "jacket", Page = 1, PageSize = 5, SortOrder = "Z-A" //Valid values are configured in Workbench > Data Configuration > Sorting/Pagination };