# Which index(element) of an array are the ones that matched in highlight search results?

**URL:** <https://discuss.elastic.co/t/which-index-element-of-an-array-are-the-ones-that-matched-in-highlight-search-results/74019>\
**Category:** Elasticsearch\
**Created:** [February 6, 2017, 7:40am UTC](https://discuss.elastic.co/t/which-index-element-of-an-array-are-the-ones-that-matched-in-highlight-search-results/74019 "2017-02-06T07:40:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjsuway](https://avatars.discourse-cdn.com/v4/letter/j/9fc348/32.png) [@jjsuway](https://discuss.elastic.co/u/jjsuway)\
**Post date:** [February 6, 2017, 7:40am UTC](https://discuss.elastic.co/t/which-index-element-of-an-array-are-the-ones-that-matched-in-highlight-search-results/74019/1 "2017-02-06T07:40:08Z")

</div>

Which index(element) of an array are the ones that matched in highlight search results ?

I'm using the elasticsearch.js of the elasticsearch 5.1.1 .

The data item is as follows:  
{  
"data" :  
[{  
"grade" : 1,  
"comment" : " This is test program"  
},  
{  
"grade" : 2,  
"comment" : " This is test program"  
},  
{  
"grade" : 3,  
"comment" : " This is commercial program"  
}]  
}

The query is as follows :

* * *

"query": {  
"bool": {  
"must": [  
{  
"nested": {  
"query": {  
"query\_string": {  
"fields": ["data.comment"],  
"query": "commercial"  
}  
},  
"path": "data"  
}  
} ]  
} } ,  
"highlight": {  
"fields" : {  
"data.comment":{  
"number\_of\_fragments" : 0,  
},  
},  
"pre\_tags": [''],  
"post\_tags": [""]  
}

* * *

* * *

I received the result as follows :  
"\_source": {  
omitted..  
},  
"highlight": {  
"data.comment": [  
" This is \<span style="font-weight:bold;color:blue"\>commercial program"  
]  
}  
}

The result is the third index(element) of "data" . But I can't know the which index(element) of array in this results.  
I can only know the value of array.

How can I know the array position (index) ?

---

<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:** [February 6, 2017, 10:15am UTC](https://discuss.elastic.co/t/which-index-element-of-an-array-are-the-ones-that-matched-in-highlight-search-results/74019/2 "2017-02-06T10:15:32Z")

</div>

> [@jjsuway](#):
>
> How can I know the array position (index) ?

You can't with a regular "flat" mapping of your data.  
[Depending on your data](https://discuss.elastic.co/t/porting-a-relational-structure-to-elasticsearch-nested-or-parent-child/32920/2) you might want to look at [nested docs] ([Nested datatype | Elasticsearch Guide [5.2] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/5.2/nested.html)) mappings - you can then use [inner hits](https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-request-inner-hits.html#nested-inner-hits) to retrieve and highlight the individual nested docs that match your search criteria.

---

<div class="post-metadata">

**Author:** ![jjsuway](https://avatars.discourse-cdn.com/v4/letter/j/9fc348/32.png) [@jjsuway](https://discuss.elastic.co/u/jjsuway)\
**Post date:** [February 7, 2017, 4:51am UTC](https://discuss.elastic.co/t/which-index-element-of-an-array-are-the-ones-that-matched-in-highlight-search-results/74019/4 "2017-02-07T04:51:29Z")

</div>

Thanks for your help. It's very helpful to me.  
I appreciate your support very much.

---

<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:** [March 7, 2017, 4:51am UTC](https://discuss.elastic.co/t/which-index-element-of-an-array-are-the-ones-that-matched-in-highlight-search-results/74019/5 "2017-03-07T04:51:34Z")

</div>

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