|
@@ -15,22 +15,24 @@ import org.elasticsearch.common.xcontent.ToXContent;
|
|
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
import org.elasticsearch.search.DocValueFormat;
|
|
|
import org.elasticsearch.search.aggregations.InternalAggregation;
|
|
|
-import org.elasticsearch.search.aggregations.metrics.InternalNumericMetricsAggregation;
|
|
|
+import org.elasticsearch.search.aggregations.metrics.InternalMultiValueAggregation;
|
|
|
import org.elasticsearch.search.sort.SortOrder;
|
|
|
import org.elasticsearch.search.sort.SortValue;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
import static java.util.Collections.emptyList;
|
|
|
import static org.elasticsearch.search.builder.SearchSourceBuilder.SORT_FIELD;
|
|
|
import static org.elasticsearch.xpack.analytics.topmetrics.TopMetricsAggregationBuilder.METRIC_FIELD;
|
|
|
|
|
|
|
|
|
-public class InternalTopMetrics extends InternalNumericMetricsAggregation.MultiValue {
|
|
|
+public class InternalTopMetrics extends InternalMultiValueAggregation {
|
|
|
private final SortOrder sortOrder;
|
|
|
private final int size;
|
|
|
private final List<String> metricNames;
|
|
@@ -162,28 +164,53 @@ public class InternalTopMetrics extends InternalNumericMetricsAggregation.MultiV
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public double value(String name) {
|
|
|
- int index = metricNames.indexOf(name);
|
|
|
+ public final double sortValue(String key) {
|
|
|
+ int index = metricNames.indexOf(key);
|
|
|
if (index < 0) {
|
|
|
- throw new IllegalArgumentException("unknown metric [" + name + "]");
|
|
|
+ throw new IllegalArgumentException("unknown metric [" + key + "]");
|
|
|
}
|
|
|
if (topMetrics.isEmpty()) {
|
|
|
return Double.NaN;
|
|
|
}
|
|
|
- assert topMetrics.size() == 1 : "property paths should only resolve against top metrics with size == 1.";
|
|
|
- // TODO it'd probably be nicer to have "compareTo" instead of assuming a double.
|
|
|
+
|
|
|
MetricValue value = topMetrics.get(0).metricValues.get(index);
|
|
|
if (value == null) {
|
|
|
return Double.NaN;
|
|
|
}
|
|
|
+
|
|
|
+ // TODO it'd probably be nicer to have "compareTo" instead of assuming a double.
|
|
|
+ // non-numeric fields always return NaN
|
|
|
return value.numberValue().doubleValue();
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public List<String> getValuesAsStrings(String name) {
|
|
|
+ int index = metricNames.indexOf(name);
|
|
|
+ if (index < 0) {
|
|
|
+ throw new IllegalArgumentException("unknown metric [" + name + "]");
|
|
|
+ }
|
|
|
+ if (topMetrics.isEmpty()) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ return topMetrics.stream().map(r -> {
|
|
|
+ MetricValue value = r.metricValues.get(index);
|
|
|
+ if (value == null) {
|
|
|
+ return "null";
|
|
|
+ }
|
|
|
+ return value.getValue().format(value.getFormat());
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public Iterable<String> valueNames() {
|
|
|
return metricNames;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ protected boolean mustReduceOnSingleInternalAgg() {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
SortOrder getSortOrder() {
|
|
|
return sortOrder;
|
|
|
}
|