|
13 | 13 | import org.elasticsearch.common.Strings;
|
14 | 14 | import org.elasticsearch.common.network.NetworkAddress;
|
15 | 15 | import org.elasticsearch.common.time.DateFormatter;
|
| 16 | +import org.elasticsearch.common.time.FormatNames; |
16 | 17 | import org.elasticsearch.core.Nullable;
|
17 | 18 | import org.elasticsearch.core.SuppressForbidden;
|
18 | 19 | import org.elasticsearch.test.cluster.ElasticsearchCluster;
|
@@ -206,6 +207,44 @@ public void testNumericMessage() throws IOException {
|
206 | 207 | verifyEcsMappings(indexName);
|
207 | 208 | }
|
208 | 209 |
|
| 210 | + public void testDateFieldsWithDifferentFormats() throws IOException { |
| 211 | + Map<String, Object> dateFieldsMap = ecsFlatFieldDefinitions.entrySet() |
| 212 | + .stream() |
| 213 | + .filter(entry -> "date".equals(entry.getValue().get("type"))) |
| 214 | + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 215 | + |
| 216 | + // test with iso8601 format |
| 217 | + String indexName = "test-date-fields-as-is8601"; |
| 218 | + createTestIndex(indexName); |
| 219 | + Map<String, Object> document = new HashMap<>(); |
| 220 | + DateFormatter formatter = DateFormatter.forPattern(FormatNames.ISO8601.getName()); |
| 221 | + for (String field : dateFieldsMap.keySet()) { |
| 222 | + document.put(field, formatter.formatMillis(System.currentTimeMillis())); |
| 223 | + } |
| 224 | + verifyAllDateFields(indexName, document, dateFieldsMap); |
| 225 | + |
| 226 | + // test with milliseconds since epoch format |
| 227 | + indexName = "test-date-fields-as-millis"; |
| 228 | + createTestIndex(indexName); |
| 229 | + document = new HashMap<>(); |
| 230 | + for (String field : dateFieldsMap.keySet()) { |
| 231 | + document.put(field, System.currentTimeMillis()); |
| 232 | + } |
| 233 | + verifyAllDateFields(indexName, document, dateFieldsMap); |
| 234 | + } |
| 235 | + |
| 236 | + private void verifyAllDateFields(String indexName, Map<String, Object> document, Map<String, Object> dateFieldsMap) throws IOException { |
| 237 | + indexDocument(indexName, document); |
| 238 | + final Map<String, Object> rawMappings = getMappings(indexName); |
| 239 | + final Map<String, Map<String, Object>> flatFieldMappings = new HashMap<>(); |
| 240 | + processRawMappingsSubtree(rawMappings, flatFieldMappings, new HashMap<>(), ""); |
| 241 | + flatFieldMappings.forEach((fieldName, fieldMappings) -> { |
| 242 | + if (dateFieldsMap.containsKey(fieldName)) { |
| 243 | + assertType("date", fieldMappings); |
| 244 | + } |
| 245 | + }); |
| 246 | + } |
| 247 | + |
209 | 248 | private void assertType(String expectedType, Map<String, Object> actualMappings) {
|
210 | 249 | assertNotNull("expected to get non-null mappings for field", actualMappings);
|
211 | 250 | assertEquals(expectedType, actualMappings.get("type"));
|
@@ -312,6 +351,7 @@ private static void createTestIndex(String indexName, @Nullable Map<String, Obje
|
312 | 351 | } else {
|
313 | 352 | indexMappings = ecsDynamicTemplates;
|
314 | 353 | }
|
| 354 | + indexMappings.put("date_detection", false); |
315 | 355 | try (XContentBuilder bodyBuilder = JsonXContent.contentBuilder()) {
|
316 | 356 | bodyBuilder.startObject();
|
317 | 357 | bodyBuilder.startObject("settings");
|
@@ -349,7 +389,7 @@ private Object generateTestValue(String type) {
|
349 | 389 | return "test";
|
350 | 390 | }
|
351 | 391 | case "date" -> {
|
352 |
| - return DateFormatter.forPattern("strict_date_optional_time").formatMillis(System.currentTimeMillis()); |
| 392 | + return DateFormatter.forPattern(FormatNames.STRICT_DATE_OPTIONAL_TIME.getName()).formatMillis(System.currentTimeMillis()); |
353 | 393 | }
|
354 | 394 | case "ip" -> {
|
355 | 395 | return NetworkAddress.format(randomIp(true));
|
@@ -485,9 +525,11 @@ private void verifyEcsMappings(String indexName, String fieldPrefix) throws IOEx
|
485 | 525 | );
|
486 | 526 | });
|
487 | 527 | fieldToWrongMappingType.forEach((fieldName, actualMappingType) -> {
|
488 |
| - Map<String, Object> fieldMappings = ecsFlatFieldDefinitions.get(fieldName); |
| 528 | + // if fieldPrefix is not null, we need to remove it from the field name for the ECS lookup |
| 529 | + String ecsFieldName = fieldPrefix == null ? fieldName : fieldName.substring(fieldPrefix.length()); |
| 530 | + Map<String, Object> fieldMappings = ecsFlatFieldDefinitions.get(ecsFieldName); |
489 | 531 | if (fieldMappings == null) {
|
490 |
| - fieldMappings = ecsFlatMultiFieldDefinitions.get(fieldName); |
| 532 | + fieldMappings = ecsFlatMultiFieldDefinitions.get(ecsFieldName); |
491 | 533 | }
|
492 | 534 | String ecsExpectedType = (String) fieldMappings.get("type");
|
493 | 535 | logger.error(
|
|
0 commit comments