# Highlight fragments in a collection fields- any way to see which item caused the match?

**URL:** <https://discuss.elastic.co/t/highlight-fragments-in-a-collection-fields-any-way-to-see-which-item-caused-the-match/4418>\
**Category:** Elasticsearch\
**Created:** [May 16, 2011, 9:33pm UTC](https://discuss.elastic.co/t/highlight-fragments-in-a-collection-fields-any-way-to-see-which-item-caused-the-match/4418 "2011-05-16T21:33:48Z")\
**Posts on this page:** 4\
**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:** [May 16, 2011, 9:33pm UTC](https://discuss.elastic.co/t/highlight-fragments-in-a-collection-fields-any-way-to-see-which-item-caused-the-match/4418/1 "2011-05-16T21:33:48Z")

</div>

If I have a document with a collection field, and I have highlight  
results on an item in such a collection, is there any way to identify  
which item the match was on?

Here's an example:

curl -XDELETE '[http://127.0.0.1:9200/someindex?pretty=1](http://127.0.0.1:9200/someindex?pretty=1)'  
curl -XPOST '[http://127.0.0.1:9200/someindex/user?pretty=1](http://127.0.0.1:9200/someindex/user?pretty=1)' -d '  
{  
"friends" : [  
{  
"name" : {  
"first" : "Jack",  
"last" : "Smith"  
},  
"email" : ["jack.smith@example.com",  
"j.smith@example.com", "jack@smith.com", "jack@jacksmith.com"]  
},  
{  
"name" : {  
"first" : "Alice",  
"last" : "Smith"  
},  
"email" : ["alice@example.com", "alice.smith@example.com"]  
},  
{  
"name" : {  
"first" : "Jack",  
"last" : "Brown"  
},  
"email" : ["jb@example.com", "jack.brown@example.com",  
"jack@example.com"]  
}  
]  
}  
'  
curl -XGET '[http://127.0.0.1:9200/someindex/user/\_search?pretty=1](http://127.0.0.1:9200/someindex/user/_search?pretty=1)' -d  
'  
{  
"query" : {  
"query\_string" : {  
"query" : "jack",  
"fields" : ["friends.name.first", "friends.name.last",  
"friends.email"]  
}  
},  
"highlight" : {  
"fields" : {  
"friends.name.first" : {},  
"friends.name.last" : {},  
"friends.email" : {}  
}  
}  
}  
'

Executing that search gets back a hit with the following highlight  
info:

"highlight" : {  
"friends.email" : [ "_jack_@smith.com", "_jack\</  
em\>@jacksmith.com", "jack@example.com" ],  
"friends.name.first" : ["Jack", "Jack"]  
}_

Is there some way to determine which friend object each hit came off  
of? I'm used to a Spring nested path syntax such as  
"friends[0].name.first" or "friends[2].email[1]" or the like, but as  
far as I can tell there is nothing similar in ES. On a hit on  
something like "friends.name.first", I'd like to be able to provide  
the broader context (both first and last name of the friend that  
matched), but without knowing which friend object had the match, it is  
difficult to put that together.

---

<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:** [May 17, 2011, 6:17pm UTC](https://discuss.elastic.co/t/highlight-fragments-in-a-collection-fields-any-way-to-see-which-item-caused-the-match/4418/2 "2011-05-17T18:17:04Z")

</div>

No, there isn't...  
On Tuesday, May 17, 2011 at 12:33 AM, Ellery Crane wrote:

> If I have a document with a collection field, and I have highlight  
> results on an item in such a collection, is there any way to identify  
> which item the match was on?
> 
> Here's an example:
> 
> curl -XDELETE '[http://127.0.0.1:9200/someindex?pretty=1](http://127.0.0.1:9200/someindex?pretty=1)'  
> curl -XPOST '[http://127.0.0.1:9200/someindex/user?pretty=1](http://127.0.0.1:9200/someindex/user?pretty=1)' -d '  
> {  
> "friends" : [  
> {  
> "name" : {  
> "first" : "Jack",  
> "last" : "Smith"  
> },  
> "email" : ["[jack.smith@example.com](mailto:jack.smith@example.com)",  
> "[j.smith@example.com](mailto:j.smith@example.com)", "[jack@smith.com](mailto:jack@smith.com)", "[jack@jacksmith.com](mailto:jack@jacksmith.com)"]  
> },  
> {  
> "name" : {  
> "first" : "Alice",  
> "last" : "Smith"  
> },  
> "email" : ["[alice@example.com](mailto:alice@example.com)", "[alice.smith@example.com](mailto:alice.smith@example.com)"]  
> },  
> {  
> "name" : {  
> "first" : "Jack",  
> "last" : "Brown"  
> },  
> "email" : ["[jb@example.com](mailto:jb@example.com)", "[jack.brown@example.com](mailto:jack.brown@example.com)",  
> "[jack@example.com](mailto:jack@example.com)"]  
> }  
> ]  
> }  
> '  
> curl -XGET '[http://127.0.0.1:9200/someindex/user/\_search?pretty=1](http://127.0.0.1:9200/someindex/user/_search?pretty=1)' -d  
> '  
> {  
> "query" : {  
> "query\_string" : {  
> "query" : "jack",  
> "fields" : ["friends.name.first", "friends.name.last",  
> "friends.email"]  
> }  
> },  
> "highlight" : {  
> "fields" : {  
> "friends.name.first" : {},  
> "friends.name.last" : {},  
> "friends.email" : {}  
> }  
> }  
> }  
> '
> 
> Executing that search gets back a hit with the following highlight  
> info:
> 
> "highlight" : {  
> "friends.email" : [ "_jack_@smith.com", "_jack\</  
> em\>@jacksmith.com", "jack@example.com" ],  
> "friends.name.first" : ["Jack", "Jack"]  
> }_
> 
>  
> 
> _Is there some way to determine which friend object each hit came off  
> of? I'm used to a Spring nested path syntax such as  
> "friends[0].name.first" or "friends[2].email[1]" or the like, but as  
> far as I can tell there is nothing similar in ES. On a hit on  
> something like "friends.name.first", I'd like to be able to provide  
> the broader context (both first and last name of the friend that  
> matched), but without knowing which friend object had the match, it is  
> difficult to put that together._

---

<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:** [May 18, 2011, 4:01pm UTC](https://discuss.elastic.co/t/highlight-fragments-in-a-collection-fields-any-way-to-see-which-item-caused-the-match/4418/3 "2011-05-18T16:01:48Z")

</div>

On May 17, 2:17 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> No, there isn't...

Hmm. This would certainly be a nice feature to have 🙂

In the meanwhile, I am considering denormalizing the nested fields  
into 'aggregate' fields, that contain all of the context I need, and  
searching on those. For instance, when indexing a user, denormalize  
the friend names into "friendNames", each element of which would be a  
string consisting of "{friend.name.first} {friend.name.last}". The  
only problem here is that it pollutes my \_source document with data  
I'm only using to get contextual highlighting.. Does ES have any way  
to set up such 'aggregate' fields automatically, perhaps as part of a  
mapping definition? Or, alternatively, am I overlooking a better way  
to achieve my goal (showing a highlight context that spans multiple  
fields within a nested collection property)?

---

<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, 4:05am UTC](https://discuss.elastic.co/t/highlight-fragments-in-a-collection-fields-any-way-to-see-which-item-caused-the-match/4418/4 "2017-07-06T04:05:51Z")

</div>


