Aggregations: Fixed conversion of date field values when using multiple date formats
When multiple date formats are specified using the || syntax in the field mappings the date_histogram aggregation breaks. This is because we are getting a parser rather than a printer from the date formatter for the object we use to convert the DateTime values back into Strings. Simple fix to get the printer from the date format and test to back it up Closes #6239
This commit is contained in:
parent
de9fca9cb2
commit
cabd2340dd
|
@ -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);
|
||||
|
|
|
@ -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<? extends DateHistogram.Bucket> 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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue