Edge n-gram tokenizer

The edge_ngram tokenizer first breaks text down into words whenever it encounters one of a list of specified characters, then it emits N-grams of each word where the start of the N-gram is anchored to the beginning of the word.

Edge N-Grams are useful for search-as-you-type queries.

When you need search-as-you-type for text which has a widely known order, such as movie or song titles, the completion suggester is a much more efficient choice than edge N-grams. Edge N-grams have the advantage when trying to autocomplete words that can appear in any order.

Example output

With the default settings, the edge_ngram tokenizer treats the initial text as a single token and produces N-grams with minimum length 1 and maximum length 2:

  1. POST _analyze
  2. {
  3. "tokenizer": "edge_ngram",
  4. "text": "Quick Fox"
  5. }

The above sentence would produce the following terms:

  1. [ Q, Qu ]

These default gram lengths are almost entirely useless. You need to configure the edge_ngram before using it.

Configuration

The edge_ngram tokenizer accepts the following parameters:

min_gram

Minimum length of characters in a gram. Defaults to 1.

max_gram

Maximum length of characters in a gram. Defaults to 2.

See Limitations of the max_gram parameter.

token_chars

Character classes that should be included in a token. Elasticsearch will split on characters that don’t belong to the classes specified. Defaults to [] (keep all characters).

Character classes may be any of the following:

  • letter —  for example a, b, ï or
  • digit —  for example 3 or 7
  • whitespace —  for example " " or "\n"
  • punctuation — for example ! or "
  • symbol —  for example $ or
  • custom —  custom characters which need to be set using the custom_token_chars setting.

custom_token_chars

Custom characters that should be treated as part of a token. For example, setting this to +-_ will make the tokenizer treat the plus, minus and underscore sign as part of a token.

Limitations of the max_gram parameter

The edge_ngram tokenizer’s max_gram value limits the character length of tokens. When the edge_ngram tokenizer is used with an index analyzer, this means search terms longer than the max_gram length may not match any indexed terms.

For example, if the max_gram is 3, searches for apple won’t match the indexed term app.

To account for this, you can use the truncate token filter with a search analyzer to shorten search terms to the max_gram character length. However, this could return irrelevant results.

For example, if the max_gram is 3 and search terms are truncated to three characters, the search term apple is shortened to app. This means searches for apple return any indexed terms matching app, such as apply, snapped, and apple.

We recommend testing both approaches to see which best fits your use case and desired search experience.

Example configuration

In this example, we configure the edge_ngram tokenizer to treat letters and digits as tokens, and to produce grams with minimum length 2 and maximum length 10:

  1. PUT my-index-00001
  2. {
  3. "settings": {
  4. "analysis": {
  5. "analyzer": {
  6. "my_analyzer": {
  7. "tokenizer": "my_tokenizer"
  8. }
  9. },
  10. "tokenizer": {
  11. "my_tokenizer": {
  12. "type": "edge_ngram",
  13. "min_gram": 2,
  14. "max_gram": 10,
  15. "token_chars": [
  16. "letter",
  17. "digit"
  18. ]
  19. }
  20. }
  21. }
  22. }
  23. }
  24. POST my-index-00001/_analyze
  25. {
  26. "analyzer": "my_analyzer",
  27. "text": "2 Quick Foxes."
  28. }

The above example produces the following terms:

  1. [ Qu, Qui, Quic, Quick, Fo, Fox, Foxe, Foxes ]

Usually we recommend using the same analyzer at index time and at search time. In the case of the edge_ngram tokenizer, the advice is different. It only makes sense to use the edge_ngram tokenizer at index time, to ensure that partial words are available for matching in the index. At search time, just search for the terms the user has typed in, for instance: Quick Fo.

Below is an example of how to set up a field for search-as-you-type.

Note that the max_gram value for the index analyzer is 10, which limits indexed terms to 10 characters. Search terms are not truncated, meaning that search terms longer than 10 characters may not match any indexed terms.

  1. PUT my-index-00001
  2. {
  3. "settings": {
  4. "analysis": {
  5. "analyzer": {
  6. "autocomplete": {
  7. "tokenizer": "autocomplete",
  8. "filter": [
  9. "lowercase"
  10. ]
  11. },
  12. "autocomplete_search": {
  13. "tokenizer": "lowercase"
  14. }
  15. },
  16. "tokenizer": {
  17. "autocomplete": {
  18. "type": "edge_ngram",
  19. "min_gram": 2,
  20. "max_gram": 10,
  21. "token_chars": [
  22. "letter"
  23. ]
  24. }
  25. }
  26. }
  27. },
  28. "mappings": {
  29. "properties": {
  30. "title": {
  31. "type": "text",
  32. "analyzer": "autocomplete",
  33. "search_analyzer": "autocomplete_search"
  34. }
  35. }
  36. }
  37. }
  38. PUT my-index-00001/_doc/1
  39. {
  40. "title": "Quick Foxes"
  41. }
  42. POST my-index-00001/_refresh
  43. GET my-index-00001/_search
  44. {
  45. "query": {
  46. "match": {
  47. "title": {
  48. "query": "Quick Fo",
  49. "operator": "and"
  50. }
  51. }
  52. }
  53. }

The autocomplete analyzer indexes the terms [qu, qui, quic, quick, fo, fox, foxe, foxes].

The autocomplete_search analyzer searches for the terms [quick, fo], both of which appear in the index.