Library Reference
...
JS Library
Custom Overrides

Modify Search Query

10min

IMPORTANT : The methods within this article are being replaced! Please review the following updated guides for efficient customization:







To modify a search, the outgoing query to the indexed catalog is intercepted and any of the available Klevu API parameters are available to be inserted, or adjusted as needed.

There are two steps:

  1. Reference the API to understand what parameters are available to meet the objective
  2. Attach the override to the appropriate outgoing query (or queries)



Please see Custom Overrides for the prerequisite details on preparing for Klevu JS overriding.

API Referencing

Any of the Klevu API paramters are available to be applied as needed or otherwise to override any default values.

For example, the following example snippet modifies the outgoing productList API Query by assigning the value of "20" as the number of filter options to return.

klevu.setObjectPath( data , "localOverrides.query.productList.filters.filtersToReturn.options.limit", 20)

The object path notation corresponds to that used in the outgoing JSON API query

"recordQueries": [ { "filters": { "filtersToReturn": { "options": { "limit": 20 } } }, "id": "productList", "typeOfRequest": "SEARCH", "settings": { "query": { "term": "jackets" } } } ],

See : Klevu API documentation on Retrieving Filters for more details and options



Attaching the override

Please see Custom Overrides for details on preparing for Klevu query overriding.

The query parameters are accessible by attaching to the query request.control within a previously established custom event listener.

klevu.coreEvent.attach("myLandingPageOverride", { name: "attachToMyLandingPageOverride", fire: function () { // Attach to the Klevu query request chain klevu.search.landing.getScope().chains.request.control.addAfter( "initRequest", { name: "modifyLandingQuery", fire: function (data, scope) { // OVERRIDE API PARAMETER HERE } }); klevu({ powerUp: { landing: true } }); } });



Now the query override can be applied

klevu.coreEvent.attach("myLandingPageOverride", { name: "attachToMyLandingPageOverride", fire: function () { // Attach to the Klevu query request chain klevu.search.landing.getScope().chains.request.control.addAfter( "initRequest", { name: "modifyLandingQuery", fire: function (data, scope) { // OVERRIDE API PARAMETER HERE // SET max filter options to retrieve klevu.setObjectPath( data , "localOverrides.query.productList.filters.filtersToReturn.options.limit", 20); } }); klevu({ powerUp: { landing: true } }); } });

Any of the Klevu API parameters are available to be applied as needed or otherwise override any default values.

Example

Landing page search query override to change the max filter options to retrieve for display:

JavaScript


Note that the action in the above example requires definition within the corresponding scope of the override.

Scope

Description

quick

QuickSearch module

landing

SRLP module

catnav

Category module

See an example below for establishing a common function that can be used to apply shared query modifications across multiple scope.



Using a Common Function

Often query modifications are intended to be applied to any search query. For this reason a common function can be created in which to apply the override. This is a useful strategy to apply override parameters to every query across quick, landing, and catnav scope.

Please see Custom Overrides for details on preparing for Klevu query overriding.

The query parameters are accessible by attaching to the query request.control within a previously established custom event listener.

In this example we are passing the arguments to a common function object reference as myCommonQueryOverride

klevu.coreEvent.attach("myLandingPageOverride", { name: "attachToMyLandingPageOverride", fire: function () { // Attach to the Klevu query request chain klevu.search.landing.getScope().chains.request.control.addAfter( "initRequest", { name: "modifyLandingQuery", fire: function (data, scope) { // REFERENCE COMMON FUNCTION HERE klevu.search.modules.myCommonQueryOverride(data, scope); } }); klevu({ powerUp: { landing: true } }); } });

Now, we will define the new common function myCommonQueryOverride that is called from the custom event listener in the previous step.

This function will loop through all outgoing queries and apply the override.

Note the use of a dynamic query.id used in the loop

(function (klevu) { klevu.extend(true, klevu.search.modules, { myCommonQueryOverride: function (data, scope) { klevu.each(data.request.current.recordQueries,function(key, query){ // OVERRIDE API PARAMETER HERE // SET active currency klevu.setObjectPath( data, "localOverrides.query." + query.id + ".settings.priceFieldSuffix", "GBP" ); }); } }); })(klevu);

Any of the Klevu API parameters are available to be applied as needed or otherwise override any default values.

Example

Landing page and Quick search query common function override to change the Currency used for pricing.

JavaScript










. . . . . . . . . . . .













.