Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java.time] Retain prefixed date pattern in formatter #48703

Merged
merged 12 commits into from
Nov 27, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,6 @@ private long parseMillis(String date) {

@Override
Function<String, ZonedDateTime> getFunction(String format, ZoneId zoneId, Locale locale) {
// support the 6.x BWC compatible way of parsing java 8 dates
if (format.startsWith("8")) {
format = format.substring(1);
}

boolean isUtc = ZoneOffset.UTC.equals(zoneId);

DateFormatter dateFormatter = DateFormatter.forPattern(format)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
"Test Index and Search locale dependent mappings / dates":
- skip:
version: "all"
reason: "Awaits fix: https://github.com/elastic/elasticsearch/issues/39981(Previously: JDK9 only supports this with a special sysproperty added in 6.2.0.)"
version: " - 6.1.99"
reason: JDK9 only supports this with a special sysproperty added in 6.2.0
- do:
indices.create:
index: test_index
Expand All @@ -13,7 +13,7 @@
properties:
date_field:
type: date
format: "E, d MMM yyyy HH:mm:ss Z"
format: "8E, d MMM uuuu HH:mm:ss Z"
locale: "de"
- do:
bulk:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,27 +129,28 @@ default String formatJoda(DateTime dateTime) {
DateMathParser toDateMathParser();

static DateFormatter forPattern(String input) {

if (Strings.hasLength(input) == false) {
throw new IllegalArgumentException("No date pattern provided");
}

// support the 6.x BWC compatible way of parsing java 8 dates
if (input.startsWith("8")) {
input = input.substring(1);
}

List<String> patterns = splitCombinedPatterns(input);
String format = strip8Prefix(input);
List<String> patterns = splitCombinedPatterns(format);
List<DateFormatter> formatters = patterns.stream()
.map(DateFormatters::forPattern)
.collect(Collectors.toList());

if (formatters.size() == 1) {
return formatters.get(0);
}

return JavaDateFormatter.combined(input, formatters);
}

static String strip8Prefix(String input) {
if (input.startsWith("8")) {
return input.substring(1);
}
return input;
}

static List<String> splitCombinedPatterns(String input) {
List<String> patterns = new ArrayList<>();
for (String pattern : Strings.delimitedListToStringArray(input, "||")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,21 @@ public DocumentMapper updateFieldType(Map<String, MappedFieldType> fullNameToFie
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return mapping.toXContent(builder, params);
}

@Override
public String toString() {
return "DocumentMapper{" +
"mapperService=" + mapperService +
", type='" + type + '\'' +
", typeText=" + typeText +
", mappingSource=" + mappingSource +
", mapping=" + mapping +
", documentParser=" + documentParser +
", fieldMappers=" + fieldMappers +
", objectMappers=" + objectMappers +
", hasNestedObjects=" + hasNestedObjects +
", deleteTombstoneMetadataFieldMappers=" + Arrays.toString(deleteTombstoneMetadataFieldMappers) +
", noopTombstoneMetadataFieldMappers=" + Arrays.toString(noopTombstoneMetadataFieldMappers) +
'}';
}
}