Faceting
If you haven't gone through the prerequisites and the v2 structure, please have a look at Overview
It is important to note that retrieving filters is an expensive operation that can slow down your search experience. For this reason, you may want to fetch filters once per unique query and cache them locally.
For example, when using pagination to retrieve records from subsequent pages, there is actually no need to fetch the filters again since these will not change from page to page. The filters from Klevu are always for the entire set of records for that query.
While searching for records, you can also request aggregations of the attribute values within the search results, so you can allow your customers to filter the results further.
For example, if the search query was "tops", you may also be interested to know all of the colors, sizes, and materials that these tops are available in to display them as selectable facets or filters.
There are two separate methods of working with filters with the Klevu search API:
- Retrieving filters applicable to a search result
- Applying one or more of those filters to a search query
To apply a filter, you need to know the key of the relevant filter and the value of the relevant option, which should be submitted using the same format and case sensitivity as is returned by Klevu.
The filtersToReturn object can be used for obtaining filters from the Klevu Search engine. You can request all possible filters or only those matching certain criteria.
Understanding response format
Params | Description | |
---|---|---|
key | This is the unique identifier of the attribute for which options are being provided. | |
label | This is the label or caption which you have associated with the attribute in the Klevu Merchant Centre. | |
type | The type of the filter will be either OPTIONS, SLIDER or RATING. For an OPTIONS filter, its different options, eg. red, blue, black, are provided. For a SLIDER filter, values such as minimum, maximum, start and end are provided in the response. For a RATING filter, its rating ranges from 1 to 5, where Range is defined as below: The rating range of 4.51 - 5.5 is considered the 5 rating The rating range of 3.51 - 4.5 is considered the 4 rating The rating range of 2.51 - 3.5 is considered the 3 rating The rating range of 1.51 - 2.5 is considered the 2 rating The rating less than 1.50 is considered 1 Rating | |
options | For type OPTIONS this will contain an array of different options available. name: The label or caption of the option, eg. Red, Blue, Black, etc. value: This is the unique identifier which should be used when applying a filter to search results. count: The number of records found with the current option in the current search results. selected: Whether ot not the current option is already applied as a filtering condition on the search results. | |
min, max, start, end | These values are provided only for the SLIDER or range filters. min, max: These values are the minimum and maximum values found in the current result set. This can be used to set the upper and lower limits of your slider. start, end: The start and end values are the filtering conditions already applied on the result set for this filter, or NULL if no filter has been applied. | |
The applyFilters object can be used for applying filter conditions so your customers can fine-tune their results based on relevant attributes.
Understanding response format
If only one filter is applied with only value, Klevu search ensures that all the records returned contain the relevant attribute with the specified value.
In the case where two or more values for the same filter are provided, the execution is dependent on the value of the singleSelect parameter.
- If it is set to true, the products returned must have both the values.
- If it is set to false, the products returned must have at least one of the values.
If constraints are added for multiple filters and the value of the singleSelect filter is false, the products returned must have at least one value from each of the filter conditions provided.