diff --git a/src/main/java/org/elasticsearch/common/joda/Joda.java b/src/main/java/org/elasticsearch/common/joda/Joda.java index c5bb25cb9a2..88940224ec8 100644 --- a/src/main/java/org/elasticsearch/common/joda/Joda.java +++ b/src/main/java/org/elasticsearch/common/joda/Joda.java @@ -19,8 +19,6 @@ package org.elasticsearch.common.joda; -import java.util.Locale; - import org.elasticsearch.common.Strings; import org.joda.time.*; import org.joda.time.field.DividedDateTimeField; @@ -28,6 +26,8 @@ import org.joda.time.field.OffsetDateTimeField; import org.joda.time.field.ScaledDurationField; import org.joda.time.format.*; +import java.util.Locale; + /** * */ @@ -142,11 +142,12 @@ public class Joda { } else { DateTimeFormatter dateTimeFormatter = null; for (int i = 0; i < formats.length; i++) { - DateTimeFormatter currentFormatter = forPattern(formats[i], locale).parser(); + FormatDateTimeFormatter currentFormatter = forPattern(formats[i], locale); + DateTimeFormatter currentParser = currentFormatter.parser(); if (dateTimeFormatter == null) { - dateTimeFormatter = currentFormatter; + dateTimeFormatter = currentFormatter.printer(); } - parsers[i] = currentFormatter.getParser(); + parsers[i] = currentParser.getParser(); } DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder().append(dateTimeFormatter.withZone(DateTimeZone.UTC).getPrinter(), parsers); diff --git a/src/test/java/org/elasticsearch/search/aggregations/bucket/DateHistogramTests.java b/src/test/java/org/elasticsearch/search/aggregations/bucket/DateHistogramTests.java index a8cece62fb5..4850fbe4778 100644 --- a/src/test/java/org/elasticsearch/search/aggregations/bucket/DateHistogramTests.java +++ b/src/test/java/org/elasticsearch/search/aggregations/bucket/DateHistogramTests.java @@ -1200,4 +1200,33 @@ public class DateHistogramTests extends ElasticsearchIntegrationTest { key = key.plusDays(interval); } } + + @Test + public void singleValue_WithMultipleDateFormatsFromMapping() throws Exception { + + String mappingJson = jsonBuilder().startObject().startObject("type").startObject("properties").startObject("date").field("type", "date").field("format", "dateOptionalTime||dd-MM-yyyy").endObject().endObject().endObject().endObject().string(); + prepareCreate("idx2").addMapping("type", mappingJson).execute().actionGet(); + IndexRequestBuilder[] reqs = new IndexRequestBuilder[5]; + for (int i = 0; i < reqs.length; i++) { + reqs[i] = client().prepareIndex("idx2", "type", "" + i).setSource(jsonBuilder().startObject().field("date", "10-03-2014").endObject()); + } + indexRandom(true, reqs); + + SearchResponse response = client().prepareSearch("idx2") + .setQuery(matchAllQuery()) + .addAggregation(dateHistogram("date_histo") + .field("date") + .interval(DateHistogram.Interval.DAY)) + .execute().actionGet(); + + assertThat(response.getHits().getTotalHits(), equalTo(5l)); + + DateHistogram histo = response.getAggregations().get("date_histo"); + Collection buckets = histo.getBuckets(); + assertThat(buckets.size(), equalTo(1)); + + DateHistogram.Bucket bucket = histo.getBucketByKey("2014-03-10T00:00:00.000Z"); + assertThat(bucket, Matchers.notNullValue()); + assertThat(bucket.getDocCount(), equalTo(5l)); + } }