-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
ML: Add support for single bucket aggs in Datafeeds #37544
Merged
benwtrent
merged 4 commits into
elastic:master
from
benwtrent:feature/ml-support-single-bucket-aggs
Jan 18, 2019
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5640b97
ML: Adding support for SingleBucketAggs
benwtrent e2380c5
ML: Adding single bucket agg support
benwtrent d7a9522
Fixing minor bug in parsing aggs, adding tests
benwtrent ebf0817
Merge branch 'master' into feature/ml-support-single-bucket-aggs
benwtrent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
import org.elasticsearch.search.aggregations.Aggregation; | ||
import org.elasticsearch.search.aggregations.Aggregations; | ||
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation; | ||
import org.elasticsearch.search.aggregations.bucket.SingleBucketAggregation; | ||
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram; | ||
import org.elasticsearch.search.aggregations.metrics.Max; | ||
import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation; | ||
|
@@ -34,6 +35,7 @@ | |
import java.util.Set; | ||
import java.util.SortedMap; | ||
import java.util.TreeMap; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Processes {@link Aggregation} objects and writes flat JSON documents for each leaf aggregation. | ||
|
@@ -93,18 +95,39 @@ private void processAggs(long docCount, List<Aggregation> aggregations) throws I | |
|
||
List<Aggregation> leafAggregations = new ArrayList<>(); | ||
List<MultiBucketsAggregation> bucketAggregations = new ArrayList<>(); | ||
List<SingleBucketAggregation> singleBucketAggregations = new ArrayList<>(); | ||
|
||
// Sort into leaf and bucket aggregations. | ||
// The leaf aggregations will be processed first. | ||
for (Aggregation agg : aggregations) { | ||
if (agg instanceof MultiBucketsAggregation) { | ||
bucketAggregations.add((MultiBucketsAggregation)agg); | ||
} else if (agg instanceof SingleBucketAggregation){ | ||
// Skip a level down for single bucket aggs, if they have a sub-agg that is not | ||
// a bucketed agg we should treat it like a leaf in this bucket | ||
SingleBucketAggregation singleBucketAggregation = (SingleBucketAggregation)agg; | ||
for (Aggregation subAgg : singleBucketAggregation.getAggregations()) { | ||
if (subAgg instanceof MultiBucketsAggregation || subAgg instanceof SingleBucketAggregation) { | ||
singleBucketAggregations.add(singleBucketAggregation); | ||
} else { | ||
leafAggregations.add(subAgg); | ||
} | ||
} | ||
} else { | ||
leafAggregations.add(agg); | ||
} | ||
} | ||
|
||
if (bucketAggregations.size() > 1) { | ||
// If on the current level (indicated via bucketAggregations) or one of the next levels (singleBucketAggregations) | ||
// we have more than 1 `MultiBucketsAggregation`, we should error out. | ||
// We need to make the check in this way as each of the items in `singleBucketAggregations` is treated as a separate branch | ||
// in the recursive handling of this method. | ||
int bucketAggLevelCount = Math.max(bucketAggregations.size(), (int)singleBucketAggregations.stream() | ||
davidkyle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.flatMap(s -> asList(s.getAggregations()).stream()) | ||
.filter(MultiBucketsAggregation.class::isInstance) | ||
.count()); | ||
|
||
if (bucketAggLevelCount > 1) { | ||
throw new IllegalArgumentException("Multiple bucket aggregations at the same level are not supported"); | ||
} | ||
|
||
|
@@ -137,6 +160,18 @@ private void processAggs(long docCount, List<Aggregation> aggregations) throws I | |
} | ||
} | ||
} | ||
noMoreBucketsToProcess = singleBucketAggregations.isEmpty() && noMoreBucketsToProcess; | ||
// we support more than one `SingleBucketAggregation` at each level | ||
// However, we only want to recurse with multi/single bucket aggs. | ||
// Non-bucketed sub-aggregations were handle as leaf aggregations at this level | ||
for (SingleBucketAggregation singleBucketAggregation : singleBucketAggregations) { | ||
processAggs(singleBucketAggregation.getDocCount(), | ||
asList(singleBucketAggregation.getAggregations()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: don't need the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do, |
||
.stream() | ||
.filter( | ||
aggregation -> (aggregation instanceof MultiBucketsAggregation || aggregation instanceof SingleBucketAggregation)) | ||
.collect(Collectors.toList())); | ||
} | ||
|
||
// If there are no more bucket aggregations to process we've reached the end | ||
// and it's time to write the doc | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if the test should be
subAgg instanceof HasAggregations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@davidkyle I wish it could be :( but
MultiBucketsAggregation
does not implement that interface. ItsBuckets
class does though :(. No way to get toBuckets
without first casting, which requires aninstanceof
check anyways.