How to do a nested in nested query?

Hi all,

My gist here: 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.


in your query you have:

'by_source' => {
    'nested' => {'path' => 'by_goal'},
    'aggs' => {
        'sources' => {
            'terms' => {'field' => ''}

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

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?

I have pasted a (simplified) reproduction of your scenario in the following gist:

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