Filter expression structure and elements
This section includes information about the structure of filter expressions and their elements.
Filter expression structure
The general structure of a filter expression is as follows:
EXCLUDE/INCLUDE ItemID/ActionID/UserID WHEREdataset type.fieldIN/NOT IN (value/parameter)
You can either manually create filter expressions or get help with expression syntax and structure by using the Expression builder in the console.
Filter expression elements
Use the following elements to create filter expressions:
- INCLUDE or EXCLUDE
-
Use
INCLUDEto limit recommendations to only items that meet the filter criteria OR useEXCLUDEto remove all items that meet the filter criteria. - ItemID/ActionID/UserID
-
Use one of these elements after the
INCLUDEorEXCLUDEelement. The element you use depends on whether you are filtering items (for item recommendations), actions (for action recommendations), or users (for user segments). - WHERE
-
Use
WHEREto check conditions for items, actions, or users. You must use theWHEREelement after theItemID,ActionID, orUserID. - AND/OR
-
To chain multiple conditions together within the same filter expression, use
ANDorOR. Conditions chained together usingANDorORcan only affect fields of the dataset used in the first condition. - Dataset.field
-
Provide the dataset and the metadata field that you want to filter recommendations by in
dataset.fieldformat. For example, to filter item recommendations based on the genres field in your Items dataset, you would use Items.genres in your filter expression. - IF condition
-
Use an
IFcondition only to check conditions for theCurrentUserand only once at the end of an expression. However, you can extend anIFcondition usingAND. - CurrentUser.attribute
-
To filter item recommendations based on the user you are getting recommendations for, in only an IF condition, use
CurrentUserand provide the user field. For example,CurrentUser.AGE. - CurrentItem.attribute
-
For only related items recipes and use cases, use
CurrentItem.attributeto filter items based on an attribute of the item you specify in your request for related items recommendations. For example,CurrentItem.GENREorCurrentItem.PRICE.You can apply a filter with the CurrentItem element only if your domain use case or custom recipe generates related items recommendations, such as the Similar-Items recipe or the More Like X domain use case. The first time you create a filter with a
CurrentItemelement, filter creation can a few minutes. If you use Amazon KMS for encryption, filter creation can take up to 15 minutes. - IN/NOT IN
-
Use
INorNOT INas comparison operators to filter based on matching (or not matching) one or more string values. Amazon Personalize filters only on exact strings. - Comparison operators
-
Use =, <, <=, >, >=, and != operators to test numerical data, including data passed in a placeholder parameter, for equality.
- Asterisk (*) character
-
Use
*to include or exclude interactions of all types. Use*only for filter expressions that use theEVENT_TYPEfield of anInteractionsdataset. - Pipe separator
-
Use the pipe separator (
|) to chain multiple expressions together. For more information, see Combining multiple expressions. - Parameters
-
For expressions that use comparison operators or the
INoperator, use the dollar sign ($) and a parameter name to add a placeholder parameter as a value. For example,$GENRES. For this example, when you get recommendations, you supply the genre or genres to filter by.Note
You define a parameter name when you add it to an expression. The parameter name does not have to match the field name. We recommend that you use a parameter name that is similar to the field name and easy to remember. You use the parameter name (case sensitive) when you apply the filter to recommendations requests. For an example that shows how to apply a filter with placeholder parameters when using the Amazon SDKS, see Applying a filter (Amazon SDKs).