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

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) ?

1 Like

You can't with a regular "flat" mapping of your data.
Depending on your data you might want to look at [nested docs] (Nested datatype | Elasticsearch Guide [5.2] | Elastic) mappings - you can then use inner hits to retrieve and highlight the individual nested docs that match your search criteria.

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

1 Like

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