Conditional token filter

Applies a set of token filters to tokens that match conditions in a provided predicate script.

This filter uses Lucene’s ConditionalTokenFilter.

Example

The following analyze API request uses the condition filter to match tokens with fewer than 5 characters in THE QUICK BROWN FOX. It then applies the lowercase filter to those matching tokens, converting them to lowercase.

  1. GET /_analyze
  2. {
  3. "tokenizer": "standard",
  4. "filter": [
  5. {
  6. "type": "condition",
  7. "filter": [ "lowercase" ],
  8. "script": {
  9. "source": "token.getTerm().length() < 5"
  10. }
  11. }
  12. ],
  13. "text": "THE QUICK BROWN FOX"
  14. }

The filter produces the following tokens:

  1. [ the, QUICK, BROWN, fox ]

Configurable parameters

filter

(Required, array of token filters) Array of token filters. If a token matches the predicate script in the script parameter, 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 filter parameter are applied to the token.

For valid parameters, see Script parameters. Only inline scripts are supported. Painless scripts are executed in the analysis predicate context and require a token property.

Customize and add to an analyzer

To customize the condition 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 following create index API request uses a custom condition filter to configure a new custom analyzer. The custom condition filter matches the first token in a stream. It then reverses that matching token using the reverse filter.

  1. PUT /palindrome_list
  2. {
  3. "settings": {
  4. "analysis": {
  5. "analyzer": {
  6. "whitespace_reverse_first_token": {
  7. "tokenizer": "whitespace",
  8. "filter": [ "reverse_first_token" ]
  9. }
  10. },
  11. "filter": {
  12. "reverse_first_token": {
  13. "type": "condition",
  14. "filter": [ "reverse" ],
  15. "script": {
  16. "source": "token.getPosition() === 0"
  17. }
  18. }
  19. }
  20. }
  21. }
  22. }