---
title: Modify Search Query
slug: template-js/modify-search-query
docTags: 
createdAt: 2022-06-03T20:02:53.000Z
---

:::hint{type="warning"}
<font color="#eb144c">**IMPORTANT**</font>**&#x20;: 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)&#x20;
:::







To modify a search, the outgoing query to the indexed catalog is intercepted and any of the available [Klevu API ](https://developers.klevu.com/api) parameters are available to be inserted, or adjusted as needed.

There are two steps:

1. *Reference the API&#x20;*&#x74;o understand what parameters are available to meet the objective
2. *Attach the override* to the appropriate outgoing query (or queries)



:::hint{type="danger"}
Please see [Custom Overrides](docId:_bc3GcThleEaToYujDtuE)  for the prerequisite details on preparing for Klevu JS overriding.
:::

## API Referencing

Any of the [Klevu API paramters](https://docs.klevu.com/apis/smart-search) 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.

:::BlockQuote
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

:::BlockQuote
"recordQueries": \[
&#x20;   \{
&#x20;     "**filters**": \{
&#x20;       "**filtersToReturn**": \{
&#x20;         "**options**": \{
&#x20;           "**limit**": **20**
&#x20;         }
&#x20;       }
&#x20;     },
&#x20;     "id": "productList",
&#x20;     "typeOfRequest": "SEARCH",
&#x20;     "settings": \{
&#x20;       "query": \{
&#x20;         "term": "jackets"
&#x20;       }
&#x20;     }
&#x20;   }
&#x20; ],
:::

See : Klevu [API documentation on Retrieving Filters](https://docs.klevu.com/apis/faceting) 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.

:::BlockQuote
klevu.coreEvent.attach("myLandingPageOverride", \{
&#x20;   name: "attachToMyLandingPageOverride",
&#x20;   fire: function () \{
&#xA;**&#x20;       // Attach to the Klevu query request chain&#x20;
&#x20;       klevu.search.landing.getScope().chains.request.control.addAfter(
&#x20;       "initRequest", \{
&#x20;           name: "modifyLandingQuery",
&#x20;           fire: function (data, scope) \{

&#x20;               // OVERRIDE API PARAMETER HERE

&#x20;              &#x20;
&#x20;           }
&#x20;       });**

&#x20;       klevu(\{
&#x20;           powerUp: \{
&#x20;               landing: true
&#x20;           }
&#x20;       });
&#x20;   }
});
:::



Now the query override can be applied

:::BlockQuote
klevu.coreEvent.attach("myLandingPageOverride", \{
&#x20;   name: "attachToMyLandingPageOverride",
&#x20;   fire: function () \{

&#x20;       // Attach to the Klevu query request chain&#x20;
&#x20;       klevu.search.landing.getScope().chains.request.control.addAfter(
&#x20;       "initRequest", \{
&#x20;           name: "modifyLandingQuery",
&#x20;           fire: function (data, scope) \{

&#x20;               // OVERRIDE API PARAMETER HERE

**// SET max filter options to retrieve
klevu.setObjectPath( data , "localOverrides.query.productList.filters.filtersToReturn.options.limit", 20);**
&#x20;              &#x20;
&#x20;           }
&#x20;       });

&#x20;       klevu(\{
&#x20;           powerUp: \{
&#x20;               landing: true
&#x20;           }
&#x20;       });
&#x20;   }
});
:::

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:

:::CodeblockTabs
JavaScript

```typescript
<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.&#x20;

| 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.&#x20;

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

:::BlockQuote
klevu.coreEvent.attach("myLandingPageOverride", \{
&#x20;   name: "attachToMyLandingPageOverride",
&#x20;   fire: function () \{

&#x20;       // Attach to the Klevu query request chain&#x20;
&#x20;       klevu.search.landing.getScope().chains.request.control.addAfter(
&#x20;       "initRequest", \{
&#x20;           name: "modifyLandingQuery",
&#x20;           fire: function (data, scope) \{
&#xA;**&#x20;               // REFERENCE COMMON FUNCTION HERE
&#x20;               klevu.search.modules.myCommonQueryOverride(data, scope);
&#x20;              &#x20;**&#xA;&#x20;           }
&#x20;       });

&#x20;       klevu(\{
&#x20;           powerUp: \{
&#x20;               landing: true
&#x20;           }
&#x20;       });
&#x20;   }
});
:::

Now, we will define the new common function *myCommonQueryOverride&#x20;*&#x74;hat is called from the custom event listener in the previous step.

This function will loop through all outgoing queries and apply the override.&#x20;

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

:::BlockQuote
(function (klevu) \{
&#x20;   klevu.extend(true, klevu.search.modules, \{
&#x20;       **myCommonQueryOverride**: function (data, scope) \{
&#x20;       klevu.each(data.request.current.recordQueries,function(key, query)\{

&#x20;       // OVERRIDE API PARAMETER HERE

**// SET active currency
klevu.setObjectPath(
&#x20;   data,
&#x20;   "localOverrides.query." + query.id + ".settings.priceFieldSuffix",
&#x20;   "GBP"
);**

&#x20;           });
&#x20;       }
&#x20;   });
})(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.

:::CodeblockTabs
JavaScript

```typescript
<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>
```
:::









. . . . . . . . . . . .&#x20;













.
