Flatten graph token filter

Flattens a token graph produced by a graph token filter, such as synonym_graph or word_delimiter_graph.

Flattening a token graph containing multi-position tokens makes the graph suitable for indexing. Otherwise, indexing does not support token graphs containing multi-position tokens.

Flattening graphs is a lossy process.

If possible, avoid using the flatten_graph filter. Instead, use graph token filters in search analyzers only. This eliminates the need for the flatten_graph filter.

The flatten_graph filter uses Lucene’s FlattenGraphFilter.

Example

To see how the flatten_graph filter works, you first need to produce a token graph containing multi-position tokens.

The following analyze API request uses the synonym_graph filter to add dns as a multi-position synonym for domain name system in the text domain name system is fragile:

  1. GET /_analyze
  2. {
  3. "tokenizer": "standard",
  4. "filter": [
  5. {
  6. "type": "synonym_graph",
  7. "synonyms": [ "dns, domain name system" ]
  8. }
  9. ],
  10. "text": "domain name system is fragile"
  11. }

The filter produces the following token graph with dns as a multi-position token.

token graph dns synonym ex

Indexing does not support token graphs containing multi-position tokens. To make this token graph suitable for indexing, it needs to be flattened.

To flatten the token graph, add the flatten_graph filter after the synonym_graph filter in the previous analyze API request.

  1. GET /_analyze
  2. {
  3. "tokenizer": "standard",
  4. "filter": [
  5. {
  6. "type": "synonym_graph",
  7. "synonyms": [ "dns, domain name system" ]
  8. },
  9. "flatten_graph"
  10. ],
  11. "text": "domain name system is fragile"
  12. }

The filter produces the following flattened token graph, which is suitable for indexing.

token graph dns invalid ex

Add to an analyzer

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

In this analyzer, a custom word_delimiter_graph filter produces token graphs containing catenated, multi-position tokens. The flatten_graph filter flattens these token graphs, making them suitable for indexing.

  1. PUT /my-index-000001
  2. {
  3. "settings": {
  4. "analysis": {
  5. "analyzer": {
  6. "my_custom_index_analyzer": {
  7. "type": "custom",
  8. "tokenizer": "standard",
  9. "filter": [
  10. "my_custom_word_delimiter_graph_filter",
  11. "flatten_graph"
  12. ]
  13. }
  14. },
  15. "filter": {
  16. "my_custom_word_delimiter_graph_filter": {
  17. "type": "word_delimiter_graph",
  18. "catenate_all": true
  19. }
  20. }
  21. }
  22. }
  23. }