Conditional token filter
Applies a set of token filters to tokens that match conditions in a provided predicate script.
This filter uses Lucene’sConditionalTokenFilter.
The followinganalyze API request uses thecondition filter to match tokens with fewer than 5 characters inTHE QUICK BROWN FOX. It then applies thelowercase filter to those matching tokens, converting them to lowercase.
GET /_analyze{ "tokenizer": "standard", "filter": [ { "type": "condition", "filter": [ "lowercase" ], "script": { "source": "token.getTerm().length() < 5" } } ], "text": "THE QUICK BROWN FOX"}
The filter produces the following tokens:
[ the, QUICK, BROWN, fox ]filter- (Required, array of token filters) Array of token filters. If a token matches the predicate script in the
scriptparameter, these filters are applied to the token in the order provided.
These filters can include custom token filters defined in the index mapping.
script- (Required,script object) Predicate script used to apply token filters. If a token matches this script, the filters in the
filterparameter are applied to the token.
For valid parameters, seeHow to write scripts. Only inline scripts are supported. Painless scripts are executed in theanalysis predicate context and require atoken property.
To customize thecondition filter, duplicate it to create the basis for a new custom token filter. You can modify the filter using its configurable parameters.
For example, the followingcreate index API request uses a customcondition filter to configure a newcustom analyzer. The customcondition filter matches the first token in a stream. It then reverses that matching token using thereverse filter.
PUT /palindrome_list{ "settings": { "analysis": { "analyzer": { "whitespace_reverse_first_token": { "tokenizer": "whitespace", "filter": [ "reverse_first_token" ] } }, "filter": { "reverse_first_token": { "type": "condition", "filter": [ "reverse" ], "script": { "source": "token.getPosition() === 0" } } } } }}