From ff4f63bd1b885110a25ad82ad426c764b4cb48a1 Mon Sep 17 00:00:00 2001 From: Joanne Wang Date: Wed, 21 Feb 2024 17:26:24 -0800 Subject: [PATCH] change naming for existingFieldMapping and change contains to equals Signed-off-by: Joanne Wang --- .../logtype/LogTypeService.java | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/opensearch/securityanalytics/logtype/LogTypeService.java b/src/main/java/org/opensearch/securityanalytics/logtype/LogTypeService.java index c1e13acd0..56f215a11 100644 --- a/src/main/java/org/opensearch/securityanalytics/logtype/LogTypeService.java +++ b/src/main/java/org/opensearch/securityanalytics/logtype/LogTypeService.java @@ -346,19 +346,27 @@ private List mergeFieldMappings(List existingF List newFieldMappings = new ArrayList<>(); fieldMappingDocs.forEach( newFieldMapping -> { Optional foundFieldMappingDoc = Optional.empty(); - for (FieldMappingDoc e: existingFieldMappings) { - if (e.getRawField().equals(newFieldMapping.getRawField())) { + for (FieldMappingDoc existingFieldMapping: existingFieldMappings) { + if (existingFieldMapping.getRawField().equals(newFieldMapping.getRawField())) { if (( - e.get(defaultSchemaField) != null && newFieldMapping.get(defaultSchemaField) != null && - e.get(defaultSchemaField).equals(newFieldMapping.get(defaultSchemaField)) + existingFieldMapping.get(defaultSchemaField) != null && newFieldMapping.get(defaultSchemaField) != null && + existingFieldMapping.get(defaultSchemaField).equals(newFieldMapping.get(defaultSchemaField)) ) || ( - e.get(defaultSchemaField) == null && newFieldMapping.get(defaultSchemaField) == null + existingFieldMapping.get(defaultSchemaField) == null && newFieldMapping.get(defaultSchemaField) == null )) { - foundFieldMappingDoc = Optional.of(e); + foundFieldMappingDoc = Optional.of(existingFieldMapping); + } + // Grabs the right side of the ID with "|" as the delimiter if present representing the ecs field from predefined mappings + // Additional check to see if raw field path + log type combination is already in existingFieldMappings so a new one is not indexed + } else { + String id = existingFieldMapping.getId(); + int indexOfPipe = id.indexOf("|"); + if (indexOfPipe != -1) { + String ecsIdField = id.substring(indexOfPipe + 1); + if (ecsIdField.equals(newFieldMapping.getRawField()) && existingFieldMapping.getLogTypes().containsAll(newFieldMapping.getLogTypes())) { + foundFieldMappingDoc = Optional.of(existingFieldMapping); + } } - // Additional check to see if raw field path + log type combination is already in existingFieldMappings so a new one is not inserted - } else if (e.getId().contains(newFieldMapping.getRawField()) && e.getLogTypes().containsAll(newFieldMapping.getLogTypes())) { - foundFieldMappingDoc = Optional.of(e); } } if (foundFieldMappingDoc.isEmpty()) {