|
@@ -9,6 +9,7 @@ package org.elasticsearch.xpack.transform.transforms.pivot;
|
|
|
|
|
|
import org.elasticsearch.common.ParseField;
|
|
|
import org.elasticsearch.common.geo.GeoPoint;
|
|
|
+import org.elasticsearch.common.io.stream.StreamOutput;
|
|
|
import org.elasticsearch.common.xcontent.ContextParser;
|
|
|
import org.elasticsearch.common.xcontent.DeprecationHandler;
|
|
|
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
|
|
@@ -20,6 +21,7 @@ import org.elasticsearch.script.Script;
|
|
|
import org.elasticsearch.search.aggregations.Aggregation;
|
|
|
import org.elasticsearch.search.aggregations.AggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
|
|
+import org.elasticsearch.search.aggregations.InternalAggregation;
|
|
|
import org.elasticsearch.search.aggregations.PipelineAggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.PipelineAggregatorBuilders;
|
|
|
import org.elasticsearch.search.aggregations.bucket.SingleBucketAggregation;
|
|
@@ -36,6 +38,8 @@ import org.elasticsearch.search.aggregations.metrics.CardinalityAggregationBuild
|
|
|
import org.elasticsearch.search.aggregations.metrics.ExtendedStatsAggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.metrics.GeoBounds;
|
|
|
import org.elasticsearch.search.aggregations.metrics.GeoCentroid;
|
|
|
+import org.elasticsearch.search.aggregations.metrics.InternalMultiValueAggregation;
|
|
|
+import org.elasticsearch.search.aggregations.metrics.InternalNumericMetricsAggregation;
|
|
|
import org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.metrics.MinAggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation;
|
|
@@ -77,6 +81,7 @@ import java.util.Set;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import static java.util.Arrays.asList;
|
|
|
+import static java.util.Collections.emptyMap;
|
|
|
import static org.hamcrest.CoreMatchers.equalTo;
|
|
|
import static org.hamcrest.CoreMatchers.hasItem;
|
|
|
import static org.hamcrest.CoreMatchers.is;
|
|
@@ -116,6 +121,96 @@ public class AggregationResultUtilsTests extends ESTestCase {
|
|
|
.collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
+ class TestMultiValueAggregation extends InternalMultiValueAggregation {
|
|
|
+
|
|
|
+ private final Map<String, String> values;
|
|
|
+
|
|
|
+ TestMultiValueAggregation(String name, Map<String, String> values) {
|
|
|
+ super(name, emptyMap());
|
|
|
+ this.values = values;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getWriteableName() {
|
|
|
+ throw new UnsupportedOperationException();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<String> getValuesAsStrings(String name) {
|
|
|
+ return Collections.singletonList(values.get(name).toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void doWriteTo(StreamOutput out) throws IOException {
|
|
|
+ throw new UnsupportedOperationException();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public InternalAggregation reduce(List<InternalAggregation> aggregations, ReduceContext reduceContext) {
|
|
|
+ throw new UnsupportedOperationException();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
|
|
|
+ throw new UnsupportedOperationException();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Iterable<String> valueNames() {
|
|
|
+ return values.keySet();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected boolean mustReduceOnSingleInternalAgg() {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object getProperty(List<String> path) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ class TestNumericMultiValueAggregation extends InternalNumericMetricsAggregation.MultiValue {
|
|
|
+
|
|
|
+ private final Map<String, Double> values;
|
|
|
+
|
|
|
+ TestNumericMultiValueAggregation(String name, Map<String, Double> values) {
|
|
|
+ super(name, emptyMap());
|
|
|
+ this.values = values;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getWriteableName() {
|
|
|
+ throw new UnsupportedOperationException();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public double value(String name) {
|
|
|
+ return values.get(name);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void doWriteTo(StreamOutput out) throws IOException {
|
|
|
+ throw new UnsupportedOperationException();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public InternalAggregation reduce(List<InternalAggregation> aggregations, ReduceContext reduceContext) {
|
|
|
+ throw new UnsupportedOperationException();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
|
|
|
+ throw new UnsupportedOperationException();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Iterable<String> valueNames() {
|
|
|
+ return values.keySet();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
protected NamedXContentRegistry xContentRegistry() {
|
|
|
return namedXContentRegistry;
|
|
@@ -664,6 +759,65 @@ public class AggregationResultUtilsTests extends ESTestCase {
|
|
|
);
|
|
|
}
|
|
|
|
|
|
+ public void testMultiValueAggExtractor() {
|
|
|
+ Aggregation agg = new TestMultiValueAggregation("mv_metric", Collections.singletonMap("ip", "192.168.1.1"));
|
|
|
+
|
|
|
+ assertThat(
|
|
|
+ AggregationResultUtils.getExtractor(agg).value(agg, Collections.singletonMap("mv_metric.ip", "ip"), ""),
|
|
|
+ equalTo(Collections.singletonMap("ip", "192.168.1.1"))
|
|
|
+ );
|
|
|
+
|
|
|
+ agg = new TestMultiValueAggregation("mv_metric", Collections.singletonMap("top_answer", "fortytwo"));
|
|
|
+
|
|
|
+ assertThat(
|
|
|
+ AggregationResultUtils.getExtractor(agg).value(agg, Collections.singletonMap("mv_metric.written_answer", "written_answer"), ""),
|
|
|
+ equalTo(Collections.singletonMap("top_answer", "fortytwo"))
|
|
|
+ );
|
|
|
+
|
|
|
+ agg = new TestMultiValueAggregation("mv_metric", Map.of("ip", "192.168.1.1", "top_answer", "fortytwo"));
|
|
|
+
|
|
|
+ assertThat(
|
|
|
+ AggregationResultUtils.getExtractor(agg).value(agg, Map.of("mv_metric.top_answer", "keyword", "mv_metric.ip", "ip"), ""),
|
|
|
+ equalTo(Map.of("top_answer", "fortytwo", "ip", "192.168.1.1"))
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testNumericMultiValueAggExtractor() {
|
|
|
+ Aggregation agg = new TestNumericMultiValueAggregation(
|
|
|
+ "mv_metric",
|
|
|
+ Collections.singletonMap("approx_answer", Double.valueOf(42.2))
|
|
|
+ );
|
|
|
+
|
|
|
+ assertThat(
|
|
|
+ AggregationResultUtils.getExtractor(agg).value(agg, Collections.singletonMap("mv_metric.approx_answer", "double"), ""),
|
|
|
+ equalTo(Collections.singletonMap("approx_answer", Double.valueOf(42.2)))
|
|
|
+ );
|
|
|
+
|
|
|
+ agg = new TestNumericMultiValueAggregation("mv_metric", Collections.singletonMap("exact_answer", Double.valueOf(42.0)));
|
|
|
+
|
|
|
+ assertThat(
|
|
|
+ AggregationResultUtils.getExtractor(agg).value(agg, Collections.singletonMap("mv_metric.exact_answer", "long"), ""),
|
|
|
+ equalTo(Collections.singletonMap("exact_answer", Long.valueOf(42)))
|
|
|
+ );
|
|
|
+
|
|
|
+ agg = new TestNumericMultiValueAggregation(
|
|
|
+ "mv_metric",
|
|
|
+ Map.of("approx_answer", Double.valueOf(42.2), "exact_answer", Double.valueOf(42.0))
|
|
|
+ );
|
|
|
+
|
|
|
+ assertThat(
|
|
|
+ AggregationResultUtils.getExtractor(agg)
|
|
|
+ .value(agg, Map.of("mv_metric.approx_answer", "double", "mv_metric.exact_answer", "long"), ""),
|
|
|
+ equalTo(Map.of("approx_answer", Double.valueOf(42.2), "exact_answer", Long.valueOf(42)))
|
|
|
+ );
|
|
|
+
|
|
|
+ assertThat(
|
|
|
+ AggregationResultUtils.getExtractor(agg)
|
|
|
+ .value(agg, Map.of("filter.mv_metric.approx_answer", "double", "filter.mv_metric.exact_answer", "long"), "filter"),
|
|
|
+ equalTo(Map.of("approx_answer", Double.valueOf(42.2), "exact_answer", Long.valueOf(42)))
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
private ScriptedMetric createScriptedMetric(Object returnValue) {
|
|
|
ScriptedMetric agg = mock(ScriptedMetric.class);
|
|
|
when(agg.aggregation()).thenReturn(returnValue);
|