Aggregations: DateHistogramBuilder accepts String preOffset and postOffset

This is what DateHistogramParser expects so will enable the builder to build valid requests using these variables.
Also added tests for preOffset and postOffset since these tests did not exist

Closes #5586
This commit is contained in:
Colin Goodheart-Smithe 2014-07-10 16:11:21 +01:00
parent 0a988ad8f7
commit 0e5f9898d1
2 changed files with 76 additions and 6 deletions

View File

@ -41,8 +41,8 @@ public class DateHistogramBuilder extends ValuesSourceAggregationBuilder<DateHis
private String postZone;
private boolean preZoneAdjustLargeInterval;
private String format;
long preOffset = 0;
long postOffset = 0;
private String preOffset;
private String postOffset;
float factor = 1.0f;
public DateHistogramBuilder(String name) {
@ -84,12 +84,12 @@ public class DateHistogramBuilder extends ValuesSourceAggregationBuilder<DateHis
return this;
}
public DateHistogramBuilder preOffset(long preOffset) {
public DateHistogramBuilder preOffset(String preOffset) {
this.preOffset = preOffset;
return this;
}
public DateHistogramBuilder postOffset(long postOffset) {
public DateHistogramBuilder postOffset(String postOffset) {
this.postOffset = postOffset;
return this;
}
@ -153,11 +153,11 @@ public class DateHistogramBuilder extends ValuesSourceAggregationBuilder<DateHis
builder.field("pre_zone_adjust_large_interval", true);
}
if (preOffset != 0) {
if (preOffset != null) {
builder.field("pre_offset", preOffset);
}
if (postOffset != 0) {
if (postOffset != null) {
builder.field("post_offset", postOffset);
}

View File

@ -1051,6 +1051,76 @@ public class DateHistogramTests extends ElasticsearchIntegrationTest {
assertThat(bucket.getDocCount(), equalTo(3l));
}
@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);
}
indexRandom(true, reqs);
SearchResponse response = client().prepareSearch("idx2")
.setQuery(matchAllQuery())
.addAggregation(dateHistogram("date_histo")
.field("date")
.preOffset("-2h")
.interval(DateHistogram.Interval.DAY)
.format("yyyy-MM-dd"))
.execute().actionGet();
assertThat(response.getHits().getTotalHits(), equalTo(5l));
DateHistogram histo = response.getAggregations().get("date_histo");
Collection<? extends DateHistogram.Bucket> buckets = histo.getBuckets();
assertThat(buckets.size(), equalTo(2));
DateHistogram.Bucket bucket = histo.getBucketByKey("2014-03-10");
assertThat(bucket, Matchers.notNullValue());
assertThat(bucket.getDocCount(), equalTo(2l));
bucket = histo.getBucketByKey("2014-03-11");
assertThat(bucket, Matchers.notNullValue());
assertThat(bucket.getDocCount(), equalTo(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);
SearchResponse response = client().prepareSearch("idx2")
.setQuery(matchAllQuery())
.addAggregation(dateHistogram("date_histo")
.field("date")
.postOffset("2d")
.interval(DateHistogram.Interval.DAY)
.format("yyyy-MM-dd"))
.execute().actionGet();
assertThat(response.getHits().getTotalHits(), equalTo(5l));
DateHistogram histo = response.getAggregations().get("date_histo");
Collection<? extends DateHistogram.Bucket> buckets = histo.getBuckets();
assertThat(buckets.size(), equalTo(2));
DateHistogram.Bucket bucket = histo.getBucketByKey("2014-03-13");
assertThat(bucket, Matchers.notNullValue());
assertThat(bucket.getDocCount(), equalTo(4l));
bucket = histo.getBucketByKey("2014-03-14");
assertThat(bucket, Matchers.notNullValue());
assertThat(bucket.getDocCount(), equalTo(1l));
}
@Test
public void singleValue_WithPreZone_WithAadjustLargeInterval() throws Exception {
prepareCreate("idx2").addMapping("type", "date", "type=date").execute().actionGet();