# How can I use inner\_hits returned by child query as context in a script or query

**URL:** <https://discuss.elastic.co/t/how-can-i-use-inner-hits-returned-by-child-query-as-context-in-a-script-or-query/162114>\
**Category:** Elasticsearch\
**Created:** [December 26, 2018, 8:36am UTC](https://discuss.elastic.co/t/how-can-i-use-inner-hits-returned-by-child-query-as-context-in-a-script-or-query/162114 "2018-12-26T08:36:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![huan1](https://avatars.discourse-cdn.com/v4/letter/h/4da419/32.png) [@huan1](https://discuss.elastic.co/u/huan1)\
**Post date:** [December 26, 2018, 8:36am UTC](https://discuss.elastic.co/t/how-can-i-use-inner-hits-returned-by-child-query-as-context-in-a-script-or-query/162114/1 "2018-12-26T08:36:56Z")

</div>

e.g. there is one parent document:  
{"\_id": 1, "name": "th", "money": 100}  
and three child document:  
{"brand": "lenovo", "price": 20, "\_parent": 1}  
{"brand": "iphone", "price": 50, "\_parent": 1}  
{"brand": "mac", "price": 90, "\_parent": 1}

so how can i find the parent whose money \> sum(price of children those brand is iphone or mac)

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [December 26, 2018, 5:00pm UTC](https://discuss.elastic.co/t/how-can-i-use-inner-hits-returned-by-child-query-as-context-in-a-script-or-query/162114/2 "2018-12-26T17:00:19Z")

</div>

The concrete answer depends on which version of elasticsearch are you using. But in general, I would use `has_child` query with function score to calculate sum of all matched child records, then I would subtract money from it using another function score on the top level and exclude documents with negative scores using `min_score` parameter.

---

<div class="post-metadata">

**Author:** ![huan1](https://avatars.discourse-cdn.com/v4/letter/h/4da419/32.png) [@huan1](https://discuss.elastic.co/u/huan1)\
**Post date:** [December 27, 2018, 3:15am UTC](https://discuss.elastic.co/t/how-can-i-use-inner-hits-returned-by-child-query-as-context-in-a-script-or-query/162114/3 "2018-12-27T03:15:44Z")

</div>

thanks for replying. In fact the calculation is more complex than the example I give. I have to collect the child records that match some conditions and calculate them in a "parent" query.  
here is the actual example:  
a people has serval events. each event has fields event\_type, event\_city and event\_time. I want to find the people that do different type event in the same city and within one week  
mapping may goes like this:  
{  
"people": {  
"properties": {  
"name": {"type": "keyword"}  
}  
},  
"event\_eat": {  
"\_parent": "people",  
"properties": {  
"time": {"type": "datetime"},  
"city": {"type": "keyword"}  
}  
},  
"event\_drink": {  
"\_parent": "people",  
"properties": {  
"time": {"type": "datetime"},  
"city": {"type": "keyword"}  
}  
}  
}  
the "time" records should be collected completely as an array in different has\_child query, and in "parent" query I should calculate the two arrays of time by some time-sequence function

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [December 27, 2018, 2:38pm UTC](https://discuss.elastic.co/t/how-can-i-use-inner-hits-returned-by-child-query-as-context-in-a-script-or-query/162114/4 "2018-12-27T14:38:25Z")

</div>

> [@huan1](#):
>
> the "time" records should be collected completely as an array in different has\_child query, and in "parent" query I should calculate the two arrays of time by some time-sequence function

Sorry, I don't understand this requirement and I still don't know which version of ES you are using.

The main limitation here is that we don't have much space to store intermediate information and pass it between child and parent queries. It is basically limited to a single float value and just a few operations that we can do on it (max, min, sum, avg). If you can fit it into a float and use this operations - something can be done. Otherwise, you would have to retrieve this data and do it in your application, or split it into multiple queries.

---

<div class="post-metadata">

**Author:** ![huan1](https://avatars.discourse-cdn.com/v4/letter/h/4da419/32.png) [@huan1](https://discuss.elastic.co/u/huan1)\
**Post date:** [December 28, 2018, 6:53am UTC](https://discuss.elastic.co/t/how-can-i-use-inner-hits-returned-by-child-query-as-context-in-a-script-or-query/162114/5 "2018-12-28T06:53:23Z")

</div>

The version of ES is 5.4.2, and your reply is helpful. My intention is to find a parent record in two steps. 1st, find the child records that match a query and retrieve some fields of them. Then calculate these fields to ensure whether the parent record could be recalled.  
As you mentioned above, only a single float value can be passed between child and parent queries. So I can retrieve data from ES but that may be much more expensive because millions of records will be pulled out, loaded in memory in my application, and in the last pieced into a huge ids query to search parent records.  
Are there any more suggestions to avoid calculating data outside ES?

---

<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:** [January 25, 2019, 6:53am UTC](https://discuss.elastic.co/t/how-can-i-use-inner-hits-returned-by-child-query-as-context-in-a-script-or-query/162114/6 "2019-01-25T06:53:32Z")

</div>

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