# Highlighting substrings in matched terms

**URL:** <https://discuss.elastic.co/t/highlighting-substrings-in-matched-terms/5565>\
**Category:** Elasticsearch\
**Created:** [October 11, 2011, 7:42pm UTC](https://discuss.elastic.co/t/highlighting-substrings-in-matched-terms/5565 "2011-10-11T19:42:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_Grundler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robert_grundler/32/3068_2.png) [@Robert\_Grundler](https://discuss.elastic.co/u/Robert_Grundler)\
**Post date:** [October 11, 2011, 7:42pm UTC](https://discuss.elastic.co/t/highlighting-substrings-in-matched-terms/5565/1 "2011-10-11T19:42:13Z")

</div>

Hi,

i'm trying to highlight my search results, and i'm not sure if i understand  
the highlighting mechanism correctly. My document has 2 fields: "firstname"  
and "lastname".

When searching for the term "jack", the document {firstname: "John",  
lastname: "Jackson"} will be highlighted as {lastname: "_Jackson_"}.  
I'd expect the highlighting  
to be "_Jackson_" though.

Is the highlighting i'm getting the correct result, or am i missing  
something in my query?

regards

-robert

---

<div class="post-metadata">

**Author:** ![Lukas\_Vlcek1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas_vlcek1/32/819_2.png) [@Lukas\_Vlcek1](https://discuss.elastic.co/u/Lukas_Vlcek1)\
**Post date:** [October 11, 2011, 8:15pm UTC](https://discuss.elastic.co/t/highlighting-substrings-in-matched-terms/5565/2 "2011-10-11T20:15:58Z")

</div>

Hi,

Imporatnat is the text analysis step in this case. Can this be because you  
are using analyzer that collapses Jackson to the jack term?

Regars,  
Lukas

On Tuesday, October 11, 2011, Robert Gründler [r.gruendler@gmail.com](mailto:r.gruendler@gmail.com) wrote:

> Hi,
> 
> i'm trying to highlight my search results, and i'm not sure if i  
> understand the highlighting mechanism correctly. My document has 2 fields:  
> "firstname" and "lastname".
> 
> When searching for the term "jack", the document {firstname: "John",  
> lastname: "Jackson"} will be highlighted as {lastname: "_Jackson_"}.  
> I'd expect the highlighting  
> to be "_Jack_son" though.
> 
> Is the highlighting i'm getting the correct result, or am i missing  
> something in my query?
> 
> regards
> 
> -robert

---

<div class="post-metadata">

**Author:** ![Robert\_Grundler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robert_grundler/32/3068_2.png) [@Robert\_Grundler](https://discuss.elastic.co/u/Robert_Grundler)\
**Post date:** [October 11, 2011, 8:39pm UTC](https://discuss.elastic.co/t/highlighting-substrings-in-matched-terms/5565/3 "2011-10-11T20:39:51Z")

</div>

> Imporatnat is the text analysis step in this case. Can this be because  
> you are using analyzer that collapses Jackson to the jack term?

I did not configure any specific analyzers, so i guess the defaults are  
being used. The highlighting i described does happen the same way with  
any other term too, so i guess it's not due to collapsing/stemming of  
the input being indexed.

Could it be that i need to use a EdgeNGram Tokenizer for this kind of  
highlighting during indexing phase?  
([Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/index-modules/analysis/edgengram-tokenizer.html))

regards

-robert

> Regars,  
> Lukas
> 
> On Tuesday, October 11, 2011, Robert GrÃ¼ndler \<[r.gruendler@gmail.com](mailto:r.gruendler@gmail.com)  
> [mailto:r.gruendler@gmail.com](mailto:r.gruendler@gmail.com)\> wrote:
> 
> > Hi,
> > 
> > i'm trying to highlight my search results, and i'm not sure if i  
> > understand the highlighting mechanism correctly. My document has 2  
> > fields: "firstname" and "lastname".
> > 
> > When searching for the term "jack", the document {firstname: "John",  
> > lastname: "Jackson"} will be highlighted as {lastname:  
> > "_Jackson_"}. I'd expect the highlighting  
> > to be "_Jack_son" though.
> > 
> > Is the highlighting i'm getting the correct result, or am i missing  
> > something in my query?
> > 
> > regards
> > 
> > -robert

---

<div class="post-metadata">

**Author:** ![Robert\_Grundler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robert_grundler/32/3068_2.png) [@Robert\_Grundler](https://discuss.elastic.co/u/Robert_Grundler)\
**Post date:** [October 11, 2011, 9:07pm UTC](https://discuss.elastic.co/t/highlighting-substrings-in-matched-terms/5565/4 "2011-10-11T21:07:35Z")

</div>

using a nGram filter during analysis solved the highlighting problem:

```
    settings = { 'index': {
        'analysis' : {
            'analyzer' : {                              
                'typeahead_analyzer' : {                    
                    'tokenizer' : 'lowercase',                        
                    'filter' : ['stop', 'ta_ngram'],
                    'type' : 'custom'
                }                              
            },
            'filter' : {
                'ta_ngram' : {                                  
                    'type' : 'nGram',
                    'max_gram' : 30,
                    'min_gram' : 2                                  
                }                            
            }
        }
    }}
```

---

<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, 3:52am UTC](https://discuss.elastic.co/t/highlighting-substrings-in-matched-terms/5565/5 "2017-07-06T03:52:20Z")

</div>


