Search Filters/Facets

Search filter is used to retrieve and filter by facets from the Hawksearch Search API.
Possibilities to filter:

  1. Populate the FacetSelections property of a SearchRequest object directly following the syntax from the official documentation https://hawksearch.atlassian.net/wiki/spaces/HSKB/pages/55246867/Hawksearch+v4.0+-+Search+API

  2. Populate the Filters property of a SearchQuery object, then pass it to the

public SearchRequest Build(SearchQuery searchQuery)

method of a SearchRequestBuilder.


The Filters property is a list of SearchFilter objects.
There are 2 out of the box search filters provided by the connector: TermsSearchFilter and RangeSearchFilter, both inheriting from the SearchFilter class.

  • TermsSearchFilter → can be used for exact values in a facet (ex: color → Red, size → XS)

  • RangeSearchFilter → can be used for a range of values (ex: price → 250$ to 500$).

Example

var query = new SearchQuery(); var filters = new List<SearchFilter>(); filters.Add(new TermsSearchFilter("Color", "Red")); filters.Add(new TermsSearchFilter("Brand", new List<string> {"Nike", "Adidas"})); filters.Add(new RangeSearchFilter("Price", 250, 500)); query.Filters = filters;


How to extend

You can create custom filters if the need arise. To do so, create a class that inherits from SearchFilter.

Then, you will need to create a search filter convertor for your new filter. Create a class implementing the ISearchFilterConvertor interface:

string[] Convert(SearchFilter filter); bool CanConvert(SearchFilter filter);


Convert method will determine how the new search filter will be converted to the search request.
CanConvert method determines what are the conditions that need to be met in order for our convertor to take action.
For exemplification, this is how the RangeSearchFilterConvertor class is implemented:

Lastly, to register the new convertor of type ISearchFilterConvertor, you need to call the

method of the current SearchRequestBuilder and pass in your custom convertor.
Behind the scenes, the Build(…) method of the SearchRequestBuilder will iterate through all possible convertors in order to find one suitable for your custom search filter, then pick it up and convert the search filter to the necessary data that needs to be sent inside the search request to Hawksearch.

Â