From 76997e76ad6b4e3c5748b550b36d0d1f239b940d Mon Sep 17 00:00:00 2001 From: Igor Motov Date: Fri, 6 Nov 2020 14:44:14 -0500 Subject: [PATCH] Add test for bucket sorting to rate agg In the initial implementation I missed the the test to check if bucket sorting works correctly. This commit adds this test. Relates to #60674 --- .../analytics/rate/RateAggregatorTests.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/rate/RateAggregatorTests.java b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/rate/RateAggregatorTests.java index 2878f41da6efe..d6d649b34e4bb 100644 --- a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/rate/RateAggregatorTests.java +++ b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/rate/RateAggregatorTests.java @@ -49,6 +49,7 @@ import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.ScriptType; import org.elasticsearch.search.aggregations.AggregatorTestCase; +import org.elasticsearch.search.aggregations.BucketOrder; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import org.elasticsearch.search.aggregations.bucket.histogram.InternalDateHistogram; @@ -354,6 +355,80 @@ public void testKeywordSandwich() throws IOException { }, dateType, numType, keywordType); } + public void testKeywordSandwichWithSorting() throws IOException { + MappedFieldType numType = new NumberFieldMapper.NumberFieldType("val", NumberFieldMapper.NumberType.INTEGER); + MappedFieldType dateType = dateFieldType(DATE_FIELD); + MappedFieldType keywordType = new KeywordFieldMapper.KeywordFieldType("term"); + RateAggregationBuilder rateAggregationBuilder = new RateAggregationBuilder("my_rate").rateUnit("week").field("val"); + boolean useSum = randomBoolean(); + if (useSum) { + if (randomBoolean()) { + rateAggregationBuilder.rateMode("sum"); + } + } else { + rateAggregationBuilder.rateMode("value_count"); + } + TermsAggregationBuilder termsAggregationBuilder = new TermsAggregationBuilder("my_term").field("term") + .order(BucketOrder.aggregation("my_rate", false)) + .subAggregation(rateAggregationBuilder); + DateHistogramAggregationBuilder dateHistogramAggregationBuilder = new DateHistogramAggregationBuilder("my_date").field(DATE_FIELD) + .calendarInterval(new DateHistogramInterval("week")) + .subAggregation(termsAggregationBuilder); + + testCase(dateHistogramAggregationBuilder, new MatchAllDocsQuery(), iw -> { + iw.addDocument( + doc("2020-11-02T01:07:45", new NumericDocValuesField("val", 1), new SortedSetDocValuesField("term", new BytesRef("a"))) + ); + iw.addDocument( + doc("2020-11-03T01:07:45", new NumericDocValuesField("val", 2), new SortedSetDocValuesField("term", new BytesRef("a"))) + ); + iw.addDocument( + doc("2020-11-04T03:43:34", new NumericDocValuesField("val", 4), new SortedSetDocValuesField("term", new BytesRef("b"))) + ); + iw.addDocument( + doc("2020-11-09T03:43:34", new NumericDocValuesField("val", 30), new SortedSetDocValuesField("term", new BytesRef("a"))) + ); + iw.addDocument( + doc("2020-11-10T03:43:34", new NumericDocValuesField("val", 4), new SortedSetDocValuesField("term", new BytesRef("b"))) + ); + iw.addDocument( + doc("2020-11-11T03:43:34", new NumericDocValuesField("val", 4), new SortedSetDocValuesField("term", new BytesRef("b"))) + ); + }, (Consumer) dh -> { + assertThat(dh.getBuckets(), hasSize(2)); + if (useSum) { + StringTerms st1 = (StringTerms) dh.getBuckets().get(0).getAggregations().asList().get(0); + assertThat(st1.getBuckets(), hasSize(2)); + assertThat(st1.getBuckets().get(0).getKeyAsString(), equalTo("b")); + assertThat(((InternalRate) st1.getBuckets().get(0).getAggregations().asList().get(0)).value(), closeTo(4.0, 0.000001)); + assertThat(st1.getBuckets().get(1).getKeyAsString(), equalTo("a")); + assertThat(((InternalRate) st1.getBuckets().get(1).getAggregations().asList().get(0)).value(), closeTo(3.0, 0.000001)); + + StringTerms st2 = (StringTerms) dh.getBuckets().get(1).getAggregations().asList().get(0); + assertThat(st2.getBuckets(), hasSize(2)); + assertThat(st2.getBuckets().get(0).getKeyAsString(), equalTo("a")); + assertThat(((InternalRate) st2.getBuckets().get(0).getAggregations().asList().get(0)).value(), closeTo(30.0, 0.000001)); + assertThat(st2.getBuckets().get(1).getKeyAsString(), equalTo("b")); + assertThat(((InternalRate) st2.getBuckets().get(1).getAggregations().asList().get(0)).value(), closeTo(8.0, 0.000001)); + } else { + StringTerms st1 = (StringTerms) dh.getBuckets().get(0).getAggregations().asList().get(0); + assertThat(st1.getBuckets(), hasSize(2)); + assertThat(st1.getBuckets().get(0).getKeyAsString(), equalTo("a")); + assertThat(((InternalRate) st1.getBuckets().get(0).getAggregations().asList().get(0)).value(), closeTo(2.0, 0.000001)); + assertThat(st1.getBuckets().get(1).getKeyAsString(), equalTo("b")); + assertThat(((InternalRate) st1.getBuckets().get(1).getAggregations().asList().get(0)).value(), closeTo(1.0, 0.000001)); + + StringTerms st2 = (StringTerms) dh.getBuckets().get(1).getAggregations().asList().get(0); + assertThat(st2.getBuckets(), hasSize(2)); + assertThat(st2.getBuckets().get(0).getKeyAsString(), equalTo("b")); + assertThat(((InternalRate) st2.getBuckets().get(0).getAggregations().asList().get(0)).value(), closeTo(2.0, 0.000001)); + assertThat(st2.getBuckets().get(1).getKeyAsString(), equalTo("a")); + assertThat(((InternalRate) st2.getBuckets().get(1).getAggregations().asList().get(0)).value(), closeTo(1.0, 0.000001)); + + } + }, dateType, numType, keywordType); + } + public void testScriptMonthToDay() throws IOException { testCase( new MatchAllDocsQuery(),