|
@@ -31,10 +31,12 @@ import org.elasticsearch.test.transport.AssertingLocalTransport;
|
|
|
import org.joda.time.DateTime;
|
|
|
import org.joda.time.DateTimeZone;
|
|
|
import org.junit.After;
|
|
|
+import org.junit.Before;
|
|
|
import org.junit.Test;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.util.List;
|
|
|
+import java.util.concurrent.ExecutionException;
|
|
|
|
|
|
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
|
|
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
|
@@ -43,14 +45,16 @@ import static org.hamcrest.Matchers.equalTo;
|
|
|
import static org.hamcrest.core.IsNull.notNullValue;
|
|
|
|
|
|
/**
|
|
|
- * The serialisation of pre and post offsets for the date histogram aggregation was corrected in version 1.4 to allow negative offsets and as such the
|
|
|
- * serialisation of negative offsets in these tests would break in pre 1.4 versions. These tests are separated from the other DateHistogramTests so the
|
|
|
+ * The serialisation of offsets for the date histogram aggregation was corrected in version 1.4 to allow negative offsets and as such the
|
|
|
+ * serialisation of negative offsets in these tests would break in pre 1.4 versions. These tests are separated from the other DateHistogramTests so the
|
|
|
* AssertingLocalTransport for these tests can be set to only use versions 1.4 onwards while keeping the other tests using all versions
|
|
|
*/
|
|
|
@ElasticsearchIntegrationTest.SuiteScopeTest
|
|
|
@ElasticsearchIntegrationTest.ClusterScope(scope=ElasticsearchIntegrationTest.Scope.SUITE)
|
|
|
public class DateHistogramOffsetTests extends ElasticsearchIntegrationTest {
|
|
|
|
|
|
+ private static final String DATE_FORMAT = "YY-MM-DD:hh-mm-ss";
|
|
|
+
|
|
|
private DateTime date(String date) {
|
|
|
return DateFieldMapper.Defaults.DATE_TIME_FORMATTER.parser().parseDateTime(date);
|
|
|
}
|
|
@@ -62,71 +66,36 @@ public class DateHistogramOffsetTests extends ElasticsearchIntegrationTest {
|
|
|
.put(AssertingLocalTransport.ASSERTING_TRANSPORT_MIN_VERSION_KEY, Version.V_1_4_0_Beta1).build();
|
|
|
}
|
|
|
|
|
|
+ @Before
|
|
|
+ public void beforeEachTest() throws IOException {
|
|
|
+ prepareCreate("idx2").addMapping("type", "date", "type=date").execute().actionGet();
|
|
|
+ }
|
|
|
+
|
|
|
@After
|
|
|
public void afterEachTest() throws IOException {
|
|
|
internalCluster().wipeIndices("idx2");
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void singleValue_WithPreOffset() throws Exception {
|
|
|
- prepareCreate("idx2").addMapping("type", "date", "type=date").execute().actionGet();
|
|
|
- IndexRequestBuilder[] reqs = new IndexRequestBuilder[5];
|
|
|
- DateTime date = date("2014-03-11T00:00:00+00:00");
|
|
|
- for (int i = 0; i < reqs.length; i++) {
|
|
|
- reqs[i] = client().prepareIndex("idx2", "type", "" + i).setSource(jsonBuilder().startObject().field("date", date).endObject());
|
|
|
- date = date.plusHours(1);
|
|
|
+ private void prepareIndex(DateTime date, int numHours, int stepSizeHours, int idxIdStart) throws IOException, InterruptedException, ExecutionException {
|
|
|
+ IndexRequestBuilder[] reqs = new IndexRequestBuilder[numHours];
|
|
|
+ for (int i = idxIdStart; i < idxIdStart + reqs.length; i++) {
|
|
|
+ reqs[i - idxIdStart] = client().prepareIndex("idx2", "type", "" + i).setSource(jsonBuilder().startObject().field("date", date).endObject());
|
|
|
+ date = date.plusHours(stepSizeHours);
|
|
|
}
|
|
|
indexRandom(true, reqs);
|
|
|
-
|
|
|
- SearchResponse response = client().prepareSearch("idx2")
|
|
|
- .setQuery(matchAllQuery())
|
|
|
- .addAggregation(dateHistogram("date_histo")
|
|
|
- .field("date")
|
|
|
- .preOffset("-2h")
|
|
|
- .interval(DateHistogramInterval.DAY)
|
|
|
- .format("yyyy-MM-dd"))
|
|
|
- .execute().actionGet();
|
|
|
-
|
|
|
- assertThat(response.getHits().getTotalHits(), equalTo(5l));
|
|
|
-
|
|
|
- Histogram histo = response.getAggregations().get("date_histo");
|
|
|
- List<? extends Histogram.Bucket> buckets = histo.getBuckets();
|
|
|
- assertThat(buckets.size(), equalTo(2));
|
|
|
-
|
|
|
- DateTime key = new DateTime(2014, 3, 10, 0, 0, DateTimeZone.UTC);
|
|
|
- Histogram.Bucket bucket = buckets.get(0);
|
|
|
- assertThat(bucket, notNullValue());
|
|
|
- assertThat(bucket.getKeyAsString(), equalTo("2014-03-10"));
|
|
|
- assertThat(((DateTime) bucket.getKey()), equalTo(key));
|
|
|
- assertThat(bucket.getDocCount(), equalTo(2l));
|
|
|
-
|
|
|
- key = new DateTime(2014, 3, 11, 0, 0, DateTimeZone.UTC);
|
|
|
- bucket = buckets.get(1);
|
|
|
- assertThat(bucket, notNullValue());
|
|
|
- assertThat(bucket.getKeyAsString(), equalTo("2014-03-11"));
|
|
|
- assertThat(((DateTime) bucket.getKey()), equalTo(key));
|
|
|
- assertThat(bucket.getDocCount(), equalTo(3l));
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void singleValue_WithPreOffset_MinDocCount() throws Exception {
|
|
|
- prepareCreate("idx2").addMapping("type", "date", "type=date").execute().actionGet();
|
|
|
- IndexRequestBuilder[] reqs = new IndexRequestBuilder[5];
|
|
|
- DateTime date = date("2014-03-11T00:00:00+00:00");
|
|
|
- for (int i = 0; i < reqs.length; i++) {
|
|
|
- reqs[i] = client().prepareIndex("idx2", "type", "" + i).setSource(jsonBuilder().startObject().field("date", date).endObject());
|
|
|
- date = date.plusHours(1);
|
|
|
- }
|
|
|
- indexRandom(true, reqs);
|
|
|
+ public void singleValue_WithPositiveOffset() throws Exception {
|
|
|
+ prepareIndex(date("2014-03-11T00:00:00+00:00"), 5, 1, 0);
|
|
|
|
|
|
SearchResponse response = client().prepareSearch("idx2")
|
|
|
.setQuery(matchAllQuery())
|
|
|
.addAggregation(dateHistogram("date_histo")
|
|
|
.field("date")
|
|
|
- .preOffset("-2h")
|
|
|
- .minDocCount(0)
|
|
|
- .interval(DateHistogramInterval.DAY)
|
|
|
- .format("yyyy-MM-dd"))
|
|
|
+ .offset("2h")
|
|
|
+ .format(DATE_FORMAT)
|
|
|
+ .interval(DateHistogramInterval.DAY))
|
|
|
.execute().actionGet();
|
|
|
|
|
|
assertThat(response.getHits().getTotalHits(), equalTo(5l));
|
|
@@ -135,39 +104,21 @@ public class DateHistogramOffsetTests extends ElasticsearchIntegrationTest {
|
|
|
List<? extends Histogram.Bucket> buckets = histo.getBuckets();
|
|
|
assertThat(buckets.size(), equalTo(2));
|
|
|
|
|
|
- DateTime key = new DateTime(2014, 3, 10, 0, 0, DateTimeZone.UTC);
|
|
|
- Histogram.Bucket bucket = buckets.get(0);
|
|
|
- assertThat(bucket, notNullValue());
|
|
|
- assertThat(bucket.getKeyAsString(), equalTo("2014-03-10"));
|
|
|
- assertThat(((DateTime) bucket.getKey()), equalTo(key));
|
|
|
- assertThat(bucket.getDocCount(), equalTo(2l));
|
|
|
-
|
|
|
- key = new DateTime(2014, 3, 11, 0, 0, DateTimeZone.UTC);
|
|
|
- bucket = buckets.get(1);
|
|
|
- assertThat(bucket, notNullValue());
|
|
|
- assertThat(bucket.getKeyAsString(), equalTo("2014-03-11"));
|
|
|
- assertThat(((DateTime) bucket.getKey()), equalTo(key));
|
|
|
- assertThat(bucket.getDocCount(), equalTo(3l));
|
|
|
+ checkBucketFor(buckets.get(0), new DateTime(2014, 3, 10, 2, 0, DateTimeZone.UTC), 2l);
|
|
|
+ checkBucketFor(buckets.get(1), new DateTime(2014, 3, 11, 2, 0, DateTimeZone.UTC), 3l);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void singleValue_WithPostOffset() throws Exception {
|
|
|
- prepareCreate("idx2").addMapping("type", "date", "type=date").execute().actionGet();
|
|
|
- IndexRequestBuilder[] reqs = new IndexRequestBuilder[5];
|
|
|
- DateTime date = date("2014-03-11T00:00:00+00:00");
|
|
|
- for (int i = 0; i < reqs.length; i++) {
|
|
|
- reqs[i] = client().prepareIndex("idx2", "type", "" + i).setSource(jsonBuilder().startObject().field("date", date).endObject());
|
|
|
- date = date.plusHours(6);
|
|
|
- }
|
|
|
- indexRandom(true, reqs);
|
|
|
+ public void singleValue_WithNegativeOffset() throws Exception {
|
|
|
+ prepareIndex(date("2014-03-11T00:00:00+00:00"), 5, -1, 0);
|
|
|
|
|
|
SearchResponse response = client().prepareSearch("idx2")
|
|
|
.setQuery(matchAllQuery())
|
|
|
.addAggregation(dateHistogram("date_histo")
|
|
|
.field("date")
|
|
|
- .postOffset("2d")
|
|
|
- .interval(DateHistogramInterval.DAY)
|
|
|
- .format("yyyy-MM-dd"))
|
|
|
+ .offset("-2h")
|
|
|
+ .format(DATE_FORMAT)
|
|
|
+ .interval(DateHistogramInterval.DAY))
|
|
|
.execute().actionGet();
|
|
|
|
|
|
assertThat(response.getHits().getTotalHits(), equalTo(5l));
|
|
@@ -176,60 +127,51 @@ public class DateHistogramOffsetTests extends ElasticsearchIntegrationTest {
|
|
|
List<? extends Histogram.Bucket> buckets = histo.getBuckets();
|
|
|
assertThat(buckets.size(), equalTo(2));
|
|
|
|
|
|
- DateTime key = new DateTime(2014, 3, 13, 0, 0, DateTimeZone.UTC);
|
|
|
- Histogram.Bucket bucket = buckets.get(0);
|
|
|
- assertThat(bucket, notNullValue());
|
|
|
- assertThat(bucket.getKeyAsString(), equalTo("2014-03-13"));
|
|
|
- assertThat(((DateTime) bucket.getKey()), equalTo(key));
|
|
|
- assertThat(bucket.getDocCount(), equalTo(4l));
|
|
|
-
|
|
|
- key = new DateTime(2014, 3, 14, 0, 0, DateTimeZone.UTC);
|
|
|
- bucket = buckets.get(1);
|
|
|
- assertThat(bucket, notNullValue());
|
|
|
- assertThat(bucket.getKeyAsString(), equalTo("2014-03-14"));
|
|
|
- assertThat(((DateTime) bucket.getKey()), equalTo(key));
|
|
|
- assertThat(bucket.getDocCount(), equalTo(1l));
|
|
|
+ checkBucketFor(buckets.get(0), new DateTime(2014, 3, 9, 22, 0, DateTimeZone.UTC), 2l);
|
|
|
+ checkBucketFor(buckets.get(1), new DateTime(2014, 3, 10, 22, 0, DateTimeZone.UTC), 3l);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Set offset so day buckets start at 6am. Index first 12 hours for two days, with one day gap.
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
@Test
|
|
|
- public void singleValue_WithPostOffset_MinDocCount() throws Exception {
|
|
|
- prepareCreate("idx2").addMapping("type", "date", "type=date").execute().actionGet();
|
|
|
- IndexRequestBuilder[] reqs = new IndexRequestBuilder[5];
|
|
|
- DateTime date = date("2014-03-11T00:00:00+00:00");
|
|
|
- for (int i = 0; i < reqs.length; i++) {
|
|
|
- reqs[i] = client().prepareIndex("idx2", "type", "" + i).setSource(jsonBuilder().startObject().field("date", date).endObject());
|
|
|
- date = date.plusHours(6);
|
|
|
- }
|
|
|
- indexRandom(true, reqs);
|
|
|
+ public void singleValue_WithOffset_MinDocCount() throws Exception {
|
|
|
+ prepareIndex(date("2014-03-11T00:00:00+00:00"), 12, 1, 0);
|
|
|
+ prepareIndex(date("2014-03-14T00:00:00+00:00"), 12, 1, 13);
|
|
|
|
|
|
SearchResponse response = client().prepareSearch("idx2")
|
|
|
.setQuery(matchAllQuery())
|
|
|
.addAggregation(dateHistogram("date_histo")
|
|
|
.field("date")
|
|
|
- .postOffset("2d")
|
|
|
+ .offset("6h")
|
|
|
.minDocCount(0)
|
|
|
- .interval(DateHistogramInterval.DAY)
|
|
|
- .format("yyyy-MM-dd"))
|
|
|
+ .format(DATE_FORMAT)
|
|
|
+ .interval(DateHistogramInterval.DAY))
|
|
|
.execute().actionGet();
|
|
|
|
|
|
- assertThat(response.getHits().getTotalHits(), equalTo(5l));
|
|
|
+ assertThat(response.getHits().getTotalHits(), equalTo(24l));
|
|
|
|
|
|
Histogram histo = response.getAggregations().get("date_histo");
|
|
|
List<? extends Histogram.Bucket> buckets = histo.getBuckets();
|
|
|
- assertThat(buckets.size(), equalTo(2));
|
|
|
+ assertThat(buckets.size(), equalTo(5));
|
|
|
|
|
|
- DateTime key = new DateTime(2014, 3, 13, 0, 0, DateTimeZone.UTC);
|
|
|
- Histogram.Bucket bucket = buckets.get(0);
|
|
|
- assertThat(bucket, notNullValue());
|
|
|
- assertThat(bucket.getKeyAsString(), equalTo("2014-03-13"));
|
|
|
- assertThat(((DateTime) bucket.getKey()), equalTo(key));
|
|
|
- assertThat(bucket.getDocCount(), equalTo(4l));
|
|
|
+ checkBucketFor(buckets.get(0), new DateTime(2014, 3, 10, 6, 0, DateTimeZone.UTC), 6L);
|
|
|
+ checkBucketFor(buckets.get(1), new DateTime(2014, 3, 11, 6, 0, DateTimeZone.UTC), 6L);
|
|
|
+ checkBucketFor(buckets.get(2), new DateTime(2014, 3, 12, 6, 0, DateTimeZone.UTC), 0L);
|
|
|
+ checkBucketFor(buckets.get(3), new DateTime(2014, 3, 13, 6, 0, DateTimeZone.UTC), 6L);
|
|
|
+ checkBucketFor(buckets.get(4), new DateTime(2014, 3, 14, 6, 0, DateTimeZone.UTC), 6L);
|
|
|
+ }
|
|
|
|
|
|
- key = new DateTime(2014, 3, 14, 0, 0, DateTimeZone.UTC);
|
|
|
- bucket = buckets.get(1);
|
|
|
+ /**
|
|
|
+ * @param bucket the bucket to check asssertions for
|
|
|
+ * @param key the expected key
|
|
|
+ * @param expectedSize the expected size of the bucket
|
|
|
+ */
|
|
|
+ private static void checkBucketFor(Histogram.Bucket bucket, DateTime key, long expectedSize) {
|
|
|
assertThat(bucket, notNullValue());
|
|
|
- assertThat(bucket.getKeyAsString(), equalTo("2014-03-14"));
|
|
|
+ assertThat(bucket.getKeyAsString(), equalTo(key.toString(DATE_FORMAT)));
|
|
|
assertThat(((DateTime) bucket.getKey()), equalTo(key));
|
|
|
- assertThat(bucket.getDocCount(), equalTo(1l));
|
|
|
+ assertThat(bucket.getDocCount(), equalTo(expectedSize));
|
|
|
}
|
|
|
}
|