# Elasticsearch highlighting

**URL:** <https://discuss.elastic.co/t/elasticsearch-highlighting/29773>\
**Category:** Elasticsearch\
**Created:** [September 22, 2015, 10:31am UTC](https://discuss.elastic.co/t/elasticsearch-highlighting/29773 "2015-09-22T10:31:37Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![evvo](https://avatars.discourse-cdn.com/v4/letter/e/dfb087/32.png) [@evvo](https://discuss.elastic.co/u/evvo)\
**Post date:** [September 22, 2015, 10:31am UTC](https://discuss.elastic.co/t/elasticsearch-highlighting/29773/1 "2015-09-22T10:31:37Z")

</div>

im using elasticsearch within a C# project using the NEST API. i want to show the searched terms as highlights in the results page but dont know how i handle the display of them. All the docs tell me how to set up the highlight feature in the search query but i cannot find any help on how i make these highlights appear on the page of results...

my query is as follows

result = client.Search(s =\> s  
.Query(a =\>

```
        a.Match(m => m.OnField("_all").Query(m_strSearchQuery))

        .From(intFrom)
        .Size(intSize)
        .Highlight(h => h
        .PreTags("<b style='color:orange'>")
        .PostTags("</b>")
        .OnFields(f => f
        .OnField(e => e.Title)
        .OnField(e => e.Content)
        )
        )
        );

```

Then i set my results to a variable that is the data for my repeater

var documents = result.Hits.Select(h =\> h.Source);

this.rptSearch.DataSource = documents;  
this.rptSearch.DataBind();  
this.rptSearch.Visible = true;  
i dont see any of the terms highlighted in my results nor do i see the terms wrapped in the highlight tags...

i did notice on debug my results have a hits and a highlights array though the highlights value doesnt seem to show the complete text returned as i see in the hits.source...

anyone help on this?

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [September 22, 2015, 1:19pm UTC](https://discuss.elastic.co/t/elasticsearch-highlighting/29773/2 "2015-09-22T13:19:55Z")

</div>

> [@evvo](#):
>
> anyone help on this?

I don't know nest but I know highlighting.

First thing's first you should wrap multiline code in fenced blocks like:

````auto
```C#
result = client.Search(s => s.Query(a =>
        a.Match(m => m.OnField("_all").Query(m_strSearchQuery))

````

```auto

That'll preserve the spaces.

Secondly I think [this link](http://nest.azurewebsites.net/nest/search/highlighting.html) has what you need.
```

---

<div class="post-metadata">

**Author:** ![evvo](https://avatars.discourse-cdn.com/v4/letter/e/dfb087/32.png) [@evvo](https://discuss.elastic.co/u/evvo)\
**Post date:** [September 22, 2015, 1:35pm UTC](https://discuss.elastic.co/t/elasticsearch-highlighting/29773/3 "2015-09-22T13:35:02Z")

</div>

thanks

ive seen that link and have that .highlight in the search query for the fields i want to use...

thats all done, what i cant see is how i get the highlighted words appearing in the text... they are not wrapped in the highlight tags...

the hits.source has the original text but the .highlights element has the highlighted snippet so i presume i need to do some sort of replace on the source text from the highlight text?

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [September 22, 2015, 1:50pm UTC](https://discuss.elastic.co/t/elasticsearch-highlighting/29773/4 "2015-09-22T13:50:31Z")

</div>

> [@evvo](#):
>
> the hits.source has the original text but the .highlights element has the highlighted snippet so i presume i need to do some sort of replace on the source text from the highlight text?

Highlighting's second job is snipping the text to the most "interesting" parts. If you just want the whole text back with the highlights there then set `fragment_size` to something large - 10k or something.

---

<div class="post-metadata">

**Author:** ![evvo](https://avatars.discourse-cdn.com/v4/letter/e/dfb087/32.png) [@evvo](https://discuss.elastic.co/u/evvo)\
**Post date:** [September 22, 2015, 1:50pm UTC](https://discuss.elastic.co/t/elasticsearch-highlighting/29773/5 "2015-09-22T13:50:48Z")

</div>

Do i need to do something in my index / mapping to make highlighting work?

maybe thats what im not doing?

---

<div class="post-metadata">

**Author:** ![evvo](https://avatars.discourse-cdn.com/v4/letter/e/dfb087/32.png) [@evvo](https://discuss.elastic.co/u/evvo)\
**Post date:** [September 22, 2015, 1:56pm UTC](https://discuss.elastic.co/t/elasticsearch-highlighting/29773/6 "2015-09-22T13:56:38Z")

</div>

ok thanks i will try that but regardless how do i get the actual highlighted text in my search results page..

currently i use

var documents = result.Hits.Select(h =\> h.Source); (this doesnt have the highlight tags at all)

i tried using result.Highlights.ToString() but that didnt work...

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [September 22, 2015, 2:03pm UTC](https://discuss.elastic.co/t/elasticsearch-highlighting/29773/7 "2015-09-22T14:03:38Z")

</div>

> [@evvo](#):
>
> Do i need to do something in my index / mapping to make highlighting work?

Highlighting works fine without making any changes to the index but you can make changes to the index to make highlighting faster - its all on the highlightings [docs](https://www.elastic.co/guide/en/elasticsearch/reference/1.7/search-request-highlighting.html).

> [@evvo](#):
>
> i tried using result.Highlights.ToString() but that didnt work...

That is the trouble - I don't know the client so I can't explain it. If you go from the client to curl I could be more helpful.

---

<div class="post-metadata">

**Author:** ![evvo](https://avatars.discourse-cdn.com/v4/letter/e/dfb087/32.png) [@evvo](https://discuss.elastic.co/u/evvo)\
**Post date:** [September 22, 2015, 2:10pm UTC](https://discuss.elastic.co/t/elasticsearch-highlighting/29773/8 "2015-09-22T14:10:13Z")

</div>

ok no problem

looks like ill have to leave it out for now...

its just that previously they used solr and it supported highlighted terms and its a requirement for elasticsearch now which has replaced it

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [September 22, 2015, 8:14pm UTC](https://discuss.elastic.co/t/elasticsearch-highlighting/29773/9 "2015-09-22T20:14:53Z")

</div>

The default for the highlighter is to highlight fields using only terms from the query that were explicitly targeted at those fields (so a search for forename:mark won't highlight any "mark" tokens found in a surname field).

However, when searching the \_all field this is precisely what you want to do - search one (indexed) field but highlight another (stored) field. Try set require\_field\_match to false.  
See [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html#field-match](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html#field-match)

---

<div class="post-metadata">

**Author:** ![evvo](https://avatars.discourse-cdn.com/v4/letter/e/dfb087/32.png) [@evvo](https://discuss.elastic.co/u/evvo)\
**Post date:** [September 23, 2015, 9:23am UTC](https://discuss.elastic.co/t/elasticsearch-highlighting/29773/10 "2015-09-23T09:23:41Z")

</div>

i can see the highlights when i step through with debug... these are set against just one field in my index...

im currently getting the results into my page by loading them from the source which is getting both the title and body fields returned from the search

var documents = result.Hits.Select(h =\> h.Source);

but if there are highlights for the hit i want the body field to be the highlight value. i can grab the highlight value but am unable to overwrite the body content or know how to populate the body field with the highlight and keep the other fields as they were....

---

<div class="post-metadata">

**Author:** ![evvo](https://avatars.discourse-cdn.com/v4/letter/e/dfb087/32.png) [@evvo](https://discuss.elastic.co/u/evvo)\
**Post date:** [September 23, 2015, 9:49am UTC](https://discuss.elastic.co/t/elasticsearch-highlighting/29773/11 "2015-09-23T09:49:55Z")

</div>

im trying to do this when i loop through each hit of the results, but the field value is never replaced it just stays as it was before the replace...

if (!hit.Highlights.Any()) continue;

```
            else
            {                    
                foreach (KeyValuePair<string, Highlight> item in hit.Highlights)
                {
                    hit.Source.ContentBody.Replace(hit.Source.ContentBody, item.Value.Highlights.FirstOrDefault());
                }
            }
```

---

<div class="post-metadata">

**Author:** ![Martijn\_Laarman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/martijn_laarman/32/4410_2.png) [@Martijn\_Laarman](https://discuss.elastic.co/u/Martijn_Laarman)\
**Post date:** [September 23, 2015, 11:29am UTC](https://discuss.elastic.co/t/elasticsearch-highlighting/29773/12 "2015-09-23T11:29:30Z")

</div>

`Replace` on string returns a new string with the replacement result, it does not mutate the original string try:

```auto
hit.Source.ContentBody = hit.Source.ContentBody
    .Replace(hit.Source.ContentBody, item.Value.Highlights.FirstOrDefault());

```

However it looks like you just want the first highlight result as the `ContentBody` inside your `Source` document so you can simply call:

```auto
hit.Source.ContentBody = item.Value.Highlights.FirstOrDefault();

```

---

<div class="post-metadata">

**Author:** ![evvo](https://avatars.discourse-cdn.com/v4/letter/e/dfb087/32.png) [@evvo](https://discuss.elastic.co/u/evvo)\
**Post date:** [September 24, 2015, 8:21am UTC](https://discuss.elastic.co/t/elasticsearch-highlighting/29773/13 "2015-09-24T08:21:39Z")

</div>

hi

thanks i get this working now to the extent that the contentbody has the new highlight snippet at this point and i see it in the source fields when the documents variable is assigned.

however i do not see the highlights in the page and view source reveals the pre and post tags are not there so need to find out why.

maybe they are lost when bound to the repeater?

---

<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 5, 2017, 11:48pm UTC](https://discuss.elastic.co/t/elasticsearch-highlighting/29773/14 "2017-07-05T23:48:19Z")

</div>


