Skip to content

Commit 86a3eff

Browse files
authored
Full coverage of ECS by ecs@mappings when date_detection is disabled (#112444)
1 parent dd5ac0f commit 86a3eff

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

docs/changelog/112444.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 112444
2+
summary: Full coverage of ECS by ecs@mappings when `date_detection` is disabled
3+
area: Mapping
4+
type: bug
5+
issues:
6+
- 112398

x-pack/plugin/core/template-resources/src/main/resources/[email protected]

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@
155155
"ingested",
156156
"*.ingested",
157157
"*.start",
158-
"*.end"
158+
"*.end",
159+
"*.indicator.first_seen",
160+
"*.indicator.last_seen",
161+
"*.indicator.modified_at",
162+
"*threat.enrichments.matched.occurred"
159163
],
160164
"unmatch_mapping_type": "object"
161165
}

x-pack/plugin/stack/src/javaRestTest/java/org/elasticsearch/xpack/stack/EcsDynamicTemplatesIT.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.elasticsearch.common.Strings;
1414
import org.elasticsearch.common.network.NetworkAddress;
1515
import org.elasticsearch.common.time.DateFormatter;
16+
import org.elasticsearch.common.time.FormatNames;
1617
import org.elasticsearch.core.Nullable;
1718
import org.elasticsearch.core.SuppressForbidden;
1819
import org.elasticsearch.test.cluster.ElasticsearchCluster;
@@ -206,6 +207,44 @@ public void testNumericMessage() throws IOException {
206207
verifyEcsMappings(indexName);
207208
}
208209

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+
209248
private void assertType(String expectedType, Map<String, Object> actualMappings) {
210249
assertNotNull("expected to get non-null mappings for field", actualMappings);
211250
assertEquals(expectedType, actualMappings.get("type"));
@@ -312,6 +351,7 @@ private static void createTestIndex(String indexName, @Nullable Map<String, Obje
312351
} else {
313352
indexMappings = ecsDynamicTemplates;
314353
}
354+
indexMappings.put("date_detection", false);
315355
try (XContentBuilder bodyBuilder = JsonXContent.contentBuilder()) {
316356
bodyBuilder.startObject();
317357
bodyBuilder.startObject("settings");
@@ -349,7 +389,7 @@ private Object generateTestValue(String type) {
349389
return "test";
350390
}
351391
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());
353393
}
354394
case "ip" -> {
355395
return NetworkAddress.format(randomIp(true));
@@ -485,9 +525,11 @@ private void verifyEcsMappings(String indexName, String fieldPrefix) throws IOEx
485525
);
486526
});
487527
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);
489531
if (fieldMappings == null) {
490-
fieldMappings = ecsFlatMultiFieldDefinitions.get(fieldName);
532+
fieldMappings = ecsFlatMultiFieldDefinitions.get(ecsFieldName);
491533
}
492534
String ecsExpectedType = (String) fieldMappings.get("type");
493535
logger.error(

x-pack/plugin/stack/src/main/java/org/elasticsearch/xpack/stack/StackTemplateRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class StackTemplateRegistry extends IndexTemplateRegistry {
4848

4949
// The stack template registry version. This number must be incremented when we make changes
5050
// to built-in templates.
51-
public static final int REGISTRY_VERSION = 13;
51+
public static final int REGISTRY_VERSION = 14;
5252

5353
public static final String TEMPLATE_VERSION_VARIABLE = "xpack.stack.template.version";
5454
public static final Setting<Boolean> STACK_TEMPLATES_ENABLED = Setting.boolSetting(

0 commit comments

Comments
 (0)