# Elasticsearch NEST Highlight

**URL:** <https://discuss.elastic.co/t/elasticsearch-nest-highlight/117158>\
**Category:** Elasticsearch\
**Created:** [January 26, 2018, 7:01am UTC](https://discuss.elastic.co/t/elasticsearch-nest-highlight/117158 "2018-01-26T07:01:05Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ramon\_Zaro](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@Ramon\_Zaro](https://discuss.elastic.co/u/Ramon_Zaro)\
**Post date:** [January 26, 2018, 7:01am UTC](https://discuss.elastic.co/t/elasticsearch-nest-highlight/117158/1 "2018-01-26T07:01:05Z")

</div>

Hi all,  
I am running an ES 5.5.0 implementation and use the NEST api in a custom .NET app.  
I am able to get correct highlight results while using exact word search. But after activating the full lucene query the highlight function returns no results.

Simply put, how do I get back the correct highlight response to lucene queries (using AND/OR, field : value combinations etc) using NEST ?? which function do I use ?

thanks in advance.

---

<div class="post-metadata">

**Author:** ![Ramon\_Zaro](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@Ramon\_Zaro](https://discuss.elastic.co/u/Ramon_Zaro)\
**Post date:** [January 28, 2018, 4:45pm UTC](https://discuss.elastic.co/t/elasticsearch-nest-highlight/117158/2 "2018-01-28T16:45:02Z")

</div>

help, assistance please ?

anyone ?

---

<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:** [January 29, 2018, 7:11am UTC](https://discuss.elastic.co/t/elasticsearch-nest-highlight/117158/3 "2018-01-29T07:11:07Z")

</div>

Can you provide a minimal, reproducible example of what you're running, what the results are, and what you expect to see?

---

<div class="post-metadata">

**Author:** ![Ramon\_Zaro](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@Ramon\_Zaro](https://discuss.elastic.co/u/Ramon_Zaro)\
**Post date:** [January 30, 2018, 6:41pm UTC](https://discuss.elastic.co/t/elasticsearch-nest-highlight/117158/4 "2018-01-30T18:41:13Z")

</div>

Hello Russ,  
the part I am having a problem with is something like this :

```
var response = elasticclient.search < >
.From(Int32.Parse(elast_fromind.tostring()))
.Size(Int32.Parse(elast_count.tostring()))
.Type("doc")
.Query(q=> q
	.Querystring(qs=>qs
		.Query(txt2search)))
.Highlight(h=>h
	.Pretags("<tag>")
	.Posttags("</tag>")
	.Fields(fs=> fs
		.Field(f=>f.content)
		.Numberoffragments(1)
		.Fragmentsize(200)))
		);

```

I am using an index created by fscrawler. Now, if I pass a search string, say "boat AND race AND file.extension : pdf" "boat NOT race" the highlight function doesn't return anything.  
The search results work fine though.  
Ideally, I would like it to behave like it does in kibana, highlight terms like boat & race in the first case, only boat in the second case while ignoring AND, file.extension and such terms.

---

<div class="post-metadata">

**Author:** ![Ramon\_Zaro](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@Ramon\_Zaro](https://discuss.elastic.co/u/Ramon_Zaro)\
**Post date:** [January 31, 2018, 2:04pm UTC](https://discuss.elastic.co/t/elasticsearch-nest-highlight/117158/5 "2018-01-31T14:04:21Z")

</div>

Hi, @forloop  
If it helps, here's a search that does return highlight hits :

```
.From(Int32.Parse(elast_fromind.tostring()))
.Size(Int32.Parse(elast_count.tostring()))
.Type("doc")
.Query(q=> q
	.Match (m=> m
		.Field(f => f.content)
		.Query (txt2search)))
.Highlight(h=>h
	.Pretags("<tag>")
	.Posttags("</tag>")
	.Fields(fs=> fs
		.Field(f=>f.content)
		.Numberoffragments(1)
		.Fragmentsize(200)
	)));

```

IOW, the highlight option works if I search inside a specific field, but if I am using lucene query any field in the index might be called. and in that case the highlight function doesn't return any hits.

---

<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:** [February 2, 2018, 12:42am UTC](https://discuss.elastic.co/t/elasticsearch-nest-highlight/117158/6 "2018-02-02T00:42:41Z")

</div>

I think you may need to include the [`highlight_query` in the highlighting](https://www.elastic.co/guide/en/elasticsearch/reference/5.5/search-request-highlighting.html#_highlight_query); the `highlight_query` in this case would simply be the same as the search query.

When using the `query_string` query in 5.5 without specifying any fields, you'll be running the query against the `_all` field, but higlighting is configured to run against the `"content"` field, so you need to indicate what query to run against this field for highlighting.

---

<div class="post-metadata">

**Author:** ![Ramon\_Zaro](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@Ramon\_Zaro](https://discuss.elastic.co/u/Ramon_Zaro)\
**Post date:** [February 8, 2018, 5:39am UTC](https://discuss.elastic.co/t/elasticsearch-nest-highlight/117158/7 "2018-02-08T05:39:06Z")

</div>

Hi @forloop, I tried this but didn't getdesired result.  
might have misunderstood what you meant.

could you provide me a sample few lines of what you mean by include highlight\_query in highlighting ?  
thanks & regards.

---

<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:** [February 9, 2018, 1:25am UTC](https://discuss.elastic.co/t/elasticsearch-nest-highlight/117158/8 "2018-02-09T01:25:01Z")

</div>

It would look something like the following (adapt to your data):

```auto
var response = client.Search<Question>(s => s
	.From(0)
	.Size(20)
	.Type("doc")
	.Query(q => q
		.QueryString(qs => qs
			.Query("text to search")
		)
	)
	.Highlight(h => h
		.PreTags("<tag>")
		.PostTags("</tag>")
		.Fields(fs => fs
			.Field(f => f.Body)
			.HighlightQuery(hq => hq
			    // repeat the search query here
				.QueryString(qs => qs
					.Query("text to search")
				)
			)
		)
		.NumberOfFragments(1)
		.FragmentSize(200)
	)
);

```

Note that it's possible that this does not highlight anything, if the documents that match the search query don't match for the highlight query in the target field i.e. `"text to search`" might be a match for a document in the `"_all"` field with a value that has originated from the `"Title"` field but not the `"Body"` field, so a highlight on `"Body"` field won't have anything to highlight.

---

<div class="post-metadata">

**Author:** ![Ramon\_Zaro](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@Ramon\_Zaro](https://discuss.elastic.co/u/Ramon_Zaro)\
**Post date:** [February 25, 2018, 5:02am UTC](https://discuss.elastic.co/t/elasticsearch-nest-highlight/117158/9 "2018-02-25T05:02:26Z")

</div>

thanks. I think I am having the exact problem you warned of. How does kibana handle this ?  
Because, as far as I can understand this feature works fine there, IOW it is able to highlight successfully without needing the field to be declared explicitly in the query.

---

<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:** [February 26, 2018, 9:07pm UTC](https://discuss.elastic.co/t/elasticsearch-nest-highlight/117158/10 "2018-02-26T21:07:12Z")

</div>

> [@Ramon\_Zaro](#):
>
> How does kibana handle this ?

Take a look at the request and response sent by Kibana for the given query:

 ![52](https://us1.discourse-cdn.com/elastic/original/3X/4/2/423418ce80e8702bbd30fba7ede355f4ec989311.png)

Kibana sends a highlight against `*` fields

 ![11](https://us1.discourse-cdn.com/elastic/original/3X/3/b/3b72473034171759afe585cab11157a53b1a97a1.png)

which you could also send in the client

```auto
var response = client.Search<Question>(s => s
	.From(0)
	.Size(20)
	.Type("doc")
	.Query(q => q
		.QueryString(qs => qs
			.Query("text to search")
		)
	)
	.Highlight(h => h
		.PreTags("<tag>")
		.PostTags("</tag>")
		.Fields(fs => fs
			.Field("*")
		)
	)
);

```

---

<div class="post-metadata">

**Author:** ![Ramon\_Zaro](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@Ramon\_Zaro](https://discuss.elastic.co/u/Ramon_Zaro)\
**Post date:** [March 15, 2018, 2:50am UTC](https://discuss.elastic.co/t/elasticsearch-nest-highlight/117158/11 "2018-03-15T02:50:32Z")

</div>

thanks a lot, that's really useful !

---

<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:** [April 12, 2018, 2:50am UTC](https://discuss.elastic.co/t/elasticsearch-nest-highlight/117158/12 "2018-04-12T02:50:56Z")

</div>

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