|
31 | 31 | import org.apache.lucene.util.NumericUtils; |
32 | 32 | import org.elasticsearch.Version; |
33 | 33 | import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 34 | +import org.elasticsearch.common.CheckedConsumer; |
34 | 35 | import org.elasticsearch.common.settings.Settings; |
35 | 36 | import org.elasticsearch.index.IndexSettings; |
36 | 37 | import org.elasticsearch.index.mapper.MappedFieldType; |
37 | 38 | import org.elasticsearch.index.mapper.NumberFieldMapper; |
| 39 | +import org.elasticsearch.search.aggregations.AggregationBuilder; |
38 | 40 | import org.elasticsearch.search.aggregations.AggregationBuilders; |
39 | 41 | import org.elasticsearch.search.aggregations.AggregatorTestCase; |
40 | 42 | import org.elasticsearch.search.aggregations.bucket.terms.InternalTerms; |
| 43 | +import org.elasticsearch.search.aggregations.bucket.terms.LongTerms; |
41 | 44 | import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; |
42 | 45 | import org.elasticsearch.search.aggregations.metrics.InternalStats; |
43 | 46 | import org.elasticsearch.search.aggregations.metrics.StatsAggregationBuilder; |
|
51 | 54 | import java.util.Map; |
52 | 55 | import java.util.function.Consumer; |
53 | 56 |
|
| 57 | +import static java.util.stream.Collectors.toList; |
| 58 | +import static org.hamcrest.Matchers.containsString; |
| 59 | +import static org.hamcrest.Matchers.equalTo; |
| 60 | + |
54 | 61 | public class VariableWidthHistogramAggregatorTests extends AggregatorTestCase { |
55 | 62 |
|
56 | 63 | private static final String NUMERIC_FIELD = "numeric"; |
57 | 64 |
|
58 | 65 | private static final Query DEFAULT_QUERY = new MatchAllDocsQuery(); |
59 | | - private VariableWidthHistogramAggregationBuilder aggregationBuilder; |
60 | 66 |
|
61 | 67 | public void testNoDocs() throws Exception{ |
62 | 68 | final List<Number> dataset = Arrays.asList(); |
@@ -424,6 +430,44 @@ public void testMultipleSegments() throws IOException{ |
424 | 430 |
|
425 | 431 | } |
426 | 432 |
|
| 433 | + public void testAsSubAggregation() throws IOException { |
| 434 | + AggregationBuilder builder = new TermsAggregationBuilder("t").field("t") |
| 435 | + .subAggregation(new VariableWidthHistogramAggregationBuilder("v").field("v").setNumBuckets(2)); |
| 436 | + CheckedConsumer<RandomIndexWriter, IOException> buildIndex = iw -> { |
| 437 | + iw.addDocument(List.of(new SortedNumericDocValuesField("t", 1), new SortedNumericDocValuesField("v", 1))); |
| 438 | + iw.addDocument(List.of(new SortedNumericDocValuesField("t", 1), new SortedNumericDocValuesField("v", 10))); |
| 439 | + iw.addDocument(List.of(new SortedNumericDocValuesField("t", 1), new SortedNumericDocValuesField("v", 11))); |
| 440 | + |
| 441 | + iw.addDocument(List.of(new SortedNumericDocValuesField("t", 2), new SortedNumericDocValuesField("v", 20))); |
| 442 | + iw.addDocument(List.of(new SortedNumericDocValuesField("t", 2), new SortedNumericDocValuesField("v", 30))); |
| 443 | + }; |
| 444 | + Consumer<LongTerms> verify = terms -> { |
| 445 | + /* |
| 446 | + * This is what the result should be but it never gets called because of |
| 447 | + * the explicit check. We do expect to remove the check in the future, |
| 448 | + * thus, this stays. |
| 449 | + */ |
| 450 | + LongTerms.Bucket t1 = terms.getBucketByKey("1"); |
| 451 | + InternalVariableWidthHistogram v1 = t1.getAggregations().get("v"); |
| 452 | + assertThat( |
| 453 | + v1.getBuckets().stream().map(InternalVariableWidthHistogram.Bucket::centroid).collect(toList()), |
| 454 | + equalTo(List.of(1.0, 10.5)) |
| 455 | + ); |
| 456 | + |
| 457 | + LongTerms.Bucket t2 = terms.getBucketByKey("1"); |
| 458 | + InternalVariableWidthHistogram v2 = t2.getAggregations().get("v"); |
| 459 | + assertThat( |
| 460 | + v2.getBuckets().stream().map(InternalVariableWidthHistogram.Bucket::centroid).collect(toList()), |
| 461 | + equalTo(List.of(20.0, 30)) |
| 462 | + ); |
| 463 | + }; |
| 464 | + Exception e = expectThrows( |
| 465 | + IllegalArgumentException.class, |
| 466 | + () -> testCase(builder, DEFAULT_QUERY, buildIndex, verify, longField("t"), longField("v")) |
| 467 | + ); |
| 468 | + assertThat(e.getMessage(), containsString("cannot be nested")); |
| 469 | + } |
| 470 | + |
427 | 471 |
|
428 | 472 | private void testSearchCase(final Query query, final List<Number> dataset, boolean multipleSegments, |
429 | 473 | final Consumer<VariableWidthHistogramAggregationBuilder> configure, |
|
0 commit comments