# Prefix highlighting

**URL:** <https://discuss.elastic.co/t/prefix-highlighting/378037>\
**Category:** Elastic Search\
**Created:** [May 12, 2025, 12:41pm UTC](https://discuss.elastic.co/t/prefix-highlighting/378037 "2025-05-12T12:41:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SoleusRex](https://avatars.discourse-cdn.com/v4/letter/s/e79b87/32.png) [@SoleusRex](https://discuss.elastic.co/u/SoleusRex)\
**Post date:** [May 12, 2025, 12:41pm UTC](https://discuss.elastic.co/t/prefix-highlighting/378037/1 "2025-05-12T12:41:37Z")

</div>

Using a `match_phrase_prefix query`, I've been trying to get highlights that include only the matching prefix itself. For example, for query `foo` and string `foobar`, what I would like is highlight `<em>foo</em>bar`, but what I get instead is `<em>foobar</em>`.

I've been working around this by reformatting the highlights returned by Elasticsearch in my own application code. This is a bit hacky, and lately has been found to cause some annoying bugs. I'm wondering if there's some way to construct the Elasticsearch query that will produce highlights in the way I describe naturally.

---

<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:** [May 19, 2025, 2:21am UTC](https://discuss.elastic.co/t/prefix-highlighting/378037/2 "2025-05-19T02:21:49Z")

</div>

Hi @SoleusRex

I don't know if you tried with edge-ngram tokenizer but it seems to work like you want.

```auto
PUT my-index
{
  "mappings": {
    "properties": {
      "my_field": {
        "type": "text",
        "analyzer": "my_analyzer"
      }
    }
  },
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "edge_ngram",
          "min_gram": 2,
          "max_gram": 10
        }
      }
    }
  }
}

POST my-index/_doc/1
{
    "my_field": "foobar"
}

POST my-index/_search
{
    "query": {
        "match_phrase_prefix": {
          "my_field": "foo"
        }
    },
    "highlight": {
        "fields": {
            "my_field": {}
        }
    }
}

```
