Reverse token filter

The reverse token filter reverses the order of the characters in each token, making suffix information accessible at the beginning of the reversed tokens during analysis.

This is useful for suffix-based searches:

The reverse token filter is useful when you need to perform suffix-based searches, such as in the following scenarios:

  • Suffix matching: Searching for words based on their suffixes, such as identifying words with a specific ending (for example, -tion or -ing).
  • File extension searches: Searching for files by their extensions, such as .txt or .jpg.
  • Custom sorting or ranking: By reversing tokens, you can implement unique sorting or ranking logic based on suffixes.
  • Autocomplete for suffixes: Implementing autocomplete suggestions that use suffixes rather than prefixes.

Example

The following example request creates a new index named my-reverse-index and configures an analyzer with a reverse filter:

  1. PUT /my-reverse-index
  2. {
  3. "settings": {
  4. "analysis": {
  5. "filter": {
  6. "reverse_filter": {
  7. "type": "reverse"
  8. }
  9. },
  10. "analyzer": {
  11. "my_reverse_analyzer": {
  12. "type": "custom",
  13. "tokenizer": "standard",
  14. "filter": [
  15. "lowercase",
  16. "reverse_filter"
  17. ]
  18. }
  19. }
  20. }
  21. }
  22. }

copy

Generated tokens

Use the following request to examine the tokens generated using the analyzer:

  1. GET /my-reverse-index/_analyze
  2. {
  3. "analyzer": "my_reverse_analyzer",
  4. "text": "hello world"
  5. }

copy

The response contains the generated tokens:

  1. {
  2. "tokens": [
  3. {
  4. "token": "olleh",
  5. "start_offset": 0,
  6. "end_offset": 5,
  7. "type": "<ALPHANUM>",
  8. "position": 0
  9. },
  10. {
  11. "token": "dlrow",
  12. "start_offset": 6,
  13. "end_offset": 11,
  14. "type": "<ALPHANUM>",
  15. "position": 1
  16. }
  17. ]
  18. }