Guide
Smart Search

Examples

19min

If you haven't gone through the prerequisites and the v2 structure, please have a look at Overview

Custom Record Type

In addition to Products, Categories and CMS Pages, Klevu APIv2 allows you to search for custom entities.

For example, if you want to display results for recipes, articles or physical stores within your search you can do so by utilising the typeOfRecords parameter.

This is an example how to search for custom record type.
POST
Request
Body Parameters
typeOfRecords
required
Array
Specify the identifier(s) of your custom entity types that you have indexed with Klevu, for example ACME_RECIPE or ACME-STORE.
Request Body (JSON)
PHP
JS
Java
Node.js
Responses
200


Try it here



Multiple Queries

Klevu APIv2 allows you to request multiple Product, Category, CMS Pages and Custom Entity queries all at the same time. All of these queries are usually fired simultaneously, meaning the total time taken is the duration of the slowest query, rather than the total of all queries.

In the example to the right, a single HTTP request is returning:

  • 3x search suggestions
  • 3x categories
  • 5x products with filters
  • 3x different products without filters
  • 5x cms pages or recipes (a custom entity)
  • 1x store (a custom entity)

Furthermore, all of the configuration and preferences found within this documentation can be applied to each and evey query separately, making an extremely powerful and fast way to display search results.

This is an example how to search for multiple queries.
POST
Request
Request Body (JSON)
PHP
JS
Java
Node.js
Responses
200


Try it here!



Fallback Queries

Sometimes customers will search for something which yields few or no results.

In this scenario, you can either present your customer with a 'no results found' message, or you can use the result of another query to show some records which they may be interested in.

We call this a fallback query.

Any query flagged as isFallbackQuery: true will not execute unless certain criteria are met. With this approach you can present customers with alternative results, without needing to make any additional HTTP requests, and also without the jeopardising performance of the initial query.

This is an example how to fetch results as a part of fall back queries.
POST
Request
Body Parameters
fallbackQueryId
required
String
The ID of another query which should be fired if the current query yields too few results.
fallbackWhenCountLessThan
optional
Integer
Use this parameter to specify the criteria for when a fallback query will be fired. For example, if you would like a fallback query to fire when you have two results or less, specify a value of '3'.
isFallbackQuery
optional
Boolean
Specify a value of true for any queries that should not be executed until some particular criteria are met, in another query. It is not possible for a fallback query to have its own fallback query.
Request Body (JSON)
PHP
JS
Java
Node.js
Responses
200


Try it here!



Hand-Picked Products

Sometimes merchants want to take control over which records are included or excluded, or whether certain records should always take precedence over others.

This can be done via query-time manual merchandising.

This is an example how to merchandise search results using APIs.
POST
Request
Body Parameters
topIds
required
Array
Specify any records which should always be displayed at the top of the result set. You can specify a record id to control this at variant level, or a itemGroupId to control this at compound item level. Note that this is only applicable when the sort order is by 'RELEVANCE'.
includeIds
optional
Array
Specify any records which should be included with the results, even if the Klevu search query did not match them. You can specify a record id to control this at variant level, or a itemGroupId to control this at compound item level.
excludeIds
optional
Array
Use this field to exclude certain records from the search results. You can specify a record id to control this at variant level, or an itemGroupId to control this at compound level.
Request Body (JSON)
PHP
JS
Java
Node.js
Responses
200


Try it here!



Custom Queries

DEPRECATED: The use of customANDQuery is being deprecated. For better control over custom queries please review and use Advanced Filtering

You can even write your own custom queries with Klevu search, to take more control over your results. The example to the right shows how you can build a query to filter results by Small or Medium records which are NOT brand "KKE."

This is an example how to merchandise search results using APIs.
POST
Request
Body Parameters
customANDQuery
required
String
DEPRECATED The custom query you would like to fire, which Klevu automatically converts into an appropriate query to be included with the request. Use a - character before the parenthesis to exclude records matching the contained condition. This is advanced usage of our API and you may need some help with building these queries, so when you need support please reach out to us via the Community Forum.
Request Body (JSON)
PHP
JS
Java
Node.js
Responses
200


Try it here!



Multiple Currencies

If your store has multiple currencies and you would like to show different values based on the currency your customer is currently viewing, this can be achieved using the priceFieldSuffix parameter.

When specifying a value for priceFieldSuffix, the following fields in your response data will be replaced with the value indexed with Klevu for the corresponding currency:

  • price
  • salePrice
  • currency
This is an example how to fetch prices for specific currency.
POST
Request
Body Parameters
priceFieldSuffix
required
String
If you have multiple currency support enabled for your store, this parameter can be used to retrieve prices for a specific currency. For example, if the data you have indexed with Klevu includes prices for a base currency GBP and an additional currency USD, a value of 'GBP' or 'USD' here will display the relevant currency values for your records.
Request Body (JSON)
PHP
JS
Java
Node.js
Responses
200


Try it here!



Customer Groups

A common B2B requirement is different product visibility and prices based on a customer group. With Klevu APIv2 you can specify parameters to filter out products which a particular customer should not see, and also show them specific prices if they differ from the base price.

When specifying a value for priceFieldSuffix, the following fields in your response data will be replaced with the value indexed with Klevu for the corresponding currency and group:

  • price
  • salePrice
  • currency

When specifying a value for visibilityGroupID, any records that do not belong to that group will be excluded from the results.

In the example to the right, the same product is indexed with data for currencies 'GBP' and 'USD'. It's visibility is set to groups 'my_group_1' and 'my_group_3', but not 'my_group_2'.

This is an example how to fetch prices specific to customer groups.
POST
Request
Body Parameters
priceFieldSuffix
required
String
If you have provided different prices for different groups when indexing your store data with Klevu, you can fetch those prices specific to a particular group. The format of this parameter is {CURRENCY}-{GROUPID}. For example if the currency you require is USD and your indexed group ID is 'my-group-1', your query should include "priceFieldSuffix": "USD-my-group-1".
visibilityGroupID
optional
String
If you have provided visibility data for each record when indexing your store data with Klevu, you can specify an ID of that group here to exclude all records which do not belong. The format of this parameter is {GROUPID}. For example if your indexed group ID is 'my-group-1', your query should include "visibilityGroupID": "my-group-1".
Request Body (JSON)
PHP
JS
Java
Node.js
Responses
200


Try it here