|
19 | 19 |
|
20 | 20 | package org.elasticsearch.search.aggregations.pipeline.moving.avg;
|
21 | 21 |
|
| 22 | +import org.elasticsearch.action.bulk.BulkRequestBuilder; |
| 23 | +import org.elasticsearch.action.bulk.BulkResponse; |
22 | 24 | import org.elasticsearch.action.index.IndexRequestBuilder;
|
23 | 25 | import org.elasticsearch.action.search.SearchPhaseExecutionException;
|
24 | 26 | import org.elasticsearch.action.search.SearchResponse;
|
| 27 | +import org.elasticsearch.action.support.WriteRequest; |
25 | 28 | import org.elasticsearch.common.collect.EvictingQueue;
|
26 | 29 | import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
|
27 | 30 | import org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket;
|
|
41 | 44 | import org.elasticsearch.test.ESIntegTestCase;
|
42 | 45 | import org.hamcrest.Matchers;
|
43 | 46 |
|
| 47 | +import java.io.IOException; |
44 | 48 | import java.util.ArrayList;
|
45 | 49 | import java.util.Arrays;
|
46 | 50 | import java.util.Collection;
|
|
67 | 71 | public class MovAvgIT extends ESIntegTestCase {
|
68 | 72 | private static final String INTERVAL_FIELD = "l_value";
|
69 | 73 | private static final String VALUE_FIELD = "v_value";
|
| 74 | + private static final String VALUE_FIELD2 = "v_value2"; |
70 | 75 |
|
71 | 76 | static int interval;
|
72 | 77 | static int numBuckets;
|
@@ -1204,6 +1209,68 @@ public void testCheckIfTunableCanBeMinimized() {
|
1204 | 1209 | }
|
1205 | 1210 | }
|
1206 | 1211 |
|
| 1212 | + public void testPredictWithNonEmptyBuckets() throws Exception { |
| 1213 | + |
| 1214 | + createIndex("predict_non_empty"); |
| 1215 | + BulkRequestBuilder bulkBuilder = client().prepareBulk().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); |
| 1216 | + |
| 1217 | + for (int i = 0; i < 10; i++) { |
| 1218 | + bulkBuilder.add(client().prepareIndex("predict_non_empty", "type").setSource( |
| 1219 | + jsonBuilder().startObject().field(INTERVAL_FIELD, i) |
| 1220 | + .field(VALUE_FIELD, 10) |
| 1221 | + .field(VALUE_FIELD2, 10) |
| 1222 | + .endObject())); |
| 1223 | + } |
| 1224 | + for (int i = 10; i < 20; i++) { |
| 1225 | + // Extra so there is a bucket that only has second field |
| 1226 | + bulkBuilder.add(client().prepareIndex("predict_non_empty", "type").setSource( |
| 1227 | + jsonBuilder().startObject().field(INTERVAL_FIELD, i).field(VALUE_FIELD2, 10).endObject())); |
| 1228 | + } |
| 1229 | + |
| 1230 | + bulkBuilder.execute().actionGet(); |
| 1231 | + ensureSearchable(); |
| 1232 | + |
| 1233 | + SearchResponse response = client() |
| 1234 | + .prepareSearch("predict_non_empty") |
| 1235 | + .setTypes("type") |
| 1236 | + .addAggregation( |
| 1237 | + histogram("histo") |
| 1238 | + .field(INTERVAL_FIELD) |
| 1239 | + .interval(1) |
| 1240 | + .subAggregation(max("max").field(VALUE_FIELD)) |
| 1241 | + .subAggregation(max("max2").field(VALUE_FIELD2)) |
| 1242 | + .subAggregation( |
| 1243 | + movingAvg("movavg_values", "max") |
| 1244 | + .window(windowSize) |
| 1245 | + .modelBuilder(new SimpleModel.SimpleModelBuilder()) |
| 1246 | + .gapPolicy(BucketHelpers.GapPolicy.SKIP).predict(5))).execute().actionGet(); |
| 1247 | + |
| 1248 | + assertSearchResponse(response); |
| 1249 | + |
| 1250 | + Histogram histo = response.getAggregations().get("histo"); |
| 1251 | + assertThat(histo, notNullValue()); |
| 1252 | + assertThat(histo.getName(), equalTo("histo")); |
| 1253 | + List<? extends Bucket> buckets = histo.getBuckets(); |
| 1254 | + assertThat("Size of buckets array is not correct.", buckets.size(), equalTo(20)); |
| 1255 | + |
| 1256 | + SimpleValue current = buckets.get(0).getAggregations().get("movavg_values"); |
| 1257 | + assertThat(current, nullValue()); |
| 1258 | + |
| 1259 | + for (int i = 1; i < 20; i++) { |
| 1260 | + Bucket bucket = buckets.get(i); |
| 1261 | + assertThat(bucket, notNullValue()); |
| 1262 | + assertThat(bucket.getKey(), equalTo((double)i)); |
| 1263 | + assertThat(bucket.getDocCount(), equalTo(1L)); |
| 1264 | + SimpleValue movAvgAgg = bucket.getAggregations().get("movavg_values"); |
| 1265 | + if (i < 15) { |
| 1266 | + assertThat(movAvgAgg, notNullValue()); |
| 1267 | + assertThat(movAvgAgg.value(), equalTo(10d)); |
| 1268 | + } else { |
| 1269 | + assertThat(movAvgAgg, nullValue()); |
| 1270 | + } |
| 1271 | + } |
| 1272 | + } |
| 1273 | + |
1207 | 1274 | private void assertValidIterators(Iterator expectedBucketIter, Iterator expectedCountsIter, Iterator expectedValuesIter) {
|
1208 | 1275 | if (!expectedBucketIter.hasNext()) {
|
1209 | 1276 | fail("`expectedBucketIter` iterator ended before `actual` iterator, size mismatch");
|
|
0 commit comments