Guide
Smart Search

Advanced Filtering

4min

In the "faceting" section, we highlight how one can obtain available filters for a search query and apply filters to narrow down search results. For example, after searching for "shoes", a shopper may want to narrow down results by applying a filter on the color attribute and choosing a value such as red. Doing so will show only "red" shoes in the results. If at this point, the shopper chooses yet another color, e.g. blue, Klevu will now show both the "red" and the "blue" shoes. Thus, it will apply the OR operation between the values of the same attribute. If at this stage, the shopper chooses "cotton" as a material, the system will show both the red and blue shoes, but it will also ensure all products have "cotton" as a material as well. In other words, it applies the AND operation among the different types of selected attributes.

However, there comes a time when one may want to perform complex filtering operations, or in other words, advanced filtering operations. For example, you may want to show a group of shoes where the "color is red" and the "brand is NOT Adidas". You may also want to add another group of shoes to this result set. For example, where the "color is blue or black", "brand is Nike" and the "price range is between 100 and 400". As you can see, unlike the behaviour of "faceting", in our example here, we like to have one or more groups of products matching different groups of conditions for the final set of products to be returned.

It is with this intention to allow advanced filtering operations on the result set, we have introduced a new functionality called "advanced filtering" in our API.

Before the introduction of advanced filtering, if a user had requested filters to be returned or some to be applied, Klevu used to find out a set of relevant products matching the user query. It then included/excluded any products requested explicitly to this result set. It is after this stage that it calculated all possible filters to be returned and also applied the selected filters to narrow down the search results. Thus, if an explicitly added product did not match the applied filter, it would be excluded in the final result set.

With the addition of advanced filtering, Klevu now identifies a set of relevant products matching the query. It then applies advanced filtering to this. It is after this step that it includes/excludes any products requested explicitly, calculates filters based on this result set and finally applies the selected filters to narrow down the search results. This way, if there is any filtering to be applied prior to including/excluding products explicitly, advanced filtering should be used.

Please note the advanced filtering can also be used with Category Merchandising and Recommendation APIs.

The groupCondition object can be used for applying the advanced filtering conditions so your customers can fine-tune their results based on relevant attributes.

Example is as below:
POST
Request
Body Parameters
groupOperator
optional
String
Defining the condition of filtering. Here are the options available. ALL_OF:This is used to filter data using AND query i.e. the search results are matching with all the attributes. ANY_OF:This is used to filter data using OR query i.e. the search results are matching with at least one attributes. NONE_OF: This is used as negation AND query i.e. when search results are matching with none of the attribute values.
key
optional
String
The ID of the attribute to filter by, eg. color
valueOperator
optional
String
Defining an attribute's operation, Here are the options available. INCLUDE : This is used to include specific attribute for the filter purpose. EXCLUDE : This is used to exclude specific attribute for the filter purpose. EXISTS: This is used to find if the specific indexed attribute value exists with product. NOT_EXISTS : This is used to find if the specific indexed attribute value does not exist at all.
singleSelect
optional
Boolean
The behaviour when specifying multiple filters or values. 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.
excludeValuesInResult
optional
Boolean
For numeric filter like price, can be used to exclude data of the starting or ending values. For example if the range is defined as "1200 - 1232" in values, and excludeValuesInResult is set to true, Klevu will use range as “1201 - 1231“. This can be used to achieve greater than or less than condition. For example,if you want to get the products greater than price 200, the values field will be “200 - *“ and the excludeValuesInResult will be set to true.
values
optional
Array
An array of values to filter by, eg. Red, Blue. When using range filters, specify the first value as the minimum value and the second as the max value. For example to retrieve records with prices between 60 to 80, use: "key": "klevu_price", "values": [60, 80]. To retrieve steps of prices, for example those with values between 0-50 or 150-200, use: "key": "klevu_price", "values": ["0-50", "150-200"]. To retrieve exact values, for example records with exact values 100 or 200, use: "key": "klevu_price", "values": ["100-100", "200-200"]. By default all attributes submitted to Klevu are indexed as STRING attributes, which means they cannot be used as range filters. The product sale price field is the only exception to this rule, which is filtered using the key klevu_price. If you have explicitly requested and Klevu has approved that certain attributes be indexed as numerical attributes, you can also retrieve those as range filters.
Request Body (JSON)
PHP
JS
Java
Node.js
Responses
200





Updated 02 Jul 2024
Doc contributor
Doc contributor
Did this page help you?