
You can manipulate item filters when making the request.

## Filter types


<div class="admonition admonition-note"><div class="admonition-icon"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5"><path stroke-linecap="round" stroke-linejoin="round" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" /></svg></div><div class="admonition-body"><div class="admonition-content">

For a detailed description of filter types, see [the User Guide](/docs/ai-hub/recommendations-v2/recommendation-filters#filter-types).

</div></div></div>


### IQL filters 

An IQL filter is a string built using the [Items Query Language](/developers/iql). It allows you to combine filters and apply logic such as IF statements to build criteria that an item must meet to be included in the recommendation response.

Elastic IQL filters let you supplement the recommendation result with items which don't meet the filter criteria in case the criteria are too restrictive.


<div class="admonition admonition-important"><div class="admonition-icon"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5"><path stroke-linecap="round" stroke-linejoin="round" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" /></svg></div><div class="admonition-body"><div class="admonition-content">

- The "equals" operator is `==`. A single `=` is not an operator in IQL.
- In the IQL string, the spaces before and after the `AND/OR` operators are required.
- When adding filters to POST requests, you must escape any `"` in the filter string in the request body, for example:
```
"additionalFilters": "brand!=\"foo\" AND brand!=\"bar baz\"",
```

</div></div></div>



<div class="admonition admonition-tip"><div class="admonition-icon"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5"><path stroke-linecap="round" stroke-linejoin="round" d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z" /></svg></div><div class="admonition-body"><div class="admonition-content">

When you add filters to a campaign in the Synerise Web Application, you can [use the API to fetch the campaign's settings, including filters](https://developers.synerise.com/AIRecommendations/AIRecommendations.html#operation/GetRecommendationCampaignV2).  
You can use this to learn IQL by seeing how filters created with the GUI are saved into IQL strings.

To see the filters, check the `slots[].filterRules` object in the response.

</div></div></div>


### Distinct filters

Distinct filters regulate how many items with certain properties can be recommended at the same time.

- In campaign result requests, the filters are set by the campaign and can't be changed when making the request.
- In model requests, you can add a distinct filter when making the request.

## Manipulating filters in campaign results

In campaign requests, you can use the `additionalFilters` and `additionalElasticFilters` parameters to modify or replace the filters from the campaign settings.

When you do that, you must the use `filtersJoiner` and `elasticFiltersJoiner` parameters to set the logic of combining the filters:
- `AND` means that an item must match both the filter you send and the one from the campaign.
- `OR` means that an item must match the filter you send, the filter from the campaign, or both.
- `REPLACE` means that an item must match the filter you send and the filter from the campaign is ignored.

Distinct filters are defined in the campaign settings and can't be changed when making a request.

**Examples**:

<div class="content-tabs" data-tab-group="tabgrp-1">
<div class="tab-buttons"><button class="tab-button" data-tab-id="tabgrp-1-0" data-tab-group="tabgrp-1" data-tab-active="true">POST &#39;AND&#39;</button><button class="tab-button" data-tab-id="tabgrp-1-1" data-tab-group="tabgrp-1">POST &#39;OR&#39;</button><button class="tab-button" data-tab-id="tabgrp-1-2" data-tab-group="tabgrp-1">GET &#39;REPLACE&#39;</button><button class="tab-button" data-tab-id="tabgrp-1-3" data-tab-group="tabgrp-1">POST &#39;AND&#39;</button></div>

<div class="tab-panel" data-tab-id="tabgrp-1-0" data-tab-group="tabgrp-1" data-tab-active="true">

In this example, the item must meet at least both of the following:
- the conditions of elastic filter from the campaign
- elastic filter: price must be more than 50

Usage example: a cart recommendation which shows items with a price that will increase the cart's value to the threshold of free shipping.


<pre><code class="language-plaintext">curl --location 'https://api.synerise.com/recommendations/v2/recommend/campaigns?token=...' \
--header 'Content-Type: application/json' \
--data '{
    "clientUUID": "cf9e9b57-7776-51bc-b7bc-75cc75abdf59",
    "campaignId": "DkhvrZoTKthD",
    "additionalElasticFilters": "price.value&gt;50",
    "elasticFiltersJoiner": "AND"
}'</code></pre>

</div>

<div class="tab-panel" data-tab-id="tabgrp-1-1" data-tab-group="tabgrp-1">

In this example, the item must meet at least one of the following:
- the conditions of elastic filter from the campaign
- elastic filter: price must be between 50 and 100


  <pre><code class="language-plaintext">curl --location 'https://api.synerise.com/recommendations/v2/recommend/campaigns?token=...' \
  --header 'Content-Type: application/json' \
  --data '{
      "clientUUID": "cf9e9b57-7776-51bc-b7bc-75cc75abdf59",
      "campaignId": "DkhvrZoTKthD",
      "additionalElasticFilters": "price.value&lt;100 AND price.value&gt;50",
      "elasticFiltersJoiner": "OR"
  }'</code></pre>

