Optimized inner hits

Hello,

I would like to know, if there is a possibility to retrieve the inner hits in an optimized way?

Consider following example:

PUT my_index
{
"mappings": {
"my_parent": {},
"my_child": {
"_parent": {
"type": "my_parent"
}
}
}
}

PUT my_index/my_parent/1
{
"text": "This is a parent document"
}

PUT my_index/my_child/2?parent=1
{
"text": "This is a child document"
}

PUT my_index/my_child/3?parent=1&refresh=true
{
"text": "This is another child document"
}

PUT my_index/my_parent/11
{
"text": "This is a parent 11 document"
}

PUT my_index/my_child/12?parent=11
{
"text": "This is a child 12 document"
}

PUT my_index/my_child/13?parent=11&refresh=true
{
"text": "This is another child 13 document"
}

When I search for inner hits using following search, it will include the "parent" for each of its child. For smaller results this is not a problem, consider when we have several child documents and then for each child document the same parent document is copied in the results. Do we have any possibility to have a separate list of parents and the inner hits contain only its corresponding parent ID? This way much traffic can be saved.

POST my_index/my_child/_search
{
"query": {
"has_parent": {
"parent_type": "my_parent",
"query": {
"match": {
"text": "parent"
}
},
"inner_hits" : {
"name" : "abc"
}
}
}
}

Thanks,
Niaz

Please also mention, if there is another option to retrieve the parents of matched children efficiently? I mean, if somehow I can use the results of the first query to make the second query in same http call? Thanks.

No, inner hits will not do that. It will include the matching parent doc
per child hit and doesn't take into account that child documents may have
the same parent doc.

If you like to optimize for that then maybe you can fetch the parent
document yourself from the client side via a subsequent multi search
request? So you will then not be using inner hits and when your search
request returns you then execute a multi search for all unique _parent ids
in your search result.

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