Library Reference
...
JS Library
Custom Overrides

Modify Search Query

10min
important important the methods within this article are being replaced! please review the following updated guides for efficient customization set template docid 6yvx9naw6ax68wygucob7 modify request overview docid ndrepmgauhyltilxkg o modify response overview docid\ k usia fv m380bqcaw7p modify recommendations overview docid\ o03qerb3f sy0bhba64af 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 docid bc3gcthleeatoyujdtue 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 docid bc3gcthleeatoyujdtue 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 \<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 docid bc3gcthleeatoyujdtue 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 \<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>