|
19 | 19 | package org.elasticsearch.search.aggregations.metrics;
|
20 | 20 |
|
21 | 21 | import org.elasticsearch.common.io.stream.Writeable;
|
| 22 | +import org.elasticsearch.common.xcontent.ToXContent; |
| 23 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 24 | +import org.elasticsearch.common.xcontent.json.JsonXContent; |
22 | 25 | import org.elasticsearch.search.DocValueFormat;
|
23 | 26 | import org.elasticsearch.search.aggregations.ParsedAggregation;
|
24 | 27 | import org.elasticsearch.search.aggregations.metrics.stats.InternalStats;
|
25 | 28 | import org.elasticsearch.search.aggregations.metrics.stats.ParsedStats;
|
26 | 29 | import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
|
27 | 30 | import org.elasticsearch.test.InternalAggregationTestCase;
|
28 | 31 |
|
| 32 | +import java.io.IOException; |
| 33 | +import java.util.Collections; |
29 | 34 | import java.util.HashMap;
|
30 | 35 | import java.util.List;
|
31 | 36 | import java.util.Map;
|
@@ -80,7 +85,7 @@ static void assertStats(InternalStats aggregation, ParsedStats parsed) {
|
80 | 85 | long count = aggregation.getCount();
|
81 | 86 | assertEquals(count, parsed.getCount());
|
82 | 87 | // for count == 0, fields are rendered as `null`, so we test that we parse to default values used also in the reduce phase
|
83 |
| - assertEquals(count > 0 ? aggregation.getMin() : Double.POSITIVE_INFINITY , parsed.getMin(), 0); |
| 88 | + assertEquals(count > 0 ? aggregation.getMin() : Double.POSITIVE_INFINITY, parsed.getMin(), 0); |
84 | 89 | assertEquals(count > 0 ? aggregation.getMax() : Double.NEGATIVE_INFINITY, parsed.getMax(), 0);
|
85 | 90 | assertEquals(count > 0 ? aggregation.getSum() : 0, parsed.getSum(), 0);
|
86 | 91 | assertEquals(count > 0 ? aggregation.getAvg() : 0, parsed.getAvg(), 0);
|
@@ -153,5 +158,55 @@ protected InternalStats mutateInstance(InternalStats instance) {
|
153 | 158 | }
|
154 | 159 | return new InternalStats(name, count, sum, min, max, formatter, pipelineAggregators, metaData);
|
155 | 160 | }
|
| 161 | + |
| 162 | + public void testDoXContentBody() throws IOException { |
| 163 | + // count is greater than zero |
| 164 | + double min = randomDoubleBetween(-1000000, 1000000, true); |
| 165 | + double max = randomDoubleBetween(-1000000, 1000000, true); |
| 166 | + double sum = randomDoubleBetween(-1000000, 1000000, true); |
| 167 | + int count = randomIntBetween(1, 10); |
| 168 | + DocValueFormat format = randomNumericDocValueFormat(); |
| 169 | + InternalStats internalStats = createInstance("stats", count, sum, min, max, format, Collections.emptyList(), null); |
| 170 | + XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint(); |
| 171 | + builder.startObject(); |
| 172 | + internalStats.doXContentBody(builder, ToXContent.EMPTY_PARAMS); |
| 173 | + builder.endObject(); |
| 174 | + |
| 175 | + String expected = "{\n" + |
| 176 | + " \"count\" : " + count + ",\n" + |
| 177 | + " \"min\" : " + min + ",\n" + |
| 178 | + " \"max\" : " + max + ",\n" + |
| 179 | + " \"avg\" : " + internalStats.getAvg() + ",\n" + |
| 180 | + " \"sum\" : " + sum; |
| 181 | + if (format != DocValueFormat.RAW) { |
| 182 | + expected += ",\n"+ |
| 183 | + " \"min_as_string\" : \"" + format.format(internalStats.getMin()) + "\",\n" + |
| 184 | + " \"max_as_string\" : \"" + format.format(internalStats.getMax()) + "\",\n" + |
| 185 | + " \"avg_as_string\" : \"" + format.format(internalStats.getAvg()) + "\",\n" + |
| 186 | + " \"sum_as_string\" : \"" + format.format(internalStats.getSum()) + "\""; |
| 187 | + } |
| 188 | + expected += "\n}"; |
| 189 | + assertEquals(expected, builder.string()); |
| 190 | + |
| 191 | + // count is zero |
| 192 | + format = randomNumericDocValueFormat(); |
| 193 | + min = 0.0; |
| 194 | + max = 0.0; |
| 195 | + sum = 0.0; |
| 196 | + count = 0; |
| 197 | + internalStats = createInstance("stats", count, sum, min, max, format, Collections.emptyList(), null); |
| 198 | + builder = JsonXContent.contentBuilder().prettyPrint(); |
| 199 | + builder.startObject(); |
| 200 | + internalStats.doXContentBody(builder, ToXContent.EMPTY_PARAMS); |
| 201 | + builder.endObject(); |
| 202 | + |
| 203 | + assertEquals("{\n" + |
| 204 | + " \"count\" : 0,\n" + |
| 205 | + " \"min\" : null,\n" + |
| 206 | + " \"max\" : null,\n" + |
| 207 | + " \"avg\" : null,\n" + |
| 208 | + " \"sum\" : 0.0\n" + |
| 209 | + "}", builder.string()); |
| 210 | + } |
156 | 211 | }
|
157 | 212 |
|
0 commit comments