|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.search.aggregations.bucket; |
| 20 | + |
| 21 | +import org.elasticsearch.Version; |
| 22 | +import org.elasticsearch.action.index.IndexRequestBuilder; |
| 23 | +import org.elasticsearch.action.search.SearchResponse; |
| 24 | +import org.elasticsearch.common.settings.ImmutableSettings; |
| 25 | +import org.elasticsearch.common.settings.Settings; |
| 26 | +import org.elasticsearch.index.mapper.core.DateFieldMapper; |
| 27 | +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogram; |
| 28 | +import org.elasticsearch.test.ElasticsearchIntegrationTest; |
| 29 | +import org.elasticsearch.test.transport.AssertingLocalTransport; |
| 30 | +import org.hamcrest.Matchers; |
| 31 | +import org.joda.time.DateTime; |
| 32 | +import org.junit.After; |
| 33 | +import org.junit.Test; |
| 34 | + |
| 35 | +import java.io.IOException; |
| 36 | +import java.util.Collection; |
| 37 | + |
| 38 | +import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; |
| 39 | +import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; |
| 40 | +import static org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram; |
| 41 | +import static org.hamcrest.Matchers.equalTo; |
| 42 | + |
| 43 | +/** |
| 44 | + * 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 |
| 45 | + * serialisation of negative offsets in these tests would break in pre 1.4 versions. These tests are separated from the other DateHistogramTests so the |
| 46 | + * AssertingLocalTransport for these tests can be set to only use versions 1.4 onwards while keeping the other tests using all versions |
| 47 | + */ |
| 48 | +@ElasticsearchIntegrationTest.SuiteScopeTest |
| 49 | +@ElasticsearchIntegrationTest.ClusterScope(scope=ElasticsearchIntegrationTest.Scope.SUITE) |
| 50 | +public class DateHistogramOffsetTests extends ElasticsearchIntegrationTest { |
| 51 | + |
| 52 | + private DateTime date(String date) { |
| 53 | + return DateFieldMapper.Defaults.DATE_TIME_FORMATTER.parser().parseDateTime(date); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + protected Settings nodeSettings(int nodeOrdinal) { |
| 58 | + return ImmutableSettings.builder() |
| 59 | + .put(AssertingLocalTransport.ASSERTING_TRANSPORT_MIN_VERSION_KEY, Version.V_1_4_0).build(); |
| 60 | + } |
| 61 | + |
| 62 | + @After |
| 63 | + public void afterEachTest() throws IOException { |
| 64 | + internalCluster().wipeIndices("idx2"); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void singleValue_WithPreOffset() throws Exception { |
| 69 | + prepareCreate("idx2").addMapping("type", "date", "type=date").execute().actionGet(); |
| 70 | + IndexRequestBuilder[] reqs = new IndexRequestBuilder[5]; |
| 71 | + DateTime date = date("2014-03-11T00:00:00+00:00"); |
| 72 | + for (int i = 0; i < reqs.length; i++) { |
| 73 | + reqs[i] = client().prepareIndex("idx2", "type", "" + i).setSource(jsonBuilder().startObject().field("date", date).endObject()); |
| 74 | + date = date.plusHours(1); |
| 75 | + } |
| 76 | + indexRandom(true, reqs); |
| 77 | + |
| 78 | + SearchResponse response = client().prepareSearch("idx2") |
| 79 | + .setQuery(matchAllQuery()) |
| 80 | + .addAggregation(dateHistogram("date_histo") |
| 81 | + .field("date") |
| 82 | + .preOffset("-2h") |
| 83 | + .interval(DateHistogram.Interval.DAY) |
| 84 | + .format("yyyy-MM-dd")) |
| 85 | + .execute().actionGet(); |
| 86 | + |
| 87 | + assertThat(response.getHits().getTotalHits(), equalTo(5l)); |
| 88 | + |
| 89 | + DateHistogram histo = response.getAggregations().get("date_histo"); |
| 90 | + Collection<? extends DateHistogram.Bucket> buckets = histo.getBuckets(); |
| 91 | + assertThat(buckets.size(), equalTo(2)); |
| 92 | + |
| 93 | + DateHistogram.Bucket bucket = histo.getBucketByKey("2014-03-10"); |
| 94 | + assertThat(bucket, Matchers.notNullValue()); |
| 95 | + assertThat(bucket.getDocCount(), equalTo(2l)); |
| 96 | + |
| 97 | + bucket = histo.getBucketByKey("2014-03-11"); |
| 98 | + assertThat(bucket, Matchers.notNullValue()); |
| 99 | + assertThat(bucket.getDocCount(), equalTo(3l)); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void singleValue_WithPreOffset_MinDocCount() throws Exception { |
| 104 | + prepareCreate("idx2").addMapping("type", "date", "type=date").execute().actionGet(); |
| 105 | + IndexRequestBuilder[] reqs = new IndexRequestBuilder[5]; |
| 106 | + DateTime date = date("2014-03-11T00:00:00+00:00"); |
| 107 | + for (int i = 0; i < reqs.length; i++) { |
| 108 | + reqs[i] = client().prepareIndex("idx2", "type", "" + i).setSource(jsonBuilder().startObject().field("date", date).endObject()); |
| 109 | + date = date.plusHours(1); |
| 110 | + } |
| 111 | + indexRandom(true, reqs); |
| 112 | + |
| 113 | + SearchResponse response = client().prepareSearch("idx2") |
| 114 | + .setQuery(matchAllQuery()) |
| 115 | + .addAggregation(dateHistogram("date_histo") |
| 116 | + .field("date") |
| 117 | + .preOffset("-2h") |
| 118 | + .minDocCount(0) |
| 119 | + .interval(DateHistogram.Interval.DAY) |
| 120 | + .format("yyyy-MM-dd")) |
| 121 | + .execute().actionGet(); |
| 122 | + |
| 123 | + assertThat(response.getHits().getTotalHits(), equalTo(5l)); |
| 124 | + |
| 125 | + DateHistogram histo = response.getAggregations().get("date_histo"); |
| 126 | + Collection<? extends DateHistogram.Bucket> buckets = histo.getBuckets(); |
| 127 | + assertThat(buckets.size(), equalTo(2)); |
| 128 | + |
| 129 | + DateHistogram.Bucket bucket = histo.getBucketByKey("2014-03-10"); |
| 130 | + assertThat(bucket, Matchers.notNullValue()); |
| 131 | + assertThat(bucket.getDocCount(), equalTo(2l)); |
| 132 | + |
| 133 | + bucket = histo.getBucketByKey("2014-03-11"); |
| 134 | + assertThat(bucket, Matchers.notNullValue()); |
| 135 | + assertThat(bucket.getDocCount(), equalTo(3l)); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void singleValue_WithPostOffset() throws Exception { |
| 140 | + prepareCreate("idx2").addMapping("type", "date", "type=date").execute().actionGet(); |
| 141 | + IndexRequestBuilder[] reqs = new IndexRequestBuilder[5]; |
| 142 | + DateTime date = date("2014-03-11T00:00:00+00:00"); |
| 143 | + for (int i = 0; i < reqs.length; i++) { |
| 144 | + reqs[i] = client().prepareIndex("idx2", "type", "" + i).setSource(jsonBuilder().startObject().field("date", date).endObject()); |
| 145 | + date = date.plusHours(6); |
| 146 | + } |
| 147 | + indexRandom(true, reqs); |
| 148 | + |
| 149 | + SearchResponse response = client().prepareSearch("idx2") |
| 150 | + .setQuery(matchAllQuery()) |
| 151 | + .addAggregation(dateHistogram("date_histo") |
| 152 | + .field("date") |
| 153 | + .postOffset("2d") |
| 154 | + .interval(DateHistogram.Interval.DAY) |
| 155 | + .format("yyyy-MM-dd")) |
| 156 | + .execute().actionGet(); |
| 157 | + |
| 158 | + assertThat(response.getHits().getTotalHits(), equalTo(5l)); |
| 159 | + |
| 160 | + DateHistogram histo = response.getAggregations().get("date_histo"); |
| 161 | + Collection<? extends DateHistogram.Bucket> buckets = histo.getBuckets(); |
| 162 | + assertThat(buckets.size(), equalTo(2)); |
| 163 | + |
| 164 | + DateHistogram.Bucket bucket = histo.getBucketByKey("2014-03-13"); |
| 165 | + assertThat(bucket, Matchers.notNullValue()); |
| 166 | + assertThat(bucket.getDocCount(), equalTo(4l)); |
| 167 | + |
| 168 | + bucket = histo.getBucketByKey("2014-03-14"); |
| 169 | + assertThat(bucket, Matchers.notNullValue()); |
| 170 | + assertThat(bucket.getDocCount(), equalTo(1l)); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void singleValue_WithPostOffset_MinDocCount() throws Exception { |
| 175 | + prepareCreate("idx2").addMapping("type", "date", "type=date").execute().actionGet(); |
| 176 | + IndexRequestBuilder[] reqs = new IndexRequestBuilder[5]; |
| 177 | + DateTime date = date("2014-03-11T00:00:00+00:00"); |
| 178 | + for (int i = 0; i < reqs.length; i++) { |
| 179 | + reqs[i] = client().prepareIndex("idx2", "type", "" + i).setSource(jsonBuilder().startObject().field("date", date).endObject()); |
| 180 | + date = date.plusHours(6); |
| 181 | + } |
| 182 | + indexRandom(true, reqs); |
| 183 | + |
| 184 | + SearchResponse response = client().prepareSearch("idx2") |
| 185 | + .setQuery(matchAllQuery()) |
| 186 | + .addAggregation(dateHistogram("date_histo") |
| 187 | + .field("date") |
| 188 | + .postOffset("2d") |
| 189 | + .minDocCount(0) |
| 190 | + .interval(DateHistogram.Interval.DAY) |
| 191 | + .format("yyyy-MM-dd")) |
| 192 | + .execute().actionGet(); |
| 193 | + |
| 194 | + assertThat(response.getHits().getTotalHits(), equalTo(5l)); |
| 195 | + |
| 196 | + DateHistogram histo = response.getAggregations().get("date_histo"); |
| 197 | + Collection<? extends DateHistogram.Bucket> buckets = histo.getBuckets(); |
| 198 | + assertThat(buckets.size(), equalTo(2)); |
| 199 | + |
| 200 | + DateHistogram.Bucket bucket = histo.getBucketByKey("2014-03-13"); |
| 201 | + assertThat(bucket, Matchers.notNullValue()); |
| 202 | + assertThat(bucket.getDocCount(), equalTo(4l)); |
| 203 | + |
| 204 | + bucket = histo.getBucketByKey("2014-03-14"); |
| 205 | + assertThat(bucket, Matchers.notNullValue()); |
| 206 | + assertThat(bucket.getDocCount(), equalTo(1l)); |
| 207 | + } |
| 208 | +} |
0 commit comments