# Elasticsearch Highlight the result of script fields

**URL:** <https://discuss.elastic.co/t/elasticsearch-highlight-the-result-of-script-fields/316121>\
**Category:** Elasticsearch\
**Created:** [October 8, 2022, 8:22am UTC](https://discuss.elastic.co/t/elasticsearch-highlight-the-result-of-script-fields/316121 "2022-10-08T08:22:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AmirMohammad\_Safari](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amirmohammad_safari/32/98042_2.png) [@AmirMohammad\_Safari](https://discuss.elastic.co/u/AmirMohammad_Safari)\
**Post date:** [October 8, 2022, 8:22am UTC](https://discuss.elastic.co/t/elasticsearch-highlight-the-result-of-script-fields/316121/1 "2022-10-08T08:22:29Z")

</div>

I write out an analyzer to remove the HTML tags in my search results, After that I thought I could highlight the results with a common query, But in the highlighting field I got other html contents that you removed with script. Would you please help me to highlight the results without html tags that I saved in my db?  
My mapping and settings:

```json
{
  "settings": {
    "analysis": {
      "filter": {
        "my_pattern_replace_filter": {
          "type": "pattern_replace",
          "pattern": "\n",
          "replacement": ""
        }
      },
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "standard",
          "filter": [
            "lowercase"
          ],
          "char_filter": [
            "html_strip"
          ]
        },
        "parsed_analyzer": {
          "type": "custom",
          "tokenizer": "keyword",
          "char_filter": [
            "html_strip"
          ],
          "filter": [
            "my_pattern_replace_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "html": {
        "type": "text",
        "analyzer": "my_analyzer",
        "fields": {
          "raw": {
            "type": "text",
            "fielddata": true,
            "analyzer": "parsed_analyzer"
          }
        }
      }
    }
  }
}

```

Search Query:

```auto
POST idx_test/_search

{
  "script_fields": {
    "raw": {
      "script": "doc['html.raw']"
    }
  }, 
  "query": {
    "match": {
      "html": "more"
    }
  },"highlight": {
    "fields": {
      "*":{ "pre_tags" : ["<strong>"], "post_tags" : ["</strong>"] }
    }
  }
}

```

Result:

```auto
"hits": [
    {
        "_index": "idx_test2",
        "_type": "_doc",
        "_id": "GijDsYMBjgX3UBaguGxc",
        "_score": 0.2876821,
        "fields": {
            "raw": [
                "Test More test"
            ]
        },
        "highlight": {
            "html": [
                "<html><body><h1 style=\"font-family: Arial\">Test</h1> <span><strong>More</strong> test</span></body></html>"
            ]
        }
    }
]

```

Result that I want to get:

```auto
"hits": [
    {
        "_index": "idx_test2",
        "_type": "_doc",
        "_id": "GijDsYMBjgX3UBaguGxc",
        "_score": 0.2876821,
        "fields": {
            "raw": [
                "Test <strong>More</strong> test"
            ]
        }
]

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [October 9, 2022, 1:15pm UTC](https://discuss.elastic.co/t/elasticsearch-highlight-the-result-of-script-fields/316121/2 "2022-10-09T13:15:25Z")

</div>

Hi @AmirMohammad_Safari

I thought of another solution. You could index two fields, the original html and the html\_extract which has only the text.  
You would have to use a processor to just index the text coming from the message and highligths would work.

**Mapping**

```
PUT idx_html_strip
{
  "mappings": {
    "properties": {
      "html": {
        "type": "text"
      },
      "html_extract": {
        "type": "text"
      }
    }
  }
}

```

**Processor Pipeline**

```auto
PUT /_ingest/pipeline/pipe_html_strip
{
  "description": "_description",
  "processors": [
    {
      "html_strip": {
        "field": "html",
        "target_field": "html_extract"
      }
    },
    {
      "script": {
        "lang": "painless",
        "source": "ctx['html_raw'] = ctx['html_raw'].replace('\n',' ').trim()"
      }
    }
  ]
}

```

**Index Data**

Note the use ?pipeline=pipe\_html\_strip

```
POST idx_html_strip/_doc?pipeline=pipe_html_strip
{
  "html": """<html><body><h1 style=\"font-family: Arial\">Test</h1> <span><strong>More</strong> test</span></body></html>"""
}

```

**Query**

```
GET idx_html_strip/_search?filter_path=hits.hits._source,hits.hits.highlight
{
  "query": {
    "multi_match": {
      "query": "More",
      "fields": ["html", "html_extract"]
    }
  },"highlight": {
    "fields": {
      "*":{ "pre_tags" : ["<strong>"], "post_tags" : ["</strong>"] }
    }
  }
}

```

**Results**

```
{
  "hits": {
    "hits": [
      {
        "_source": {
          "html": """<html><body><h1 style=\"font-family: Arial\">Test</h1> <span><strong>More</strong> test</span></body></html>""",
          "html_extract": "Test More test"
        },
        "highlight": {
          "html": [
            """<html><body><h1 style=\"font-family: Arial\">Test</h1> <span><strong><strong>More</strong></strong> test</span></body>"""
          ],
          "html_extract": [
            "Test <strong>More</strong> test"
          ]
        }
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![AmirMohammad\_Safari](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amirmohammad_safari/32/98042_2.png) [@AmirMohammad\_Safari](https://discuss.elastic.co/u/AmirMohammad_Safari)\
**Post date:** [October 9, 2022, 2:11pm UTC](https://discuss.elastic.co/t/elasticsearch-highlight-the-result-of-script-fields/316121/3 "2022-10-09T14:11:19Z")

</div>

Thanks a lot mate for answering, I tried to reproduce your answer but I got the following error.  
Request that I was sent:

```auto
PUT /_ingest/pipeline/pipe_html_strip

{
  "description": "_description",
  "processors": [
    {
      "html_strip": {
        "field": "html",
        "target_field": "html_extract"
      }
    },
    {
      "script": {
        "lang": "painless",
        "source": "ctx['html_extract'] = ctx['html_extract'].replace('','').trim()"
      }
    }
  ]
}

```

Error:

```auto
{
    "error": {
        "root_cause": [
            {
                "type": "parse_exception",
                "reason": "No processor type exists with name [html_strip]",
                "processor_type": "html_strip"
            }
        ],
        "type": "parse_exception",
        "reason": "No processor type exists with name [html_strip]",
        "processor_type": "html_strip"
    },
    "status": 400
}

```

---

<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:** [November 6, 2022, 2:12pm UTC](https://discuss.elastic.co/t/elasticsearch-highlight-the-result-of-script-fields/316121/4 "2022-11-06T14:12:04Z")

</div>

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