# How to access children documents for a scripted query

**URL:** <https://discuss.elastic.co/t/how-to-access-children-documents-for-a-scripted-query/39238>\
**Category:** Elasticsearch\
**Created:** [January 14, 2016, 3:26pm UTC](https://discuss.elastic.co/t/how-to-access-children-documents-for-a-scripted-query/39238 "2016-01-14T15:26:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![yehosef](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yehosef/32/42175_2.png) [@yehosef](https://discuss.elastic.co/u/yehosef)\
**Post date:** [January 14, 2016, 3:26pm UTC](https://discuss.elastic.co/t/how-to-access-children-documents-for-a-scripted-query/39238/1 "2016-01-14T15:26:39Z")

</div>

I have a index that has a "user" document and a child document called "user\_day" which includes the actions the person did on a certain day. Let's say that the user\_day document includes "num\_actions" and "days\_since\_signup" fields.

I would like to find the users that have num\_actions on one day equal to num\_actions on the next day.

I tried a scripted query - but I don't know if or how to access the children documents in the script (I'm using inner\_hits). I'm thinking that's not possible because the children are filled in after the parent match. But maybe I'm wrong.

I thought perhaps I could do it with a pipeline aggregation - but I'm also not sure if that makes sense or helps. If I'm querying the users - I'm not sure that there are multiple things to combine or compare. Again, I'm not sure and I'm very new to the pipeline aggregations.

My last ideas is that I ask for user\_day that has a parent of user that has a child of user\_day with a scripted query that references the top parent document. But, again, I don't know how to access the parent document, if at all possible, in the script.

Does anyone know if/how I could do this?

(please don't reply that I really should be using a relational database!)

---

<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:** [January 14, 2016, 3:31pm UTC](https://discuss.elastic.co/t/how-to-access-children-documents-for-a-scripted-query/39238/2 "2016-01-14T15:31:36Z")

</div>

For this kind of behavioural stuff I normally advise re-orientating data around entity-centric indexes. Why and how here : [https://www.youtube.com/watch?v=yBf7oeJKH2Y](https://www.youtube.com/watch?v=yBf7oeJKH2Y)

---

<div class="post-metadata">

**Author:** ![yehosef](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yehosef/32/42175_2.png) [@yehosef](https://discuss.elastic.co/u/yehosef)\
**Post date:** [January 14, 2016, 3:34pm UTC](https://discuss.elastic.co/t/how-to-access-children-documents-for-a-scripted-query/39238/3 "2016-01-14T15:34:15Z")

</div>

Thanks! looks interesting.

---

<div class="post-metadata">

**Author:** ![yehosef](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yehosef/32/42175_2.png) [@yehosef](https://discuss.elastic.co/u/yehosef)\
**Post date:** [January 14, 2016, 4:21pm UTC](https://discuss.elastic.co/t/how-to-access-children-documents-for-a-scripted-query/39238/4 "2016-01-14T16:21:54Z")

</div>

Video is interesting - but it seems we're already doing what he's speaking about (eg, the users/user\_days are entities that we query..) - but I'll review more to see if I can get more ideas.

Incidentally - if these were nested documents instead of children documents, it might be possible to do ala [http://asquera.de/blog/2014-06-02/elasticsearch-script-filters/](http://asquera.de/blog/2014-06-02/elasticsearch-script-filters/)

---

<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:** [January 14, 2016, 4:49pm UTC](https://discuss.elastic.co/t/how-to-access-children-documents-for-a-scripted-query/39238/5 "2016-01-14T16:49:14Z")

</div>

What I'm talking about in the video is one JSON doc per user.

Right now you have one doc per user per day together with the sort of query that ticks the boxes for what I call the "Coke and Mentos" recipe: [https://speakerdeck.com/elastic/building-entity-centric-indexes?slide=5](https://speakerdeck.com/elastic/building-entity-centric-indexes?slide=5)

The [http://bit.ly/entcent](http://bit.ly/entcent) scripts I provide as an example provide the basis for building single-JSON-doc entity-centric indexes from your event store.

---

<div class="post-metadata">

**Author:** ![yehosef](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yehosef/32/42175_2.png) [@yehosef](https://discuss.elastic.co/u/yehosef)\
**Post date:** [January 14, 2016, 4:58pm UTC](https://discuss.elastic.co/t/how-to-access-children-documents-for-a-scripted-query/39238/6 "2016-01-14T16:58:36Z")

</div>

(didn't notice it's you in the video 🙂 )

ok - that's what I said in reference to if they were nested instead of children. But then we lose all the cool things we can do because they are children. If there is no other way - we might sync from the children to nested to enable this kind of query. As you pointed out from the bank example, it's also possible to have a "coke and mentos" recipe with a single document.

---

<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:** [July 5, 2017, 11:24pm UTC](https://discuss.elastic.co/t/how-to-access-children-documents-for-a-scripted-query/39238/7 "2017-07-05T23:24:27Z")

</div>


