# Nested query and highlight fields

**URL:** https://discuss.elastic.co/t/nested-query-and-highlight-fields/4949
**Category:** Elasticsearch
**Created:** [July 25, 2011, 9:19pm UTC](https://discuss.elastic.co/t/nested-query-and-highlight-fields/4949 "2011-07-25T21:19:24Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Ellery\_Crane\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ellery_crane_2/32/3369_2.png) [@Ellery\_Crane\_2](https://discuss.elastic.co/u/Ellery_Crane_2)
#### Post date: [July 25, 2011, 9:19pm UTC](https://discuss.elastic.co/t/nested-query-and-highlight-fields/4949/1 "2011-07-25T21:19:24Z")

</div>

I've got my nested query working as intended, but no highlight  
fragments are generated on a hit. I assume that this is because each  
nested document is indexed separately from the root document, even if  
I have include\_in\_root and include\_in\_parent set to true. Is there any  
way to get highlight fragments to work in nested fields? Thanks!

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [July 26, 2011, 4:56am UTC](https://discuss.elastic.co/t/nested-query-and-highlight-fields/4949/2 "2011-07-26T04:56:02Z")

</div>

No, this is not supported currently (and requires some thinking on how to  
support it properly...).

On Tue, Jul 26, 2011 at 12:19 AM, Ellery Crane [seidaku@gmail.com](mailto:seidaku@gmail.com) wrote:

> I've got my nested query working as intended, but no highlight  
> fragments are generated on a hit. I assume that this is because each  
> nested document is indexed separately from the root document, even if  
> I have include\_in\_root and include\_in\_parent set to true. Is there any  
> way to get highlight fragments to work in nested fields? Thanks!

---

<div class="post-metadata">

### Author: ![Ellery\_Crane\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ellery_crane_2/32/3369_2.png) [@Ellery\_Crane\_2](https://discuss.elastic.co/u/Ellery_Crane_2)
#### Post date: [July 26, 2011, 1:32pm UTC](https://discuss.elastic.co/t/nested-query-and-highlight-fields/4949/3 "2011-07-26T13:32:51Z")

</div>

As a work around for this issue, I've created my nested query as a  
filter with a non-nested version of it as my query. I have all of my  
nested fields as include\_in\_root and include\_in\_parent, so using the  
non-nested version of the query gives me the highlight fragments I'm  
after. Unfortunately, this does not restrict the highlights based on  
the nested rules, so I need to process them manually in my application  
to remove any highlights for nested documents that don't match the  
query. This is sub-optimal, but it is working. Couldn't something like  
this be done on the ES side of things to generate the highlights for  
nested queries?

On Jul 26, 12:56 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> No, this is not supported currently (and requires some thinking on how to  
> support it properly...).
> 
> On Tue, Jul 26, 2011 at 12:19 AM, Ellery Crane [seid...@gmail.com](mailto:seid...@gmail.com) wrote:
> 
> > I've got my nested query working as intended, but no highlight  
> > fragments are generated on a hit. I assume that this is because each  
> > nested document is indexed separately from the root document, even if  
> > I have include\_in\_root and include\_in\_parent set to true. Is there any  
> > way to get highlight fragments to work in nested fields? Thanks!

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [July 26, 2011, 2:06pm UTC](https://discuss.elastic.co/t/nested-query-and-highlight-fields/4949/4 "2011-07-26T14:06:43Z")

</div>

As you say, the solution you came up with is problematic. In order to do  
highlighting based on the nested query, the nested documents needs to be  
extracted as well in order to highlight it, which is more problematic (and  
less performant).

On Tue, Jul 26, 2011 at 4:32 PM, Ellery Crane [seidaku@gmail.com](mailto:seidaku@gmail.com) wrote:

> As a work around for this issue, I've created my nested query as a  
> filter with a non-nested version of it as my query. I have all of my  
> nested fields as include\_in\_root and include\_in\_parent, so using the  
> non-nested version of the query gives me the highlight fragments I'm  
> after. Unfortunately, this does not restrict the highlights based on  
> the nested rules, so I need to process them manually in my application  
> to remove any highlights for nested documents that don't match the  
> query. This is sub-optimal, but it is working. Couldn't something like  
> this be done on the ES side of things to generate the highlights for  
> nested queries?
> 
> On Jul 26, 12:56 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > No, this is not supported currently (and requires some thinking on how to  
> > support it properly...).
> > 
> > On Tue, Jul 26, 2011 at 12:19 AM, Ellery Crane [seid...@gmail.com](mailto:seid...@gmail.com)  
> > wrote:
> > 
> > > I've got my nested query working as intended, but no highlight  
> > > fragments are generated on a hit. I assume that this is because each  
> > > nested document is indexed separately from the root document, even if  
> > > I have include\_in\_root and include\_in\_parent set to true. Is there any  
> > > way to get highlight fragments to work in nested fields? Thanks!

---

<div class="post-metadata">

### Author: ![Ellery\_Crane\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ellery_crane_2/32/3369_2.png) [@Ellery\_Crane\_2](https://discuss.elastic.co/u/Ellery_Crane_2)
#### Post date: [July 26, 2011, 3:26pm UTC](https://discuss.elastic.co/t/nested-query-and-highlight-fields/4949/5 "2011-07-26T15:26:46Z")

</div>

Is there no way to pass on the highlight fields of the main query to  
the nested query? The fields are the same, so presumably they would  
need no enhancement? Then you could aggregate the highlight field  
results of the nested queries with those of the outer query. Or is  
this not at all how the nested queries are being performed? I confess  
to be being unaware of the details. The documentation and behavior  
I've observed suggests that it's joining the results of individual  
searches against the nested documents with that of the root document;  
I have no idea how possible passing along highlight fields is in that  
context.

On Jul 26, 10:06 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> As you say, the solution you came up with is problematic. In order to do  
> highlighting based on the nested query, the nested documents needs to be  
> extracted as well in order to highlight it, which is more problematic (and  
> less performant).
> 
> On Tue, Jul 26, 2011 at 4:32 PM, Ellery Crane [seid...@gmail.com](mailto:seid...@gmail.com) wrote:
> 
> > As a work around for this issue, I've created my nested query as a  
> > filter with a non-nested version of it as my query. I have all of my  
> > nested fields as include\_in\_root and include\_in\_parent, so using the  
> > non-nested version of the query gives me the highlight fragments I'm  
> > after. Unfortunately, this does not restrict the highlights based on  
> > the nested rules, so I need to process them manually in my application  
> > to remove any highlights for nested documents that don't match the  
> > query. This is sub-optimal, but it is working. Couldn't something like  
> > this be done on the ES side of things to generate the highlights for  
> > nested queries?
> 
> > On Jul 26, 12:56 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> > 
> > > No, this is not supported currently (and requires some thinking on how to  
> > > support it properly...).
> 
> > > On Tue, Jul 26, 2011 at 12:19 AM, Ellery Crane [seid...@gmail.com](mailto:seid...@gmail.com)  
> > > wrote:
> > > 
> > > > I've got my nested query working as intended, but no highlight  
> > > > fragments are generated on a hit. I assume that this is because each  
> > > > nested document is indexed separately from the root document, even if  
> > > > I have include\_in\_root and include\_in\_parent set to true. Is there any  
> > > > way to get highlight fragments to work in nested fields? Thanks!

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [July 27, 2011, 4:45am UTC](https://discuss.elastic.co/t/nested-query-and-highlight-fields/4949/6 "2011-07-27T04:45:53Z")

</div>

Thats not how highlighting works...

On Tue, Jul 26, 2011 at 6:26 PM, Ellery Crane [seidaku@gmail.com](mailto:seidaku@gmail.com) wrote:

> Is there no way to pass on the highlight fields of the main query to  
> the nested query? The fields are the same, so presumably they would  
> need no enhancement? Then you could aggregate the highlight field  
> results of the nested queries with those of the outer query. Or is  
> this not at all how the nested queries are being performed? I confess  
> to be being unaware of the details. The documentation and behavior  
> I've observed suggests that it's joining the results of individual  
> searches against the nested documents with that of the root document;  
> I have no idea how possible passing along highlight fields is in that  
> context.
> 
> On Jul 26, 10:06 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > As you say, the solution you came up with is problematic. In order to do  
> > highlighting based on the nested query, the nested documents needs to be  
> > extracted as well in order to highlight it, which is more problematic  
> > (and  
> > less performant).
> > 
> > On Tue, Jul 26, 2011 at 4:32 PM, Ellery Crane [seid...@gmail.com](mailto:seid...@gmail.com) wrote:
> > 
> > > As a work around for this issue, I've created my nested query as a  
> > > filter with a non-nested version of it as my query. I have all of my  
> > > nested fields as include\_in\_root and include\_in\_parent, so using the  
> > > non-nested version of the query gives me the highlight fragments I'm  
> > > after. Unfortunately, this does not restrict the highlights based on  
> > > the nested rules, so I need to process them manually in my application  
> > > to remove any highlights for nested documents that don't match the  
> > > query. This is sub-optimal, but it is working. Couldn't something like  
> > > this be done on the ES side of things to generate the highlights for  
> > > nested queries?
> > 
> > > On Jul 26, 12:56 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> > > 
> > > > No, this is not supported currently (and requires some thinking on  
> > > > how to  
> > > > support it properly...).
> > 
> > > > On Tue, Jul 26, 2011 at 12:19 AM, Ellery Crane [seid...@gmail.com](mailto:seid...@gmail.com)  
> > > > wrote:
> > > > 
> > > > > I've got my nested query working as intended, but no highlight  
> > > > > fragments are generated on a hit. I assume that this is because  
> > > > > each  
> > > > > nested document is indexed separately from the root document, even  
> > > > > if  
> > > > > I have include\_in\_root and include\_in\_parent set to true. Is there  
> > > > > any  
> > > > > way to get highlight fragments to work in nested fields? 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, 3:59am UTC](https://discuss.elastic.co/t/nested-query-and-highlight-fields/4949/7 "2017-07-06T03:59:07Z")

</div>


