Skip to content

Commit

Permalink
Add test for bucket sorting to rate agg
Browse files Browse the repository at this point in the history
In the initial implementation I missed the the test to check if bucket
sorting works correctly. This commit adds this test.

Relates to elastic#60674
  • Loading branch information
imotov committed Nov 6, 2020
1 parent 6a92b3e commit 76997e7
Showing 1 changed file with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<InternalDateHistogram>) 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(),
Expand Down

0 comments on commit 76997e7

Please sign in to comment.