</div>

<div class="tab-panel" data-tab-id="tabgrp-1-2" data-tab-group="tabgrp-1">

In this example, the filter from the campaign is replaced. The new filter is that the item's brand can't be "foo" or "bar baz". 

<pre><code class="language-plaintext">curl --location 'https://api.synerise.com/recommendations/v2/recommend/campaigns/DkhvrZoTKthD?&amp;additionalFilters=brand!%3D%22foob%22%20AND%20brand!%3D%22bar%20baz%22&amp;filtersJoiner=REPLACE&amp;token=...&amp;clientUUID=...'</code></pre>

</div>

<div class="tab-panel" data-tab-id="tabgrp-1-3" data-tab-group="tabgrp-1">

In this example, the item must meet both of the following:
- the conditions of filter from the campaign
- color is `red`

  <pre><code class="language-plaintext">curl --location 'https://api.synerise.com/recommendations/v2/recommend/campaigns?token=...' \
  --header 'Content-Type: application/json' \
  --data '{
      "clientUUID": "cf9e9b57-7776-51bc-b7bc-75cc75abdf59",
      "campaignId": "DkhvrZoTKthD",
      "additionalFilters": "color==\"red\"",
      "filtersJoiner": "AND"
  }'</code></pre>

</div>
</div>


## Requests to a model
In requests to a model, you can add the following parameters:
- `filters` is an [IQL filter](/developers/iql)
- `elastic:filters` is an elastic [IQL filter](/developers/iql)
- `distinctFilter` is an object:  
    
  <pre><code class="language-json">"distinctFilter": {
  "elastic": boolean, // when `true`, if there are not enough items which meet the criteria,
                      // other items can be added to meet the minimum number of items in the slot
  "filters": // an array of filters
      [
          {
              "field": "color", // attribute name
              "maxNumItems": n, // max number of items with the same attribute value
              "levelRangeModifier": n // only used when field is 'category'.
                                      // Removes the last n category levels
          }
      ]
  }</code></pre>



<div class="admonition admonition-important"><div class="admonition-icon"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5"><path stroke-linecap="round" stroke-linejoin="round" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" /></svg></div><div class="admonition-body"><div class="admonition-content">

The available filter types depend on the recommendation type. For details, refer to each endpoint's [reference documentation](https://developers.synerise.com/AIRecommendations/AIRecommendations.html#tag/Recommendations).

</div></div></div>


**Example**:  
A request to the "Personalized" model with a filter that uses an attribute from the profile which made the request:

<div class="content-tabs" data-tab-group="tabgrp-2">
<div class="tab-buttons"><button class="tab-button" data-tab-id="tabgrp-2-0" data-tab-group="tabgrp-2" data-tab-active="true">GET</button><button class="tab-button" data-tab-id="tabgrp-2-1" data-tab-group="tabgrp-2">POST</button></div>

<div class="tab-panel" data-tab-id="tabgrp-2-0" data-tab-group="tabgrp-2" data-tab-active="true">

<pre><code class="language-plaintext">curl --location 'https://api.synerise.com/recommendations/v2/recommend/items/users/8ae22439-f72b-4fe7-98e5-da3217215c54?filters=colors%3D%3Dclient.attributes.favoriteColor&amp;token=98A5FC55-0000-0000-0000-98339BDECAE6'</code></pre>

</div>

<div class="tab-panel" data-tab-id="tabgrp-2-1" data-tab-group="tabgrp-2">

<pre><code class="language-plaintext">curl --location 'https://api.synerise.com/recommendations/v2/recommend/items/users/8ae22439-f72b-4fe7-98e5-da3217215c54?token=98A5FC55-0000-0000-0000-98339BDECAE6' \
--header 'Content-Type: application/json' \
--data '{
    "clientUuid": "8ae22439-f72b-4fe7-98e5-da3217215c54",
    "slots": [
        {
            "filters": "colors==client.attributes.favoriteColor"
        }
    ]
}'</code></pre>

</div>
</div>

