# How can I index HTML tags

**URL:** <https://discuss.elastic.co/t/how-can-i-index-html-tags/265078>\
**Category:** Elasticsearch\
**Created:** [February 22, 2021, 2:28pm UTC](https://discuss.elastic.co/t/how-can-i-index-html-tags/265078 "2021-02-22T14:28:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterbe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peterbe/32/14836_2.png) [@peterbe](https://discuss.elastic.co/u/peterbe)\
**Post date:** [February 22, 2021, 2:28pm UTC](https://discuss.elastic.co/t/how-can-i-index-html-tags/265078/1 "2021-02-22T14:28:59Z")

</div>

I use the standard tokenizer and I don't use the `html_strip` char filter.  
How can I index HTML tags?

In fact, I want to be able to search with and without the `<` and `>` characters. I.e. a search for `<section>` should match `This is about the <section> tag`, but it should _not_ match `In this section we talks about stuff`. The standard tokenizer will turn that (search) text to `["section"]`.

As a bonus, if this can be done I don't have to worry about the `stop` char filter turning `<a>` into `[]`.

---

<div class="post-metadata">

**Author:** ![peterbe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peterbe/32/14836_2.png) [@peterbe](https://discuss.elastic.co/u/peterbe)\
**Post date:** [February 22, 2021, 2:54pm UTC](https://discuss.elastic.co/t/how-can-i-index-html-tags/265078/2 "2021-02-22T14:54:26Z")

</div>

I've found one hacky way to get it to work.

```auto
keep_html_char_filter = char_filter(
    "keep_html_char_filter",
    type="mapping",
    mappings=[
        "<a> => _a_",
        "<i> => _i_",
        "<b> => _b_",
        "<section> => _section_",
    ],
)

```

This seems to work. The tokens from `<a>` now becomes: `["_a_"]` and for `<section>` it becomes `["_section_", "section"]` which probably because of how I use my analyzer in conjunction with this char filter.

Still eager to hear some expert advice for the "proper" way to do this.

---

<div class="post-metadata">

**Author:** ![peterbe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peterbe/32/14836_2.png) [@peterbe](https://discuss.elastic.co/u/peterbe)\
**Post date:** [February 22, 2021, 3:02pm UTC](https://discuss.elastic.co/t/how-can-i-index-html-tags/265078/3 "2021-02-22T15:02:24Z")

</div>

I also tried:

```auto
keep_html_char_filter = char_filter(
    "keep_html_char_filter",
    type="html_strip",
    escaped_tags=["a", "b", "section", "i"],
)

```

But that didn't work. Because now the tokens become `a` and `section` etc. (instead of being removed) but when passed into the token filters the `stop` filter removes `a`.

---

<div class="post-metadata">

**Author:** ![peterbe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peterbe/32/14836_2.png) [@peterbe](https://discuss.elastic.co/u/peterbe)\
**Post date:** [February 22, 2021, 3:09pm UTC](https://discuss.elastic.co/t/how-can-i-index-html-tags/265078/4 "2021-02-22T15:09:27Z")

</div>

I figured it out!!

```auto
keep_html_char_filter = char_filter(
    "keep_html_char_filter",
    type="pattern_replace",
    pattern="<(\\w+)>",
    replacement="html$1html",
)

```

Now, if the text is `<b> <a> <i> <script>` I get the following tokens:

```auto
'htmlbhtml', 'htmlahtml', 'htmlihtml', 'htmlscripthtml'

```

That's great. A search for `<section>` will match `This is about the <section> tag` but it will not match `In this section we talks about stuff`  
And the `<a>` gets turned into `htmlahtml` which means it's not treated as `a` alone which would become a stopword.

Would still appreciate an experts advice. But otherwise happy to close this as resolved.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 22, 2021, 3:09pm UTC](https://discuss.elastic.co/t/how-can-i-index-html-tags/265078/5 "2021-03-22T15:09:51Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
