# How to do a nested in nested query?

**URL:** <https://discuss.elastic.co/t/how-to-do-a-nested-in-nested-query/26794>\
**Category:** Elasticsearch\
**Created:** [August 4, 2015, 11:22am UTC](https://discuss.elastic.co/t/how-to-do-a-nested-in-nested-query/26794 "2015-08-04T11:22:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wflanagan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wflanagan/32/3856_2.png) [@wflanagan](https://discuss.elastic.co/u/wflanagan)\
**Post date:** [August 4, 2015, 11:22am UTC](https://discuss.elastic.co/t/how-to-do-a-nested-in-nested-query/26794/1 "2015-08-04T11:22:48Z")

</div>

Hi all,

My gist here: [https://gist.github.com/wflanagan/eb44812a9272c0528b73](https://gist.github.com/wflanagan/eb44812a9272c0528b73) has the mapping, the query, and the results.

I'm trying to do a, what I thought was simple, nested in nested query. For a document, PageStat, I have "by\_goals" nested document that has a by\_sources nested document inside it. I'm just trying to aggregate for a "PageStat" the source\_id counts by goal for that page by day.

According to SO, this should work. However, I haven't manage to get this done. 4 frigging days.

Help?

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [August 4, 2015, 11:33am UTC](https://discuss.elastic.co/t/how-to-do-a-nested-in-nested-query/26794/2 "2015-08-04T11:33:40Z")

</div>

in your query you have:

```json
'by_source' => {
    'nested' => {'path' => 'by_goal'},
    'aggs' => {
        'sources' => {
            'terms' => {'field' => 'by_goal.by_source.id'}
         }
    }
}

```

I think the `path` here should be `by_source` since you are looking for the inner nested document here?

---

<div class="post-metadata">

**Author:** ![wflanagan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wflanagan/32/3856_2.png) [@wflanagan](https://discuss.elastic.co/u/wflanagan)\
**Post date:** [August 4, 2015, 11:35am UTC](https://discuss.elastic.co/t/how-to-do-a-nested-in-nested-query/26794/3 "2015-08-04T11:35:12Z")

</div>

OK.. there is a by\_source on the root.. and a by\_source that is below the by\_goal.. (nested in nested). This is the one I'm querying.. not the root object. Given that, is that what I should still do?

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [August 4, 2015, 11:39am UTC](https://discuss.elastic.co/t/how-to-do-a-nested-in-nested-query/26794/4 "2015-08-04T11:39:55Z")

</div>

I have pasted a (simplified) reproduction of your scenario in the following gist: [https://gist.github.com/colings86/adb4e18b721611644597](https://gist.github.com/colings86/adb4e18b721611644597)

I think your problem is that your paths need to be changed to be relative to your root object. So to reference the `by_source` which is below `by_goal` in your inner nested aggregation you need to specify the path as `by_goal.by_source`, the same with the paths in your terms aggregations

Hope that helps

---

<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:57pm UTC](https://discuss.elastic.co/t/how-to-do-a-nested-in-nested-query/26794/5 "2017-07-05T23:57:33Z")

</div>


