# Edge Ngram gives bad highlight when using position offsets

**URL:** <https://discuss.elastic.co/t/edge-ngram-gives-bad-highlight-when-using-position-offsets/10210>\
**Category:** Elasticsearch\
**Created:** [January 1, 2013, 9:14pm UTC](https://discuss.elastic.co/t/edge-ngram-gives-bad-highlight-when-using-position-offsets/10210 "2013-01-01T21:14:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebastien\_Lorber](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sebastien_lorber/32/2551_2.png) [@Sebastien\_Lorber](https://discuss.elastic.co/u/Sebastien_Lorber)\
**Post date:** [January 1, 2013, 9:14pm UTC](https://discuss.elastic.co/t/edge-ngram-gives-bad-highlight-when-using-position-offsets/10210/1 "2013-01-01T21:14:17Z")

</div>

Hello,

I've posted a question on StackOverflow but nobody answered:

> <https://stackoverflow.com/questions/11303660/elasticsearch-edgengram-highlight-term-vector-bad-highlights>

Does someone know why when using position offsets, edge ngrams only  
highlights the smallest matching token?

I would expect it to highlight the longest matching token, because with an  
edge-ngrams with min size = 3, this gives me a highlight of 3 chars while  
there should be another matching token of 5 or 6 chars (for exemple)

Thanks

--

---

<div class="post-metadata">

**Author:** ![Sebastien\_Lorber](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sebastien_lorber/32/2551_2.png) [@Sebastien\_Lorber](https://discuss.elastic.co/u/Sebastien_Lorber)\
**Post date:** [January 3, 2013, 1:51pm UTC](https://discuss.elastic.co/t/edge-ngram-gives-bad-highlight-when-using-position-offsets/10210/2 "2013-01-03T13:51:23Z")

</div>

Nobody knows?

On Tuesday, January 1, 2013 10:14:17 PM UTC+1, Sébastien Lorber wrote:

> Hello,
> 
> I've posted a question on StackOverflow but nobody answered:
> 
> [java - Elasticsearch - EdgeNgram + highlight + term\_vector = bad highlights - Stack Overflow](http://stackoverflow.com/questions/11303660/elasticsearch-edgengram-highlight-term-vector-bad-highlights)
> 
> Does someone know why when using position offsets, edge ngrams only  
> highlights the smallest matching token?
> 
> I would expect it to highlight the longest matching token, because with an  
> edge-ngrams with min size = 3, this gives me a highlight of 3 chars while  
> there should be another matching token of 5 or 6 chars (for exemple)
> 
> Thanks

--

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [January 3, 2013, 3:17pm UTC](https://discuss.elastic.co/t/edge-ngram-gives-bad-highlight-when-using-position-offsets/10210/3 "2013-01-03T15:17:14Z")

</div>

Hi Sebastien,

What version of ES are you using and are you using the the ngram  
tokenizer also during query time?  
In your case the edge ngram should be applied only during index time.  
Otherwise your query term gets expanded into `cou` and `couc`.

With the latest ES version (0.20.2) the highlighting on a field that  
has the edge ngram filter configured works as expected:

> <https://gist.github.com/martijnvg/4444187>

Martijn

On 3 January 2013 14:51, Sébastien Lorber [lorber.sebastien@gmail.com](mailto:lorber.sebastien@gmail.com) wrote:

> Nobody knows?
> 
> On Tuesday, January 1, 2013 10:14:17 PM UTC+1, Sébastien Lorber wrote:
> 
> > Hello,
> > 
> > I've posted a question on StackOverflow but nobody answered:
> > 
> > [java - Elasticsearch - EdgeNgram + highlight + term\_vector = bad highlights - Stack Overflow](http://stackoverflow.com/questions/11303660/elasticsearch-edgengram-highlight-term-vector-bad-highlights)
> > 
> > Does someone know why when using position offsets, edge ngrams only  
> > highlights the smallest matching token?
> > 
> > I would expect it to highlight the longest matching token, because with an  
> > edge-ngrams with min size = 3, this gives me a highlight of 3 chars while  
> > there should be another matching token of 5 or 6 chars (for exemple)
> > 
> > Thanks
> 
> --

--  
Met vriendelijke groet,

Martijn van Groningen

--

---

<div class="post-metadata">

**Author:** ![Sebastien\_Lorber](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sebastien_lorber/32/2551_2.png) [@Sebastien\_Lorber](https://discuss.elastic.co/u/Sebastien_Lorber)\
**Post date:** [January 4, 2013, 11:27pm UTC](https://discuss.elastic.co/t/edge-ngram-gives-bad-highlight-when-using-position-offsets/10210/4 "2013-01-04T23:27:16Z")

</div>

Hi,

By mistake I sent a private message so for people interested here is the  
conversation:

* * *

Thanks

I guess the problem is that I read almost everywhere to always use the same  
analyzers for index and query time.  
I'm not a search export so I follow this advice.

But yes it seems right that it is because of the search term tokenization  
into "cou" and "couc" and other tokens...

So in my index there are "cou" and "couc" and other tokens...  
And my query is tokenized into "cou" and "couc" and other tokens...

Wouldn't it make sense that the largest maching token is choosed?  
My intuition tells me that the "positions offsets" are kind of attached to  
the indexed tokens no?  
So if the match happens on many tokens, it would be nice to:

- Not have a biased scoring because of the number of tokens
- Keep the largest token so that my highlight is based on the position  
offsets of "couc" and not "cou"  
Perhaps there is an option to do that?

* * *

Answer:

In the case of EgdeNGram it is best to only apply it at index time  
(e.g. in the case for autocomplete like functionality).

I think the fast vector highlighter just prefers the shortest match if  
it occurs on the same location. Each token does have an offset  
stored in the term vector inside the index, but I'm not sure if what  
you want is possible with fast vector highlighting.

For normal ngram token filter it usually does make sense to have have  
it configured at both query and index time.

Martijn

On Tuesday, January 1, 2013 10:14:17 PM UTC+1, Sébastien Lorber wrote:

> Hello,
> 
> I've posted a question on StackOverflow but nobody answered:
> 
> [java - Elasticsearch - EdgeNgram + highlight + term\_vector = bad highlights - Stack Overflow](http://stackoverflow.com/questions/11303660/elasticsearch-edgengram-highlight-term-vector-bad-highlights)
> 
> Does someone know why when using position offsets, edge ngrams only  
> highlights the smallest matching token?
> 
> I would expect it to highlight the longest matching token, because with an  
> edge-ngrams with min size = 3, this gives me a highlight of 3 chars while  
> there should be another matching token of 5 or 6 chars (for exemple)
> 
> Thanks

--

---

<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:** [July 6, 2017, 2:57am UTC](https://discuss.elastic.co/t/edge-ngram-gives-bad-highlight-when-using-position-offsets/10210/5 "2017-07-06T02:57:50Z")

</div>


