|
@@ -19,6 +19,10 @@
|
|
|
|
|
|
package org.elasticsearch.search.aggregations.metrics.percentiles;
|
|
|
|
|
|
+import org.elasticsearch.common.Strings;
|
|
|
+import org.elasticsearch.common.xcontent.ToXContent;
|
|
|
+import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
+import org.elasticsearch.common.xcontent.json.JsonXContent;
|
|
|
import org.elasticsearch.search.DocValueFormat;
|
|
|
import org.elasticsearch.search.aggregations.Aggregation.CommonFields;
|
|
|
import org.elasticsearch.search.aggregations.InternalAggregation;
|
|
@@ -27,11 +31,14 @@ import org.elasticsearch.test.InternalAggregationTestCase;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.function.Predicate;
|
|
|
|
|
|
+import static org.hamcrest.Matchers.equalTo;
|
|
|
+
|
|
|
public abstract class AbstractPercentilesTestCase<T extends InternalAggregation & Iterable<Percentile>>
|
|
|
extends InternalAggregationTestCase<T> {
|
|
|
|
|
@@ -49,7 +56,7 @@ public abstract class AbstractPercentilesTestCase<T extends InternalAggregation
|
|
|
|
|
|
@Override
|
|
|
protected T createTestInstance(String name, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
|
|
|
- int numValues = randomInt(100);
|
|
|
+ int numValues = frequently() ? randomInt(100) : 0;
|
|
|
double[] values = new double[numValues];
|
|
|
for (int i = 0; i < numValues; ++i) {
|
|
|
values[i] = randomDouble();
|
|
@@ -89,4 +96,53 @@ public abstract class AbstractPercentilesTestCase<T extends InternalAggregation
|
|
|
protected Predicate<String> excludePathsFromXContentInsertion() {
|
|
|
return path -> path.endsWith(CommonFields.VALUES.getPreferredName());
|
|
|
}
|
|
|
+
|
|
|
+ protected abstract void assertPercentile(T agg, Double value);
|
|
|
+
|
|
|
+ public void testEmptyRanksXContent() throws IOException {
|
|
|
+ double[] percents = new double[]{1,2,3};
|
|
|
+ boolean keyed = randomBoolean();
|
|
|
+ DocValueFormat docValueFormat = randomNumericDocValueFormat();
|
|
|
+
|
|
|
+ T agg = createTestInstance("test", Collections.emptyList(), Collections.emptyMap(), keyed, docValueFormat, percents, new double[0]);
|
|
|
+
|
|
|
+ for (Percentile percentile : agg) {
|
|
|
+ Double value = percentile.getValue();
|
|
|
+ assertPercentile(agg, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint();
|
|
|
+ builder.startObject();
|
|
|
+ agg.doXContentBody(builder, ToXContent.EMPTY_PARAMS);
|
|
|
+ builder.endObject();
|
|
|
+ String expected;
|
|
|
+ if (keyed) {
|
|
|
+ expected = "{\n" +
|
|
|
+ " \"values\" : {\n" +
|
|
|
+ " \"1.0\" : null,\n" +
|
|
|
+ " \"2.0\" : null,\n" +
|
|
|
+ " \"3.0\" : null\n" +
|
|
|
+ " }\n" +
|
|
|
+ "}";
|
|
|
+ } else {
|
|
|
+ expected = "{\n" +
|
|
|
+ " \"values\" : [\n" +
|
|
|
+ " {\n" +
|
|
|
+ " \"key\" : 1.0,\n" +
|
|
|
+ " \"value\" : null\n" +
|
|
|
+ " },\n" +
|
|
|
+ " {\n" +
|
|
|
+ " \"key\" : 2.0,\n" +
|
|
|
+ " \"value\" : null\n" +
|
|
|
+ " },\n" +
|
|
|
+ " {\n" +
|
|
|
+ " \"key\" : 3.0,\n" +
|
|
|
+ " \"value\" : null\n" +
|
|
|
+ " }\n" +
|
|
|
+ " ]\n" +
|
|
|
+ "}";
|
|
|
+ }
|
|
|
+
|
|
|
+ assertThat(Strings.toString(builder), equalTo(expected));
|
|
|
+ }
|
|
|
}
|