AI Hints
Home›Elasticsearch Tutorial›Stemming, Synonyms and Custom Analyzers
Elasticsearch · Lesson 57 of 120

Custom Analyzers

Your own pipeline.

⏱ 2 min read▶ Video at 53:41🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

Trailhead's names contain things like Ultra-Light and Ultralight. Some descriptions contain HTML. A custom analyzer puts exactly the right pieces together.

  • Messy text
  • HTML
  • Pick the pieces

Define and test

In settings, we build an analyzer called clean text. It strips HTML, uses the standard tokenizer, lower cases, folds accents, and stems English. Then we test it on the index. Run it.

Dev Tools Consoletext
PUT products
{ "settings": { "analysis": { "analyzer": { "clean_text": {
    "type": "custom",
    "char_filter": ["html_strip"],
    "tokenizer": "standard",
    "filter": ["lowercase", "asciifolding", "porter_stem"] } } } } }

GET products/_analyze?filter_path=tokens.token
{ "analyzer": "clean_text", "text": "<p>Crème Café Hiking Boots</p>" }
Output
# PUT products 200 OK
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "products"
}

# GET products/_analyze 200 OK
{
  "tokens": [
    {"token": "creme"},
    {"token": "cafe"},
    {"token": "hike"},
    {"token": "boot"}
  ]
}

Building blocks

Character filters include HTML strip, mapping and pattern replace. Tokenizers include standard, whitespace, pattern and n gram. Token filters include lowercase, stop, stemmers, synonyms, and ASCII folding.

keyvalue
Char filtershtml_strip, mapping
Tokenizersstandard, whitespace, ngram
Token filterslowercase, stop, stemmer
Moresynonyms, asciifolding

Summary

A custom analyzer combines character filters, one tokenizer and token filters. Test it with_analyze before you use it. Next, normalizers for keyword fields.

Key takeaways
  • custom
  • Pick the pieces
  • Test with _analyze
Next up: Normalizers — tidy keywords