# Multiple pre & post tags in highlighter query

**URL:** <https://discuss.elastic.co/t/multiple-pre-post-tags-in-highlighter-query/318492>\
**Category:** Elasticsearch\
**Created:** [November 9, 2022, 3:57am UTC](https://discuss.elastic.co/t/multiple-pre-post-tags-in-highlighter-query/318492 "2022-11-09T03:57:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mayanknc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayanknc/32/115266_2.png) [@mayanknc](https://discuss.elastic.co/u/mayanknc)\
**Post date:** [November 9, 2022, 3:57am UTC](https://discuss.elastic.co/t/multiple-pre-post-tags-in-highlighter-query/318492/1 "2022-11-09T03:57:20Z")

</div>

Hi  
I want to highlight individual query string's match result with different pre & post tags.

- Create Index

```auto
PUT multilang_index
{
  "mappings": {
    "dynamic_templates": [
			{
				"stripped_section_template": {
					"path_match": "STRIPPED_SECTION.*",
					"mapping": {
						"search_analyzer": "index_analyzer",
						"analyzer": "index_analyzer",
						"type": "text",
						"term_vector": "with_positions_offsets"
					}
				}
			}
		]
  },
  "settings": {
		"analysis": {
			"analyzer": {
				"index_analyzer": {
					"filter": [
						"lowercase"
					],
					"tokenizer": "standard"
				}
			}
		}
	}
}

```

- Index document

```auto
POST multilang_index/_doc/1
{
  "STRIPPED_SECTION": {
    "description": "failure of heart and increase in blood pressure needs to be examined",
    "id": 1
  }
}

```

- Perform search query

```auto
POST multilang_index/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "fuzziness": "AUTO", 
            "query": "blood pressure",
            "fields": [
              "STRIPPED_SECTION.*"
            ]
          }
        },
        {
          "multi_match": {
            "fuzziness": "AUTO", 
            "query": "heart failure",
            "fields": [
              "STRIPPED_SECTION.*"
            ]
          }
        },
        {
          "multi_match": {
            "fuzziness": "AUTO", 
            "query": "examine",
            "fields": [
              "STRIPPED_SECTION.*"
            ]
          }
        }
      ],
      "minimum_should_match": 1
    }
  },
  "highlight": {
    "fields": {
      "STRIPPED_SECTION.*": {
        "type": "fvh",
        "pre_tags": [
          "<em1>",
          "<em2>",
          "<em3>"
        ],
        "post_tags": [
          "</em1>",
          "</em2>",
          "</em3>"
        ]
      }
    }
  }
}

```

- ACTUAL Output

```auto
"highlight" : {
          "STRIPPED_SECTION.description" : [
            "<em1>failure</em1> of <em3>heart</em3> and increase in <em1>blood</em1> <em2>pressure</em2> needs to be <em2>examined</em2>"
          ]
}

```

In ACTUAL Output, random tags assigned to highlighted keywords.

- EXPECTED Output

```auto
"highlight" : {
          "STRIPPED_SECTION.description" : [
            "<em2>failure</em2> of <em2>heart</em2> and increase in <em1>blood</em1> <em1>pressure</em1> needs to be <em3>examined</em3>"
          ]
}

```

I am expecting each matched word from one query string should highlighted with same tag

For Ex :

- All match result of 'heart failure' query should have same tag like failure, heart
- All match result of 'blood pressure' query should have same tag like blood, pressure
- All match result of 'examine' query should have same tag like examined

Is there any way to achieve above mentioned behaviour?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood1/32/101255_2.png) [@Mark\_Harwood1](https://discuss.elastic.co/u/Mark_Harwood1)\
**Post date:** [November 9, 2022, 7:53am UTC](https://discuss.elastic.co/t/multiple-pre-post-tags-in-highlighter-query/318492/2 "2022-11-09T07:53:04Z")

</div>

Hi Mayank,  
This may be of interest

> [@Mapping the query to the highlight using FVH](https://discuss.elastic.co/t/mapping-the-query-to-the-highlight-using-fvh/257836/2):
>
> Thanks for the write-up. One approach for more transparency into what search terms matched might be to use the highlighter that ships with the [annotated\_text](https://www.elastic.co/blog/search-for-things-not-strings-with-the-annotated-text-plugin) field which is installed as an extra [plugin](https://www.elastic.co/guide/en/elasticsearch/plugins/7.10/mapper-annotated-text.html). Although it's designed for use on annotated\_text fields it also works with text fields and uses a markdown-like syntax for adding annotations to text e.g. given this doc: { "text": "The new hot technology has emerged!" } and this query { "query": { "bool": { "should": [ {"match": {…

---

<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:** [December 7, 2022, 7:53am UTC](https://discuss.elastic.co/t/multiple-pre-post-tags-in-highlighter-query/318492/3 "2022-12-07T07:53:48Z")

</div>

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