mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-26 14:54:56 +00:00
Make partial dates without year to be 1970 based instead of 2000
Fixes #4451 Date fields without date (HH:mm:ss, for example) are parsed as time on Jan 1, 1970 UTC. However, before this change partial dates without year (MMM dd HH:mm:ss, for example) were parsed as as days of they year 2000. This change makes all partial dates to be treated based on year 1970. This is breaking change - before this change "Dec 15, 10:00:00" in most cases was parsed (and indexed) as "2000-12-15T10:00:00Z". After this change, it will be consistently parsed and indexed as "1970-12-15T10:00:00Z"
This commit is contained in:
parent
5a02ec86a8
commit
56b3941706
@ -44,8 +44,8 @@ public class FormatDateTimeFormatter {
|
|||||||
public FormatDateTimeFormatter(String format, DateTimeFormatter parser, DateTimeFormatter printer, Locale locale) {
|
public FormatDateTimeFormatter(String format, DateTimeFormatter parser, DateTimeFormatter printer, Locale locale) {
|
||||||
this.format = format;
|
this.format = format;
|
||||||
this.locale = locale;
|
this.locale = locale;
|
||||||
this.printer = locale == null ? printer : printer.withLocale(locale);
|
this.printer = locale == null ? printer.withDefaultYear(1970) : printer.withLocale(locale).withDefaultYear(1970);
|
||||||
this.parser = locale == null ? parser : parser.withLocale(locale);
|
this.parser = locale == null ? parser.withDefaultYear(1970) : parser.withLocale(locale).withDefaultYear(1970);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String format() {
|
public String format() {
|
||||||
|
@ -224,6 +224,30 @@ public class SimpleDateMappingTests extends ElasticsearchTestCase {
|
|||||||
assertThat(rangeFilter.getMin(), equalTo(new DateTime(TimeValue.timeValueHours(10).millis()).getMillis()));
|
assertThat(rangeFilter.getMin(), equalTo(new DateTime(TimeValue.timeValueHours(10).millis()).getMillis()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDayWithoutYearFormat() throws Exception {
|
||||||
|
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
|
||||||
|
.field("date_detection", false)
|
||||||
|
.startObject("properties").startObject("date_field").field("type", "date").field("format", "MMM dd HH:mm:ss").endObject().endObject()
|
||||||
|
.endObject().endObject().string();
|
||||||
|
|
||||||
|
DocumentMapper defaultMapper = mapper(mapping);
|
||||||
|
|
||||||
|
ParsedDocument doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder()
|
||||||
|
.startObject()
|
||||||
|
.field("date_field", "Jan 02 10:00:00")
|
||||||
|
.endObject()
|
||||||
|
.bytes());
|
||||||
|
assertThat(((LongFieldMapper.CustomLongNumericField) doc.rootDoc().getField("date_field")).numericAsString(), equalTo(Long.toString(new DateTime(TimeValue.timeValueHours(34).millis(), DateTimeZone.UTC).getMillis())));
|
||||||
|
|
||||||
|
Filter filter = defaultMapper.mappers().smartNameFieldMapper("date_field").rangeFilter("Jan 02 10:00:00", "Jan 02 11:00:00", true, true, null);
|
||||||
|
assertThat(filter, instanceOf(NumericRangeFilter.class));
|
||||||
|
NumericRangeFilter<Long> rangeFilter = (NumericRangeFilter<Long>) filter;
|
||||||
|
assertThat(rangeFilter.getMax(), equalTo(new DateTime(TimeValue.timeValueHours(35).millis() + 999).getMillis())); // +999 to include the 00-01 minute
|
||||||
|
assertThat(rangeFilter.getMin(), equalTo(new DateTime(TimeValue.timeValueHours(34).millis()).getMillis()));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIgnoreMalformedOption() throws Exception {
|
public void testIgnoreMalformedOption() throws Exception {
|
||||||
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
|
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user