Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Mastering Elasticsearch Built-In Text Tokenizers

Tech Nov 15 18

Core Functionality

In Elasticsearch, tokenization serves as the foundational step for full-text search operations. When processing fields defined with the text data type, the engine relies on analyzers to break raw strings into discrete units known as tokens. While the platform ships with several out-of-the-box components, it also supports fully custom implementations through plugins or index settings. These built-in utilities fall into three primary categories: word-level segmentation, character-n-gram generation, and structured/specialized parsing.

Word-Level Segmentation Tools

These components focus on splitting continuous text based on linguistic rules, whitespace, or punctuation boundaries.

Standard Tokenizer

Utilizing the ICU (International Components for Unicode) text boundary algorithm, this tool identifies word boundaries across most scripts. It automatical converts output to lowercase and strips the majority of punctuation marks. Users can refine its behavior by defining exclusion lists.

Input: "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
// Resulting array after processing:
["the", "2", "quick", "brown", "foxes", "jumped", "over", "the", "lazy", "dogs", "bone"]

Configuration snippet:

{
  "settings": {
    "analysis": {
      "analyzer": {
        "prod_standard_v2": {
          "type": "standard",
          "stopwords_path": "config/custom_stop.txt",
          "max_token_length": 40
        }
      }
    }
  }
}

Key parameters include max_token_length (defaults to 255, splits oversized tokens), and stopwords (accepts language codes like _english_ or a custom file path via stopwords_path).

Simple & Lowercase Processing

The Simple Tokenizer acts aggressively on punctuation and digits. It divides text wherever a non-alphabetical character appears and forces all results to lowercase. It is generally unsuitable for CJK languages.

Input: "Project Alpha-9 launch!"
Output: ["project", "alpha", "9", "launch"]

This component does not expose any tunible settings.

Conversely, the Lowercase Filter focuses exclusively on case normalization. It is typically chained after another tokenizer rather than functioning independently, transforming every character to its lowercase equivalent before passing it downstream.

Whitespace & Classic Parsers

The Whitespace Tokenizer operates purely on space characters. Unlike other models, it preserves original casing and leaves punctuation intact.

Input: "LogLevel ERROR SystemInit"
Output: ["LogLevel", "ERROR", "SystemInit"]

The Classic Tokenizer offers legacy compatibility and improves upon the standard model for English-heavy datasets. It retains dots inside acronyms or domain names, recognizes email addresses and URLs as single tokens, and handles hyphenated words gracefully.

{
  "mappings": {
    "properties": {
      "content": {
        "type": "text",
        "fields": {
          "legacy": {
            "type": "text",
            "analyzer": "classic_legacy_v1"
          }
        }
      }
    }
  },
  "settings": {
    "analysis": {
      "analyzer": {
        "classic_legacy_v1": {
          "tokenizer": "split_by_classic"
        }
      },
      "tokenizer": {
        "split_by_classic": {
          "type": "classic",
          "max_token_length": 100
        }
      }
    }
  }
}

The sole adjustable attribute is max_token_length, controlling chunk size limits.

Structured Pattern Matching

For highly specific formats, Elasticsearch provides tools like the UAX URL Email Tokenizer. It isolates contact information and web links, treating them as indivisible units while splitting surrounding prose normally.

Contact: admin@internal.net for help
Tokens: [Contact:, admin@internal.net, for, help]

Similar to others, it accepts max_token_length overrides. Additionally, pattern-based tools like pattern, keyword_pattern, and path_hierarchy addres comma-separated values, regex-delimited streams, and directory trees respectively.

Character N-Gram Generators

When searching requires prefix completion or partial match capabilities, character-level segmentation becomes essential. These parsers extract overlapping sequences of fixed length from the input stream.

N-Gram Tokenizer breaks text into fragments using spaces or punctuation as delimiters, then generates all possible substrings within the configured bounds.

Input: "NodeJS"
Sequence: N, No, od, de, deJ, eJS, JS

Edge N-Gram Tokenizer differs by only generating prefixes starting from the beginning of the word. This makes it highly efficient for autocomplete features.

Input: "NodeJS"
Sequence: N, No, nod, node

Both rely on identical tuning knobs:

  • min_gram: Minimum character count per generated fragment.
  • max_gram: Maximum character count limit.
  • token_chars: Array specifying allowed character sets (e.g., ["letter", "digit", "punctuation"]). Characters outside this list trigger split points.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.