-
Notifications
You must be signed in to change notification settings - Fork 24.8k
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
[Transform] add support for terms agg in transforms #56696
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
import org.elasticsearch.search.aggregations.Aggregation; | ||
import org.elasticsearch.search.aggregations.AggregationBuilder; | ||
import org.elasticsearch.search.aggregations.PipelineAggregationBuilder; | ||
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation; | ||
import org.elasticsearch.search.aggregations.bucket.SingleBucketAggregation; | ||
import org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregation; | ||
import org.elasticsearch.search.aggregations.metrics.GeoBounds; | ||
|
@@ -52,6 +53,7 @@ public final class AggregationResultUtils { | |
tempMap.put(GeoBounds.class.getName(), new GeoBoundsAggExtractor()); | ||
tempMap.put(Percentiles.class.getName(), new PercentilesAggExtractor()); | ||
tempMap.put(SingleBucketAggregation.class.getName(), new SingleBucketAggExtractor()); | ||
tempMap.put(MultiBucketsAggregation.class.getName(), new MultiBucketsAggExtractor()); | ||
TYPE_VALUE_EXTRACTOR_MAP = Collections.unmodifiableMap(tempMap); | ||
} | ||
|
||
|
@@ -120,6 +122,8 @@ static AggValueExtractor getExtractor(Aggregation aggregation) { | |
return TYPE_VALUE_EXTRACTOR_MAP.get(Percentiles.class.getName()); | ||
} else if (aggregation instanceof SingleBucketAggregation) { | ||
return TYPE_VALUE_EXTRACTOR_MAP.get(SingleBucketAggregation.class.getName()); | ||
} else if (aggregation instanceof MultiBucketsAggregation) { | ||
return TYPE_VALUE_EXTRACTOR_MAP.get(MultiBucketsAggregation.class.getName()); | ||
} else { | ||
// Execution should never reach this point! | ||
// Creating transforms with unsupported aggregations shall not be possible | ||
|
@@ -246,6 +250,35 @@ public Object value(Aggregation agg, Map<String, String> fieldTypeMap, String lo | |
} | ||
} | ||
|
||
static class MultiBucketsAggExtractor implements AggValueExtractor { | ||
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. 👍 this is almost exactly the way I implemented it, too (well, there is probably no other way). 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. Great minds think alike. |
||
@Override | ||
public Object value(Aggregation agg, Map<String, String> fieldTypeMap, String lookupFieldPrefix) { | ||
MultiBucketsAggregation aggregation = (MultiBucketsAggregation) agg; | ||
|
||
HashMap<String, Object> nested = new HashMap<>(); | ||
|
||
for (MultiBucketsAggregation.Bucket bucket : aggregation.getBuckets()) { | ||
if (bucket.getAggregations().iterator().hasNext() == false) { | ||
nested.put(bucket.getKeyAsString(), bucket.getDocCount()); | ||
} else { | ||
HashMap<String, Object> nestedBucketObject = new HashMap<>(); | ||
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. it would be good to cover this branch and have a test with nested terms aggs, like your common user example, broke down by e.g. businesses or filtered by something 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. OK, will do. |
||
for (Aggregation subAgg : bucket.getAggregations()) { | ||
nestedBucketObject.put( | ||
subAgg.getName(), | ||
getExtractor(subAgg).value( | ||
subAgg, | ||
fieldTypeMap, | ||
lookupFieldPrefix.isEmpty() ? agg.getName() : lookupFieldPrefix + "." + agg.getName() | ||
) | ||
); | ||
} | ||
nested.put(bucket.getKeyAsString(), nestedBucketObject); | ||
} | ||
} | ||
return nested; | ||
} | ||
} | ||
|
||
static class ScriptedMetricAggExtractor implements AggValueExtractor { | ||
@Override | ||
public Object value(Aggregation agg, Map<String, String> fieldTypeMap, String lookupFieldPrefix) { | ||
|
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.
😄