

# Filter expression examples


 Use the filter expressions in the following sections to learn how to build your own filter expressions. 

**Topics**
+ [

# Item recommendation filter expression examples
](item-recommendation-filter-examples.md)
+ [

# User segment filter expressions
](user-segment-filter-examples.md)
+ [

# Action recommendation filter expression examples
](action-recommendation-filter-examples.md)
+ [

# Combining multiple expressions
](multiple-expression-example.md)

# Item recommendation filter expression examples


The following filter expressions show how to filter item recommendations based on item interactions, item metadata, and user metadata. They are organized by data type.

**Topics**
+ [

## Item interaction data
](#item-interaction-filter-examples)
+ [

## Item data
](#item-filter-examples)
+ [

## User data
](#user-filter-examples)

## Item interaction data


The following expression excludes items based on an event type (such as click) or event types that you specify when you get recommendations using the `$EVENT_TYPE` parameter.

```
EXCLUDE ItemID WHERE Interactions.EVENT_TYPE IN ($EVENT_TYPE)
```

 The following expression excludes items that a user clicked or streamed.

```
EXCLUDE ItemID WHERE Interactions.EVENT_TYPE IN ("click", "stream")
```

The following expression includes only items that the user has clicked.

```
INCLUDE ItemID WHERE Interactions.EVENT_TYPE IN ("click")
```

## Item data


The following expression excludes items based on a category or categories that you specify when you get recommendations using the `$CATEGORY` parameter.

```
EXCLUDE ItemID WHERE Items.CATEGORY IN ($CATEGORY)
```

The following expression includes only items that are cheaper than the current item (the item you specify in the request for related items recommendations), and created by the same studio as the current item. You can apply a filter with the CurrentItem element only if your domain use case or custom recipe generates related items recommendations.

```
INCLUDE ItemID WHERE Items.PRICE < CurrentItem.PRICE AND Items.GENRE IN CurrentItem.GENRE
```

 The following expression excludes items based on multiple levels of categorical fields. It excludes items with a CATEGORY\$1L1 value of `shoe` that *do not* have a CATEGORY\$1L2 value of `boot`. 

```
EXCLUDE ItemID WHERE Items.CATEGORY_L1 IN ("shoe") AND Items.CATEGORY_L2 NOT IN ("boot")
```

The following expression includes only items with a price less than or equal to the price that you specify when you get recommendations using the `$PRICE` parameter.

```
INCLUDE ItemID WHERE Items.PRICE <= $PRICE
```

The following expression includes only items that have been created earlier than a timestamp (in Unix epoch time) that you specify when you get recommendations.

```
INCLUDE ItemID WHERE Items.CREATION_TIMESTAMP < $DATE
```

The following expression includes only items with a genre or genres that you specify when you get recommendations using the `$GENRE` parameter.

```
INCLUDE ItemID WHERE Items.GENRE IN ($GENRE)
```

The following expression includes only items that are more expensive than the current item *and* created more recently than a timestamp (in Unix epoch time) that you specify. You might use this filter if you are getting related item recommendations, and want to apply some specific business rules based on price and a varying creation date.

```
INCLUDE ItemID WHERE Items.PRICE < CurrentItem.PRICE AND Items.CREATION_TIMESTAMP > $DATE
```

## User data


The following expression excludes items with a genre or genres that you specify when you get recommendations using the `$GENRE` parameter, but only if the current user's age is equal to the value that you specify when you get recommendations using the `$AGE` parameter. 

```
EXCLUDE ItemID WHERE Items.GENRE IN ($GENRE) IF CurrentUser.AGE = $AGE
```

The following expression includes only items with `watch` for CATEGORY\$1L1 and `luxury` for CATEGORY\$1L2, if the current user's age is over `18`.

```
INCLUDE ItemID WHERE Items.CATEGORY_L1 IN ("watch") AND Items.CATEGORY_L2 IN ("luxury") IF CurrentUser.AGE > 18
```

# User segment filter expressions


The following filter expressions show how to filter user segments based on item interactions data and user metadata. They are organized by data type.

 **User data** 

The following filter expression includes only users with a membership status equal to the value that you specify when you get user segments.

```
INCLUDE UserID WHERE Users.MEMBERSHIP_STATUS IN ($MEMBERSHIP)
```

The following filter expression excludes users with an `AGE` less than a value you specify when you get user segments.

```
EXCLUDE UserID WHERE Users.AGE < $AGE
```

 **Item interaction data** 

The following filter expression includes only users who have clicked or rated items.

```
INCLUDE UserID WHERE Interactions.EVENT_TYPE IN ("click", "rating")
```

The following filter expression excludes users from user segments who have item interactions with an event type you specify when you get user segments.

```
EXCLUDE UserID WHERE Interactions.EVENT_TYPE IN ($EVENT_TYPE)
```

# Action recommendation filter expression examples


The following filter expression examples show how to filter actions based on action interactions data, action data. and user data. They are organized by data type.

**Topics**
+ [

## Action interaction data
](#action-interaction-filter-examples)
+ [

## Action data
](#action-filter-examples)
+ [

## User data
](#user-action-filter-examples)

## Action interaction data


The following filter expression includes only actions in recommendations that the user has interacted with, when those interactions have an event type that you specify when you get recommendations. 

```
INCLUDE ActionID WHERE Action_Interactions.EVENT_TYPE IN ($EVENT_TYPE)
```

The following filter expression excludes actions that the user has not taken based on event type.

```
EXCLUDE ActionID WHERE Action_Interactions.EVENT_TYPE IN ("NOT_TAKEN")
```

## Action data


The following expression excludes actions based on a category or categories that you specify when you get recommendations using the `$CATEGORY` parameter.

```
EXCLUDE ActionID WHERE Actions.CATEGORY IN ($CATEGORY)
```

The following expression includes only actions with a value greater than a value that you specify when you get recommendations.

```
INCLUDE ActionID WHERE Actions.VALUE > ($VALUE)
```

## User data


The following expression includes only actions for premium members if the current user has a premium membership. 

```
INCLUDE ActionID WHERE Action.MEMBERSHIP_LEVEL IN ("Premium") IF CurrentUser.MEMBERSHIP = $PREMIUM
```

The following expression excludes actions with a `VALUE` less than a value that you specify when you get recommendations if the current user is a premium member.

```
EXCLUDE ActionID WHERE Actions.VALUE < ($VALUE) IF CurrentUser.MEMBERSHIP = $PREMIUM
```

# Combining multiple expressions


To combine multiple expressions together you use a pipe separator (`|`). Use a combination of expressions when you want to use a single filter and filter on Items and Item interactions datasets, or Action and Action interactions datasets. Each expression is first evaluated independently and the result is either the union or the intersection of the two results. The following examples show how to create expressions for Items and Item interactions datasets, but the same rules apply when working with Actions and Action interactions. 

**Matching expressions example**

 If both expressions use `EXCLUDE` or both expressions use `INCLUDE`, the result is the union of the two results as follows (A and B are different expressions): 
+ `Exclude A | Exclude B` is equal to `Exclude result from A or result from B`
+ `Include A | Include B` is equal to `Include result from A or result from B`

The following example shows how to combine two expressions that use `INCLUDE`. The first expression includes only items with a category or categories that you specify when you get recommendations using the `$CATEGORY` parameter. The second expression includes items the user has marked as a `favorite`. Recommendations will include only items with the category you specify along with items that the user has marked as a favorite.

```
INCLUDE ItemID WHERE Items.CATEGORY IN ($CATEGORY) | INCLUDE ItemID WHERE Interactions.EVENT_TYPE IN ("favorite")
```

**INCLUDE and EXCLUDE example**

 If one or more expression uses `INCLUDE` and one more expression uses `EXCLUDE`, the result is the subtraction of the `EXCLUDE` expression result from the `INCLUDE` expression result as follows (A, B, C, and D are different expressions).
+ `Include A | Exclude B` is equal to `Include result from A - result from B`
+  `Include A | Include B | Exclude C | Exclude D` is equal to `Include (A or B) - (C or D)` 

Expression order does not matter: If the EXCLUDE expression comes before the INCLUDE expression, the result is the same.

The following example shows how to combine an `INCLUDE` expression and a `EXCLUDE` expression. The first expression includes only items with a genre or genres that you specify when you get recommendations using the `$GENRE` parameter. The second expression excludes items that the user has clicked or streamed. Recommendations will include only items with a genre that you specify that have not have been clicked or streamed.

```
INCLUDE ItemID WHERE Items.GENRE IN ($GENRE) | EXCLUDE ItemID WHERE Interactions.EVENT_TYPE IN ("click", "stream")
```