Shingle token filter

Add shingles, or word n-grams, to a token stream by concatenating adjacent tokens. By default, the shingle token filter outputs two-word shingles and unigrams.

For example, many tokenizers convert the lazy dog to [ the, lazy, dog ]. You can use the shingle filter to add two-word shingles to this stream: [ the, the lazy, lazy, lazy dog, dog ].

Tip

Shingles are often used to help speed up phrase queries, such as match_phrase. Rather than creating shingles using the shingles filter, we recommend you use the index-phrases mapping parameter on the appropriate text field instead.

This filter uses Lucene’s ShingleFilter.

The following analyze API request uses the shingle filter to add two-word shingles to the token stream for quick brown fox jumps:

				GET /_analyze
					{
  "tokenizer": "whitespace",
  "filter": [ "shingle" ],
  "text": "quick brown fox jumps"
}
		

The filter produces the following tokens:

[ quick, quick brown, brown, brown fox, fox, fox jumps, jumps ]
		

To produce shingles of 2-3 words, add the following arguments to the analyze API request:

  • min_shingle_size: 2
  • max_shingle_size: 3
				GET /_analyze
					{
  "tokenizer": "whitespace",
  "filter": [
    {
      "type": "shingle",
      "min_shingle_size": 2,
      "max_shingle_size": 3
    }
  ],
  "text": "quick brown fox jumps"
}
		

The filter produces the following tokens:

[ quick, quick brown, quick brown fox, brown, brown fox, brown fox jumps, fox, fox jumps, jumps ]
		

To only include shingles in the output, add an output_unigrams argument of false to the request.

				GET /_analyze
					{
  "tokenizer": "whitespace",
  "filter": [
    {
      "type": "shingle",
      "min_shingle_size": 2,
      "max_shingle_size": 3,
      "output_unigrams": false
    }
  ],
  "text": "quick brown fox jumps"
}
		

The filter produces the following tokens:

[ quick brown, quick brown fox, brown fox, brown fox jumps, fox jumps ]
		

The following create index API request uses the shingle filter to configure a new custom analyzer.

				PUT /my-index-000001
					{
  "settings": {
    "analysis": {
      "analyzer": {
        "standard_shingle": {
          "tokenizer": "standard",
          "filter": [ "shingle" ]
        }
      }
    }
  }
}
		

To set these parameters, create a custom shingle filter.

min_shingle_size
(Optional, integer) Minimum number of tokens in a shingle. Defaults to 2.
max_shingle_size
(Optional, integer) Maximum number of tokens in a shingle. Defaults to 2. Must be at least min_shingle_size.
output_unigrams
(Optional, Boolean) Whether to include the original input tokens alongside the shingles. Defaults to true.
output_unigrams_if_no_shingles
(Optional, Boolean) Whether to output the original input tokens when the filter produces no shingles. Applies when output_unigrams is false. Defaults to false.

The filter subtracts min_shingle_size from max_shingle_size and adds 1 if output_unigrams is true. The result must be at most the size limit, which defaults to 3. With that default, the largest size difference is 2 with unigrams and 3 without them.

To change the limit, set index.max_shingle_diff.

token_separator
(Optional, string) Separator between tokens in a shingle. Defaults to a space (" ").
filler_token
(Optional, string) String used to fill gaps when building shingles. A position increment greater than 1 indicates a gap, which can occur when the stop filter removes a stop word. The filler appears only in shingles. Defaults to an underscore (_).

To customize the shingle 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 shingle filter, my_shingle_filter, to configure a new custom analyzer.

The filter produces shingles of two to five tokens. It sets output_unigrams to false, so the size difference of 3 fits the default limit.

				PUT /my-index-000001
					{
  "settings": {
    "analysis": {
      "analyzer": {
        "en": {
          "tokenizer": "standard",
          "filter": [ "my_shingle_filter" ]
        }
      },
      "filter": {
        "my_shingle_filter": {
          "type": "shingle",
          "min_shingle_size": 2,
          "max_shingle_size": 5,
          "output_unigrams": false
        }
      }
    }
  }
}