liveBook · Manning

Html_strip character filter

As explained in Elasticsearch in Action, Second Edition

The html_strip character filter removes HTML markup from an input character stream before tokenization, optionally preserving specified tags.

The html_strip character filter removes HTML tags from an input stream before tokenization. Use it when indexed text contains markup that should not affect search analysis. section 7.6 section 7.6.1

Strip headings before tokenization

POST _analyze { "text": "<h1>Where is my cheese?</h1>", "tokenizer": "standard", "char_filter": ["html_strip"] } The tags are removed first, leaving the words “Where”, “is”, “my”, and “cheese” for the standard tokenizer. section 7.6.1

Use html_strip inside a custom analyzer alongside a tokenizer and token filters. Character filters run first, so later analysis receives text without the removed markup. section 7.4 section 7.6

Preserve selected tags

Define a custom html_strip filter with an escaped_tags array when some tags must remain unparsed: "char_filter": { "my_html_strip_filter": { "type": "html_strip", "escaped_tags": ["h1"] } } With <h1>Hello,</h1> <h2>World!</h2>, the h1 tags remain while the h2 tags are stripped. section 7.6.1

Listing 7.24 Custom analyzer with filters and a tokenizer
PUT index_with_custom_analyzer
{
  "settings": {
    "analysis": {
      "analyzer": {
        "custom_analyzer":{                 #1
          "type":"custom",                  #2
          "char_filter":["html_strip"],     #3
          "tokenizer":"standard",           #4
          "filter":["uppercase"]            #5
        }
      }
    }
  }
}
#1 Specifies the custom analyzer
#2 The type must be custom to let Elasticsearch know about our custom analyzer.
#3 Array of character filters
#4 Declares a single tokenizer (standard, in this case)
#5 Token filter to uppercase incoming tokens

Inspect the complete custom analyzer configuration, including the character-filter array, standard tokenizer, and uppercase token filter. section 7.4

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains removal, analyzer ordering, custom configuration, and tag preservation, but does not cover implementation internals.
Examples
Demonstrates removing h1 markup, preserving selected h1 tags, and combining html_strip with standard or keyword tokenizers.
What it assumes
Assumes basic analyzer terminology while introducing custom analyzer structure, character-filter ordering, and tokenizer interaction.
What you can do after
Apply it via Elasticsearch analyzer settings and the _analyze API to strip markup or preserve selected tags.
How it connects
Connects character filters with custom analyzers, tokenizers, token filters, and the _analyze API.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.