# Highlighting Using Nest

**URL:** <https://discuss.elastic.co/t/highlighting-using-nest/208180>\
**Category:** Elasticsearch\
**Created:** [November 16, 2019, 6:13pm UTC](https://discuss.elastic.co/t/highlighting-using-nest/208180 "2019-11-16T18:13:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![han1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/han1/32/20141_2.png) [@han1](https://discuss.elastic.co/u/han1)\
**Post date:** [November 16, 2019, 6:13pm UTC](https://discuss.elastic.co/t/highlighting-using-nest/208180/1 "2019-11-16T18:13:10Z")

</div>

We are trying to use the Highlighting feature , but we just want to update each document that has the relevant highlights. For example:

> var hits = searchResponse.Hits.Where(h =\> h.Highlight.Count \> 0).Select(p =\> { p.Source.Keywords= String.Join(",", p.Highlight.Values); return p; });

Here we are throwing all the highlighted keywords into a field called Keywords.

The problem is that the hits items are updated but when you come to get the actual documents. i.e. response.Documents, the documents do not have this update.

How can we achieve this.  
many thanks

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [November 24, 2019, 11:39pm UTC](https://discuss.elastic.co/t/highlighting-using-nest/208180/2 "2019-11-24T23:39:39Z")

</div>

> [@han1](#):
>
> var hits = searchResponse.Hits.Where(h =\> h.Highlight.Count \> 0).Select(p =\> { p.Source.Keywords= String.Join(",", p.Highlight.Values); return p; });

The mutated documents will be available on `hits`

```auto
var hits = searchResponse.Hits.Where(h => h.Highlight.Count > 0).Select(p => { p.Source.Keywords= String.Join(",", p.Highlight.Values); return p; });

foreach (var hit in hits)
{
	var document = hit.Source;
    // do something with document
}

```

---

<div class="post-metadata">

**Author:** ![han1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/han1/32/20141_2.png) [@han1](https://discuss.elastic.co/u/han1)\
**Post date:** [November 25, 2019, 2:17pm UTC](https://discuss.elastic.co/t/highlighting-using-nest/208180/3 "2019-11-25T14:17:52Z")

</div>

Hi  
Thanks for responding.

Am I correct that if we ask for a highlight of a word/phrase, if there are 20 occurrences, then all 20 is returned?

If so, is there any way to ask Elastic to return just one instance as we only need to know the existence of it as opposed to wanting to highlight every occurrence.

Many thanks in advance.

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [November 25, 2019, 11:52pm UTC](https://discuss.elastic.co/t/highlighting-using-nest/208180/4 "2019-11-25T23:52:59Z")

</div>

> [@han1](#):
>
> Am I correct that if we ask for a highlight of a word/phrase, if there are 20 occurrences, then all 20 is returned?

Highlighting will highlight the number of fragments specified by [`number_of_fragments`](https://www.elastic.co/guide/en/elasticsearch/reference/7.4/search-request-body.html#highlighting-settings) in the targeted fields. The default is `5`.

---

<div class="post-metadata">

**Author:** ![han1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/han1/32/20141_2.png) [@han1](https://discuss.elastic.co/u/han1)\
**Post date:** [November 26, 2019, 6:11pm UTC](https://discuss.elastic.co/t/highlighting-using-nest/208180/5 "2019-11-26T18:11:58Z")

</div>

As always, Many thanks.

---

<div class="post-metadata">

**Author:** ![han1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/han1/32/20141_2.png) [@han1](https://discuss.elastic.co/u/han1)\
**Post date:** [November 26, 2019, 9:32pm UTC](https://discuss.elastic.co/t/highlighting-using-nest/208180/6 "2019-11-26T21:32:33Z")

</div>

Sorry to bother you again but setting the number\_of\_fragments produces incorrect results.  
Imaging we are sending 3 keywords : keyword1, keyword2 and keyword3.

If there are 50 occurrences of all the keywords, then we should get back just 3items, keyword1, keyword2 and keyword3.

if there are 20 occurrences of keyword1 and one occurrence of keyword2, then we should get back just 2 items keyword1 and keyword2.

If we limit the number of fragments, ES just brings back the first ones it finds without caring which keywords had in fact been matched.

Something like this, but I don't know if thee is a more efficient way of achieving this:

var hits = searchResponse.Hits.Where(h =\> h.Highlight.Count \> 0).Select(p =\> { p.Source.PostCode1 = String.Join(",", p.Highlight.Values.FirstOrDefault().Select(x=\>x.ToLower()).Distinct()); return p; });

and here is the query:

.Highlight(h =\> h  
.PreTags("")  
.PostTags("")  
.FragmentSize(1)  
//.NumberOfFragments(3)  
//.MaxFragmentLength(0)  
.Fields(  
fs =\> fs  
.Field(p =\> p.Summary)  
.HighlightQuery(q =\> q  
.Match(m =\> m  
//.Slop(2)  
.Field(p =\> p.Summary)  
.Query("keyword1 keyword2 keyword3")  
)  
)  
)  
)

Thank in advance.

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [November 27, 2019, 1:19am UTC](https://discuss.elastic.co/t/highlighting-using-nest/208180/7 "2019-11-27T01:19:36Z")

</div>

You can use `order` to exert some influence over the fragment output order, based on `score` for example (default order is the order in which they are found).

> [@han1](#):
>
> If there are 50 occurrences of all the keywords, then we should get back just 3items, keyword1, keyword2 and keyword3.
> 
> if there are 20 occurrences of keyword1 and one occurrence of keyword2, then we should get back just 2 items keyword1 and keyword2.

Highlighting doesn't return _distinct_ highlighted terms in this manner. Take a look at [how highlighters work internally](https://www.elastic.co/guide/en/elasticsearch/reference/7.4/search-request-body.html#highlighter-internal-work).

---

<div class="post-metadata">

**Author:** ![han1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/han1/32/20141_2.png) [@han1](https://discuss.elastic.co/u/han1)\
**Post date:** [November 27, 2019, 5:15am UTC](https://discuss.elastic.co/t/highlighting-using-nest/208180/8 "2019-11-27T05:15:37Z")

</div>

Thanks. Need to come up with another strategy to get this functionality.

---

<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:** [December 25, 2019, 5:15am UTC](https://discuss.elastic.co/t/highlighting-using-nest/208180/9 "2019-12-25T05:15:39Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
