|
@@ -6,6 +6,22 @@
|
|
|
|
|
|
package org.elasticsearch.xpack.analytics.rate;
|
|
|
|
|
|
+import static org.elasticsearch.xpack.analytics.AnalyticsTestsUtils.histogramFieldDocValues;
|
|
|
+import static org.hamcrest.Matchers.closeTo;
|
|
|
+import static org.hamcrest.Matchers.equalTo;
|
|
|
+import static org.hamcrest.Matchers.hasSize;
|
|
|
+import static org.hamcrest.Matchers.instanceOf;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.function.Consumer;
|
|
|
+import java.util.function.Function;
|
|
|
+
|
|
|
import org.apache.lucene.document.Field;
|
|
|
import org.apache.lucene.document.NumericDocValuesField;
|
|
|
import org.apache.lucene.document.SortedNumericDocValuesField;
|
|
@@ -40,21 +56,7 @@ import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
|
|
|
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
|
|
|
import org.elasticsearch.search.lookup.LeafDocLookup;
|
|
|
import org.elasticsearch.xpack.analytics.AnalyticsPlugin;
|
|
|
-
|
|
|
-import java.io.IOException;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.function.Consumer;
|
|
|
-import java.util.function.Function;
|
|
|
-
|
|
|
-import static org.hamcrest.Matchers.closeTo;
|
|
|
-import static org.hamcrest.Matchers.equalTo;
|
|
|
-import static org.hamcrest.Matchers.hasSize;
|
|
|
-import static org.hamcrest.Matchers.instanceOf;
|
|
|
+import org.elasticsearch.xpack.analytics.mapper.HistogramFieldMapper;
|
|
|
|
|
|
public class RateAggregatorTests extends AggregatorTestCase {
|
|
|
|
|
@@ -386,6 +388,67 @@ public class RateAggregatorTests extends AggregatorTestCase {
|
|
|
}, dateType, numType);
|
|
|
}
|
|
|
|
|
|
+ public void testHistogramFieldMonthToMonth() throws IOException {
|
|
|
+ MappedFieldType histType = new HistogramFieldMapper.HistogramFieldType("val", Collections.emptyMap());
|
|
|
+ MappedFieldType dateType = dateFieldType(DATE_FIELD);
|
|
|
+ RateAggregationBuilder rateAggregationBuilder = new RateAggregationBuilder("my_rate").rateUnit("month").field("val");
|
|
|
+
|
|
|
+ DateHistogramAggregationBuilder dateHistogramAggregationBuilder = new DateHistogramAggregationBuilder("my_date").field(DATE_FIELD)
|
|
|
+ .calendarInterval(new DateHistogramInterval("month"))
|
|
|
+ .subAggregation(rateAggregationBuilder);
|
|
|
+
|
|
|
+ testCase(dateHistogramAggregationBuilder, new MatchAllDocsQuery(), iw -> {
|
|
|
+ iw.addDocument(doc("2010-03-01T00:00:00", histogramFieldDocValues("val", new double[] { 1, 2 })));
|
|
|
+ iw.addDocument(doc("2010-04-01T00:00:00", histogramFieldDocValues("val", new double[] { 3, 4 })));
|
|
|
+ }, (Consumer<InternalDateHistogram>) dh -> {
|
|
|
+ assertThat(dh.getBuckets(), hasSize(2));
|
|
|
+ assertThat(((InternalRate) dh.getBuckets().get(0).getAggregations().asList().get(0)).getValue(), closeTo(3.0, 0.000001));
|
|
|
+ assertThat(((InternalRate) dh.getBuckets().get(1).getAggregations().asList().get(0)).getValue(), closeTo(7.0, 0.000001));
|
|
|
+ }, dateType, histType);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testHistogramFieldMonthToYear() throws IOException {
|
|
|
+ MappedFieldType histType = new HistogramFieldMapper.HistogramFieldType("val", Collections.emptyMap());
|
|
|
+ MappedFieldType dateType = dateFieldType(DATE_FIELD);
|
|
|
+ RateAggregationBuilder rateAggregationBuilder = new RateAggregationBuilder("my_rate").rateUnit("month").field("val");
|
|
|
+
|
|
|
+ DateHistogramAggregationBuilder dateHistogramAggregationBuilder = new DateHistogramAggregationBuilder("my_date").field(DATE_FIELD)
|
|
|
+ .calendarInterval(new DateHistogramInterval("year"))
|
|
|
+ .subAggregation(rateAggregationBuilder);
|
|
|
+
|
|
|
+ testCase(dateHistogramAggregationBuilder, new MatchAllDocsQuery(), iw -> {
|
|
|
+ iw.addDocument(doc("2010-03-01T00:00:00", histogramFieldDocValues("val", new double[] { 1, 2 })));
|
|
|
+ iw.addDocument(doc("2010-04-01T00:00:00", histogramFieldDocValues("val", new double[] { 3, 4 })));
|
|
|
+ }, (Consumer<InternalDateHistogram>) dh -> {
|
|
|
+ assertThat(dh.getBuckets(), hasSize(1));
|
|
|
+ assertThat(((InternalRate) dh.getBuckets().get(0).getAggregations().asList().get(0)).getValue(), closeTo(10.0 / 12, 0.000001));
|
|
|
+ }, dateType, histType);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testFilterWithHistogramField() throws IOException {
|
|
|
+ MappedFieldType histType = new HistogramFieldMapper.HistogramFieldType("val", Collections.emptyMap());
|
|
|
+ MappedFieldType dateType = dateFieldType(DATE_FIELD);
|
|
|
+ MappedFieldType keywordType = new KeywordFieldMapper.KeywordFieldType("term");
|
|
|
+ RateAggregationBuilder rateAggregationBuilder = new RateAggregationBuilder("my_rate").rateUnit("month").field("val");
|
|
|
+
|
|
|
+ DateHistogramAggregationBuilder dateHistogramAggregationBuilder = new DateHistogramAggregationBuilder("my_date").field(DATE_FIELD)
|
|
|
+ .calendarInterval(new DateHistogramInterval("month"))
|
|
|
+ .subAggregation(rateAggregationBuilder);
|
|
|
+
|
|
|
+ testCase(dateHistogramAggregationBuilder, new TermQuery(new Term("term", "a")), iw -> {
|
|
|
+ iw.addDocument(doc("2010-03-01T00:00:00", histogramFieldDocValues("val", new double[] { 1, 2 }),
|
|
|
+ new StringField("term", "a", Field.Store.NO)));
|
|
|
+ iw.addDocument(doc("2010-04-01T00:00:00", histogramFieldDocValues("val", new double[] { 3 }),
|
|
|
+ new StringField("term", "a", Field.Store.NO)));
|
|
|
+ iw.addDocument(doc("2010-04-01T00:00:00", histogramFieldDocValues("val", new double[] { 4 }),
|
|
|
+ new StringField("term", "b", Field.Store.NO)));
|
|
|
+ }, (Consumer<InternalDateHistogram>) dh -> {
|
|
|
+ assertThat(dh.getBuckets(), hasSize(2));
|
|
|
+ assertThat(((InternalRate) dh.getBuckets().get(0).getAggregations().asList().get(0)).value(), closeTo(3.0, 0.000001));
|
|
|
+ assertThat(((InternalRate) dh.getBuckets().get(1).getAggregations().asList().get(0)).value(), closeTo(3.0, 0.000001));
|
|
|
+ }, dateType, histType, keywordType);
|
|
|
+ }
|
|
|
+
|
|
|
private void testCase(
|
|
|
Query query,
|
|
|
String interval,
|