|
| 1 | +package org.elasticsearch.xpack.transform.integration.continuous; |
| 2 | + |
| 3 | +import org.elasticsearch.action.search.SearchRequest; |
| 4 | +import org.elasticsearch.action.search.SearchResponse; |
| 5 | +import org.elasticsearch.action.support.IndicesOptions; |
| 6 | +import org.elasticsearch.client.transform.transforms.DestConfig; |
| 7 | +import org.elasticsearch.client.transform.transforms.SourceConfig; |
| 8 | +import org.elasticsearch.client.transform.transforms.TransformConfig; |
| 9 | +import org.elasticsearch.client.transform.transforms.pivot.DateHistogramGroupSource; |
| 10 | +import org.elasticsearch.client.transform.transforms.pivot.GroupConfig; |
| 11 | +import org.elasticsearch.client.transform.transforms.pivot.PivotConfig; |
| 12 | +import org.elasticsearch.client.transform.transforms.pivot.TermsGroupSource; |
| 13 | +import org.elasticsearch.common.xcontent.support.XContentMapValues; |
| 14 | +import org.elasticsearch.search.SearchHit; |
| 15 | +import org.elasticsearch.search.aggregations.AggregatorFactories; |
| 16 | +import org.elasticsearch.search.aggregations.BucketOrder; |
| 17 | +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder; |
| 18 | +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; |
| 19 | +import org.elasticsearch.search.aggregations.bucket.histogram.Histogram; |
| 20 | +import org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket; |
| 21 | +import org.elasticsearch.search.aggregations.bucket.terms.Terms; |
| 22 | +import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; |
| 23 | +import org.elasticsearch.search.builder.SearchSourceBuilder; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.time.ZonedDateTime; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.Iterator; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | + |
| 33 | +import static org.hamcrest.Matchers.equalTo; |
| 34 | +import static org.hamcrest.Matchers.is; |
| 35 | +import static org.hamcrest.Matchers.lessThanOrEqualTo; |
| 36 | + |
| 37 | +/** |
| 38 | + * Testcase for date histogram group_by on _different_ fields than used for sync |
| 39 | + */ |
| 40 | +public class DateHistogramGroupByOtherTimeFieldIT extends ContinuousTestCase { |
| 41 | + private static final String NAME = "continuous-date-histogram-pivot-other-timefield-test"; |
| 42 | + |
| 43 | + private final boolean addGroupByTerms; |
| 44 | + |
| 45 | + public DateHistogramGroupByOtherTimeFieldIT() { |
| 46 | + addGroupByTerms = randomBoolean(); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public TransformConfig createConfig() { |
| 51 | + TransformConfig.Builder transformConfigBuilder = new TransformConfig.Builder(); |
| 52 | + addCommonBuilderParameters(transformConfigBuilder); |
| 53 | + transformConfigBuilder.setSource(new SourceConfig(CONTINUOUS_EVENTS_SOURCE_INDEX)); |
| 54 | + transformConfigBuilder.setDest(new DestConfig(NAME, INGEST_PIPELINE)); |
| 55 | + transformConfigBuilder.setId(NAME); |
| 56 | + PivotConfig.Builder pivotConfigBuilder = new PivotConfig.Builder(); |
| 57 | + GroupConfig.Builder groups = new GroupConfig.Builder().groupBy( |
| 58 | + "second", |
| 59 | + new DateHistogramGroupSource.Builder().setField("metric-timestamp") |
| 60 | + .setInterval(new DateHistogramGroupSource.FixedInterval(DateHistogramInterval.SECOND)) |
| 61 | + .build() |
| 62 | + ); |
| 63 | + if (addGroupByTerms) { |
| 64 | + groups.groupBy("event", new TermsGroupSource.Builder().setField("event").build()); |
| 65 | + } |
| 66 | + pivotConfigBuilder.setGroups(groups.build()); |
| 67 | + AggregatorFactories.Builder aggregations = new AggregatorFactories.Builder(); |
| 68 | + addCommonAggregations(aggregations); |
| 69 | + |
| 70 | + pivotConfigBuilder.setAggregations(aggregations); |
| 71 | + transformConfigBuilder.setPivotConfig(pivotConfigBuilder.build()); |
| 72 | + return transformConfigBuilder.build(); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public String getName() { |
| 77 | + return NAME; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void testIteration(int iteration) throws IOException { |
| 82 | + SearchRequest searchRequestSource = new SearchRequest(CONTINUOUS_EVENTS_SOURCE_INDEX).allowPartialSearchResults(false) |
| 83 | + .indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN); |
| 84 | + SearchSourceBuilder sourceBuilderSource = new SearchSourceBuilder().size(0); |
| 85 | + DateHistogramAggregationBuilder bySecond = new DateHistogramAggregationBuilder("second").field("metric-timestamp") |
| 86 | + .fixedInterval(DateHistogramInterval.SECOND) |
| 87 | + .order(BucketOrder.key(true)); |
| 88 | + |
| 89 | + if (addGroupByTerms) { |
| 90 | + TermsAggregationBuilder terms = new TermsAggregationBuilder("event").size(1000).field("event").order(BucketOrder.key(true)); |
| 91 | + bySecond.subAggregation(terms); |
| 92 | + } |
| 93 | + sourceBuilderSource.aggregation(bySecond); |
| 94 | + searchRequestSource.source(sourceBuilderSource); |
| 95 | + SearchResponse responseSource = search(searchRequestSource); |
| 96 | + |
| 97 | + SearchRequest searchRequestDest = new SearchRequest(NAME).allowPartialSearchResults(false) |
| 98 | + .indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN); |
| 99 | + SearchSourceBuilder sourceBuilderDest = new SearchSourceBuilder().size(10000).sort("second"); |
| 100 | + if (addGroupByTerms) { |
| 101 | + sourceBuilderDest.sort("event"); |
| 102 | + } |
| 103 | + |
| 104 | + searchRequestDest.source(sourceBuilderDest); |
| 105 | + SearchResponse responseDest = search(searchRequestDest); |
| 106 | + |
| 107 | + if (addGroupByTerms) { |
| 108 | + assertResultsGroupByDateHistogramAndTerms(iteration, responseSource, responseDest); |
| 109 | + } else { |
| 110 | + assertResultsGroupByDateHistogram(iteration, responseSource, responseDest); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private void assertResultsGroupByDateHistogram(int iteration, SearchResponse responseSource, SearchResponse responseDest) { |
| 115 | + List<? extends Bucket> buckets = ((Histogram) responseSource.getAggregations().get("second")).getBuckets(); |
| 116 | + Iterator<? extends Bucket> sourceIterator = buckets.iterator(); |
| 117 | + Iterator<SearchHit> destIterator = responseDest.getHits().iterator(); |
| 118 | + |
| 119 | + while (sourceIterator.hasNext() && destIterator.hasNext()) { |
| 120 | + Bucket bucket = sourceIterator.next(); |
| 121 | + SearchHit searchHit = destIterator.next(); |
| 122 | + Map<String, Object> source = searchHit.getSourceAsMap(); |
| 123 | + |
| 124 | + Long transformBucketKey = (Long) XContentMapValues.extractValue("second", source); |
| 125 | + |
| 126 | + // aggs return buckets with 0 doc_count while composite aggs skip over them |
| 127 | + while (bucket.getDocCount() == 0L) { |
| 128 | + assertTrue(sourceIterator.hasNext()); |
| 129 | + bucket = sourceIterator.next(); |
| 130 | + } |
| 131 | + long bucketKey = ((ZonedDateTime) bucket.getKey()).toEpochSecond() * 1000; |
| 132 | + |
| 133 | + // test correctness, the results from the aggregation and the results from the transform should be the same |
| 134 | + assertThat( |
| 135 | + "Buckets did not match, source: " + source + ", expected: " + bucketKey + ", iteration: " + iteration, |
| 136 | + transformBucketKey, |
| 137 | + equalTo(bucketKey) |
| 138 | + ); |
| 139 | + assertThat( |
| 140 | + "Doc count did not match, source: " + source + ", expected: " + bucket.getDocCount() + ", iteration: " + iteration, |
| 141 | + XContentMapValues.extractValue("count", source), |
| 142 | + equalTo(Double.valueOf(bucket.getDocCount())) |
| 143 | + ); |
| 144 | + |
| 145 | + // transform should only rewrite documents that require it |
| 146 | + assertThat( |
| 147 | + "Ingest run: " |
| 148 | + + XContentMapValues.extractValue(INGEST_RUN_FIELD, source) |
| 149 | + + " did not match max run: " |
| 150 | + + XContentMapValues.extractValue(MAX_RUN_FIELD, source) |
| 151 | + + ", iteration: " |
| 152 | + + iteration, |
| 153 | + // we use a fixed_interval of `1s`, the transform runs every `1s`, a bucket might be recalculated at the next run |
| 154 | + // but should NOT be recalculated for the 2nd/3rd/... run |
| 155 | + Double.valueOf((Integer) XContentMapValues.extractValue(INGEST_RUN_FIELD, source)) - (Double) XContentMapValues |
| 156 | + .extractValue(MAX_RUN_FIELD, source), |
| 157 | + is(lessThanOrEqualTo(1.0)) |
| 158 | + ); |
| 159 | + |
| 160 | + } |
| 161 | + assertFalse(sourceIterator.hasNext()); |
| 162 | + assertFalse(destIterator.hasNext()); |
| 163 | + } |
| 164 | + |
| 165 | + private void assertResultsGroupByDateHistogramAndTerms(int iteration, SearchResponse responseSource, SearchResponse responseDest) { |
| 166 | + List<? extends Bucket> buckets = ((Histogram) responseSource.getAggregations().get("second")).getBuckets(); |
| 167 | + |
| 168 | + List<Map<String, Object>> flattenedBuckets = new ArrayList<>(); |
| 169 | + for (Bucket b : buckets) { |
| 170 | + if (b.getDocCount() == 0) { |
| 171 | + continue; |
| 172 | + } |
| 173 | + long second = ((ZonedDateTime) b.getKey()).toEpochSecond() * 1000; |
| 174 | + List<? extends Terms.Bucket> terms = ((Terms) b.getAggregations().get("event")).getBuckets(); |
| 175 | + for (Terms.Bucket t : terms) { |
| 176 | + flattenedBuckets.add(flattenedResult(second, t.getKeyAsString(), t.getDocCount())); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + Iterator<Map<String, Object>> sourceIterator = flattenedBuckets.iterator(); |
| 181 | + Iterator<SearchHit> destIterator = responseDest.getHits().iterator(); |
| 182 | + |
| 183 | + while (sourceIterator.hasNext() && destIterator.hasNext()) { |
| 184 | + Map<String, Object> bucket = sourceIterator.next(); |
| 185 | + |
| 186 | + SearchHit searchHit = destIterator.next(); |
| 187 | + Map<String, Object> source = searchHit.getSourceAsMap(); |
| 188 | + |
| 189 | + Long transformBucketKey = (Long) XContentMapValues.extractValue("second", source); |
| 190 | + |
| 191 | + // test correctness, the results from the aggregation and the results from the transform should be the same |
| 192 | + assertThat( |
| 193 | + "Buckets did not match, source: " + source + ", expected: " + bucket.get("second") + ", iteration: " + iteration, |
| 194 | + transformBucketKey, |
| 195 | + equalTo(bucket.get("second")) |
| 196 | + ); |
| 197 | + assertThat( |
| 198 | + "Doc count did not match, source: " + source + ", expected: " + bucket.get("count") + ", iteration: " + iteration, |
| 199 | + XContentMapValues.extractValue("count", source), |
| 200 | + equalTo(Double.valueOf(((Long) bucket.get("count")))) |
| 201 | + ); |
| 202 | + assertThat( |
| 203 | + "Term did not match, source: " + source + ", expected: " + bucket.get("event") + ", iteration: " + iteration, |
| 204 | + XContentMapValues.extractValue("event", source), |
| 205 | + equalTo(bucket.get("event")) |
| 206 | + ); |
| 207 | + |
| 208 | + // transform should only rewrite documents that require it |
| 209 | + assertThat( |
| 210 | + "Ingest run: " |
| 211 | + + XContentMapValues.extractValue(INGEST_RUN_FIELD, source) |
| 212 | + + " did not match max run: " |
| 213 | + + XContentMapValues.extractValue(MAX_RUN_FIELD, source) |
| 214 | + + ", iteration: " |
| 215 | + + iteration, |
| 216 | + // we use a fixed_interval of `1s`, the transform runs every `1s`, a bucket might be recalculated at the next run |
| 217 | + // but should NOT be recalculated for the 2nd/3rd/... run |
| 218 | + Double.valueOf((Integer) XContentMapValues.extractValue(INGEST_RUN_FIELD, source)) - (Double) XContentMapValues |
| 219 | + .extractValue(MAX_RUN_FIELD, source), |
| 220 | + is(lessThanOrEqualTo(1.0)) |
| 221 | + ); |
| 222 | + } |
| 223 | + assertFalse(sourceIterator.hasNext()); |
| 224 | + assertFalse(destIterator.hasNext()); |
| 225 | + } |
| 226 | + |
| 227 | + private static Map<String, Object> flattenedResult(long second, String event, long count) { |
| 228 | + Map<String, Object> doc = new HashMap<>(); |
| 229 | + doc.put("second", second); |
| 230 | + doc.put("event", event); |
| 231 | + doc.put("count", count); |
| 232 | + return doc; |
| 233 | + } |
| 234 | +} |
0 commit comments