Modify Search Query
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:
- Reference the API to understand what parameters are available to meet the objective
- 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.
The object path notation corresponds to that used in the outgoing JSON API query
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.
Now the query override can be applied
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:
<script type="text/javascript">
// Defer initialization (powerUp) of the Klevu component
klevu({
powerUp: {
landing: false
}
});
// Define a new custom event, which will fire after SRLP has initialised
klevu.coreEvent.build({
name: "myLandingPageOverride",
fire: function () {
if (klevu.getGlobalSetting("flags.setRemoteConfigLanding.build", false)) {
return true;
}
return false;
},
maxCount: 150,
delay: 100
});
// Listen for the new custom event
klevu.coreEvent.attach("myLandingPageOverride", {
name: "attachToMyLandingPageOverride",
fire: function () {
// Fire the custom javascript action or override an API parameter
// 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);
}
});
// Activate the initialization (powerUp)
klevu({
powerUp: {
landing: true
}
});
}
});
</script>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
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
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.
<script type="text/javascript">
// Defer initialization (powerUp) of the Klevu components
klevu({
powerUp: {
quick: false,
landing: false,
catnav: false
}
});
// QUICKSEARCH OVERRIDE
// Define a new custom event, which will fire after Quicksearch has initialised
klevu.coreEvent.build({
name: "myQuickSearchOverride",
fire: function () {
if (klevu.getGlobalSetting("flags.setRemoteConfigQuick.build", false)) {
return true;
}
return false;
},
maxCount: 150,
delay: 100
});
// Listen for the new custom event
klevu.coreEvent.attach("myQuickSearchOverride", {
name: "attachToMyQuickSearchOverridee",
fire: function () {
// Fire the custom javascript action or override an API parameter
klevu.search.quick.getScope().chains.request.control.addAfter("initRequest", {
name: "modifyQuickQuery",
fire: function (data, scope) {
// REFERENCE COMMON FUNCTION HERE
klevu.search.modules.addMyOverrideToQuery(data, scope);
}
});
// Activate the initialization (powerUp)
klevu({
powerUp: {
quick: true
}
});
}
});
// LANDING PAGE OVERRIDE
// Define a new custom event, which will fire after SRLP has initialised
klevu.coreEvent.build({
name: "myLandingPageOverride",
fire: function () {
if (klevu.getGlobalSetting("flags.setRemoteConfigLanding.build", false)) {
return true;
}
return false;
},
maxCount: 150,
delay: 100
});
// Listen for the new custom event
klevu.coreEvent.attach("myLandingPageOverride", {
name: "attachToMyLandingPageOverride",
fire: function () {
// Fire the custom javascript action or override an API parameter
klevu.search.landing.getScope().chains.request.control.addAfter("initRequest", {
name: "modifyLandingQuery",
fire: function (data, scope) {
// REFERENCE COMMON FUNCTION HERE
klevu.search.modules.addMyOverrideToQuery(data, scope);
}
});
// Activate the initialization (powerUp)
klevu({
powerUp: {
landing: true
}
});
}
});
// CATEGORY PAGE OVERRIDE
klevu.coreEvent.build({
name: "myCategoryPageOverride",
fire: function () {
if (klevu.getGlobalSetting("flags.setRemoteConfigCatnav.build", false)) {
return true;
}
return false;
},
maxCount: 150,
delay: 100
});
klevu.coreEvent.attach("myCategoryPageOverride", {
name: "attachToMyCategoryPageOverride",
fire: function () {
// Override Query
klevu.search.catnav.getScope().chains.request.control.addAfter("initRequest", {
name: "modifyCatnavQuery",
fire: function (data, scope) {
klevu.search.modules.addMyOverrideToQuery(data, scope);
}
});
// power up
klevu({
powerUp: {
catnav: true
}
});
}
});
// COMMON FUNCTION : Overridden by all Quick, Landing, and Catnav queries
(function (klevu) {
klevu.extend(true, klevu.search.modules, {
addMyOverrideToQuery: 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);
</script>
. . . . . . . . . . . .
.