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


(jjsuway) #1

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


(Mark Harwood) #2

You can't with a regular "flat" mapping of your data.
Depending on your data you might want to look at [nested docs] (https://www.elastic.co/guide/en/elasticsearch/reference/5.2/nested.html) mappings - you can then use inner hits to retrieve and highlight the individual nested docs that match your search criteria.


(jjsuway) #4

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


(system) #5

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