# Filtering

{% hint style="info" %}
Filtering is similar to facets selection, since the results will be filtered based upon selected facets AND filters. However, with filters, you don't have access to results grouping, list and count of the values, ...

Filters can be applied to any attribute in the catalog
{% endhint %}

## Parameters

Filtering the results is controlled by the `filter` parameter. This parameter expects a **SQL-like** formatted string that describes the filtering rules.

*Check below for more info.*

| **Name**     | Type     | Is Required ? | Default value |
| ------------ | -------- | ------------- | ------------- |
| **`filter`** | `string` | ✖             | empty string  |

### `filter`

Filter the results using an SQL-like syntax. The generic syntax is as follows:

```
filterRule: `${attribute} ${rule_operator} ${value}`

filterGroup: `(${filterRule1} ${group_operator} ${filterRule2} ...)`

rule_operator: =, !=, <, <=, >, >= 
group_operator: AND, OR
```

**`filter`** can be either a single `filterRule` or a `filterGroup`.

* For example, all the following filters are valid:

```sql
size = 137
size != 137
price > 50

(size = 137)
(size = 137 OR size = 133 OR size = 48)
price >= 10 AND price < 100

(size = 137 OR size = 119) AND (price > 10 AND price < 100)

size = 137 OR price > 10
size = 137 AND price > 10
```

## Response

All responses from XO Search uses the same format.\
Check [API Reference](https://crownpeak.gitbook.io/product-discovery/resources/archived-pages/introduction/api) page for a more detailed description of this format.

{% content-ref url="../api" %}
[api](https://crownpeak.gitbook.io/product-discovery/resources/archived-pages/introduction/api)
{% endcontent-ref %}

## Usage examples

* Filter results with a price higher than 10 €

{% tabs %}
{% tab title="SDK - JS" %}
**NodeJS / NPM example**

```javascript
import { search } from '@attraqt/search';

const query = 'T-shirt';

search.init({ token: SEARCH_API_TOKEN });

const response = await search.query(query, {
  filter: 'price > 10'
});

console.log(response);
```

**HTML example**

```markup
<script type="text/javascript">
    xo.init({
        search: {
            token: SEARCH_API_TOKEN
        }
    });

    xo.search.query('T-Shirt', {
        filter: 'price > 10'
    }).then((response) => {
        console.log(response);
    });
</script>
```

{% endtab %}

{% tab title="API - POST" %}
**HTTP example**

```http
POST https://api-eu.attraqt.io/search HTTP/1.1
Content-Type: application/json; charset=UTF-8

{
  "token": "SEARCH_API_TOKEN",
  "query": "T-Shirt",
  "options": {
    "filter": "price > 10"
  }
}
```

**`curl` example**

```bash
curl -d "{\"token\":\"${SEARCH_API_TOKEN}\", \"query\":\"T-Shirt\", \"options\":{\"filter\": \"price > 10\"}}" \
     -H "Content-Type: application/json; charset=UTF-8"                                                        \
     -X POST "https://api-eu.attraqt.io/search"
```

**JavaScript example**

```javascript
const response = await fetch('https://api-eu.attraqt.io/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json; charset=UTF-8'
  },
  body: JSON.stringify({
    token: SEARCH_API_TOKEN,
    query: 'T-Shirt',
    options: {
      filter: 'price > 10'
    }
  })
});

if (response.ok) {
  console.log(await response.json());
}
```

{% endtab %}

{% tab title="API - GET" %}
**HTTP example**

```http
GET https://api-eu.attraqt.io/search/:token?encoded=:encodedParams HTTP/1.1
```

**`curl` example**

```bash
curl "https://api-eu.attraqt.io/search/${SEARCH_API_TOKEN}?encoded=%7B%22query%22%3A%22T-Shirt%22%2C%20%22options%22%3A%7B%22filter%22%3A%22price%20%3E%2010%22%7D%7D"
```

**JavaScript example**

```javascript
const token = SEARCH_API_TOKEN;
const params = encodeURIComponent(JSON.stringify({
  query: 'T-shirt',
  options: {
    filter: 'price > 10'
  }  
}));

const response = await fetch(
  `https://api-eu.attraqt.io/search/${token}?encoded=${params}`
);

if (response.ok) {
  console.log(await response.json());
}
```

{% endtab %}
{% endtabs %}
