Prefix highlighting

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.

Hi @SoleusRex

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

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": {}
        }
    }
}