Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A nested nested aggregation falls outside of its parent nested aggregation bounds #5728

Closed
wants to merge 2 commits into from

Conversation

martijnvg
Copy link
Member

Every nested aggs uses the non nested docs filter as parent reference. This can lead to incorrect doc counts and other metrics.

This PR sets the proper parent docs for nested nested aggs, which depends on which is the closest parent nested aggs.

@martijnvg martijnvg added bug and removed bug labels Apr 8, 2014
@martijnvg martijnvg changed the title Nested nested aggregation don't respect its parent nested aggregation. Nested nested aggregation doesn't respect its parent nested aggregation. Apr 8, 2014
return (NestedAggregator) parent;
}
}
return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to remember to update this when we add the reverse_nested aggregator :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heh true :) maybe we turn into a useful utility? Something like: findClosestAggregator(Class) which find the first agg of a specific impl up the hierarchy.

@uboness
Copy link
Contributor

uboness commented Apr 8, 2014

Good catch!

// So at the time a nested 'nested' aggs is parsed its closest parent nested aggs hasn't been constructed
// so we lookup its child filter when we really need it and that time the closest parent nested aggs
// has been created and we can use its child filter as the parent filter.
parentFilterNotCached = new ClosestNestedParentFilter(closestNestedAggregator);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need a better fix for this: it's bad that we pass the parent aggregator in but it's not fully constructed yet. Additionally, I'm concerned that the equals method of ClosestNestedParentFilter is not symetric, this could lead to duplicates in the filter cache.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ClosestNestedParentFilter#equals is invoked once the parent agg is completely constructed (FilterCacheFilterWrapper#getDocIdSet). I agree it is tricky, but I think the fix is ok? Maybe we need an assertion to double check.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is not OK because of the lack of symetry of the equals method: although new ClosestNestedParentFilter(parentAggregator).equals(parentFilter) would return true, parentFilter.equals(new ClosestNestedParentFilter(parentAggregator)) would return false, which means we might pollute the filter cache with a new filter that will never be reused every time this aggregation is used. So I think we need to fix the root issue, that is that parent aggregators should be fully constructed before constructing sub aggregators, before fixing this one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I see what you mean. The same issue then also exists in the nested query and filter, since it uses a similar mechanism to resolve the actual parent filter later on in the query execution.

@dazraf
Copy link

dazraf commented Apr 8, 2014

Hi Martijn,

Many thanks for this. I've tested this with v1.1.0 (excluding the test case which couldn't be merged).
Works correctly. Here's the script that I used and the respective results.
https://gist.github.com/dazraf/10126360

kind regards
Fuzz.

@dazraf
Copy link

dazraf commented Apr 8, 2014

I hope this is the right way to use the aggregation. Please let me know if there's a better way. Thanks

@martijnvg
Copy link
Member Author

@dazraf That way of using aggregations looks all right to me.

…ound the symmetry issue of ClosestNestedParentFilter#equals (this filter has been removed now)
@martijnvg
Copy link
Member Author

@jpountz I tried to get around the symmetry issue you just described by setting the parentFilter field lazily in the setNextReader() method. I don't think that this is the nicest fix (parentFilter field is not final and gets set lazily), but I think it is good enough.

@jpountz
Copy link
Contributor

jpountz commented Apr 9, 2014

Agreed that it is good enough now. +1 to merge.

Thanks @martijnvg for fixing this!

@martijnvg martijnvg changed the title Nested nested aggregation doesn't respect its parent nested aggregation. A nested nested aggregation falls outside of its parent nested aggregation bounds Apr 11, 2014
martijnvg added a commit that referenced this pull request Apr 11, 2014
…ent `nested` aggregation bounds.

The nested `nested` aggs now gets the proper parent docs that define its bounds correctly.

Closes #5728
@martijnvg martijnvg closed this in c4a49c2 Apr 11, 2014
martijnvg added a commit that referenced this pull request Apr 11, 2014
…ent `nested` aggregation bounds.

The nested `nested` aggs now gets the proper parent docs that define its bounds correctly.

Closes #5728
martijnvg added a commit that referenced this pull request Apr 11, 2014
…ent `nested` aggregation bounds.

The nested `nested` aggs now gets the proper parent docs that define its bounds correctly.

Closes #5728
@martijnvg martijnvg deleted the bug/nested-nested-agg branch May 18, 2015 23:32
mute pushed a commit to mute/elasticsearch that referenced this pull request Jul 29, 2015
…ent `nested` aggregation bounds.

The nested `nested` aggs now gets the proper parent docs that define its bounds correctly.

Closes elastic#5728
mute pushed a commit to mute/elasticsearch that referenced this pull request Jul 29, 2015
…ent `nested` aggregation bounds.

The nested `nested` aggs now gets the proper parent docs that define its bounds correctly.

Closes elastic#5728
@clintongormley clintongormley added :Search/Search Search-related issues that do not fall into other categories and removed :Nested Docs labels Feb 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>bug :Search/Search Search-related issues that do not fall into other categories v1.0.3 v1.1.1 v1.2.0 v2.0.0-beta1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants