diff --git a/src/main/java/com/amazon/opendistroforelasticsearch/sql/rewriter/matchtoterm/TermFieldRewriter.java b/src/main/java/com/amazon/opendistroforelasticsearch/sql/rewriter/matchtoterm/TermFieldRewriter.java index 314299d104..8cbc85cada 100644 --- a/src/main/java/com/amazon/opendistroforelasticsearch/sql/rewriter/matchtoterm/TermFieldRewriter.java +++ b/src/main/java/com/amazon/opendistroforelasticsearch/sql/rewriter/matchtoterm/TermFieldRewriter.java @@ -261,43 +261,45 @@ private void checkMappingCompatibility(TermFieldScope scope, Map final FieldMappings fieldMappings; if (indexMappings.size() > 1) { - Map> mergedMapping = new HashMap<>(); + Map mergedMapping = new HashMap<>(); for (FieldMappings f : indexMappings) { - Map> m = f.data(); - m.forEach((k, v) -> verifySingleFieldMapping(k, v, mergedMapping)); + verifyFieldsMapping(f.mapping(""), mergedMapping); } - fieldMappings = new FieldMappings(mergedMapping); + Map> finalMapping = new HashMap<>(); + mergedMapping.forEach((k, v) -> finalMapping.put(k, (Map) v)); + fieldMappings = new FieldMappings(finalMapping); } else { + // single index fieldMappings = curScope().getMapper().firstMapping().firstMapping(); } // We need finalMapping to lookup for rewriting curScope().setFinalMapping(fieldMappings); } - private void verifySingleFieldMapping(final String fieldName, final Map fieldMapping, - final Map> mergedMapping) { - - if (!mergedMapping.containsKey(fieldName)) { - mergedMapping.put(fieldName, fieldMapping); - } else { - - final Map visitedMapping = mergedMapping.get(fieldName); - // check if types are same - if (!fieldMapping.equals(visitedMapping)) { - // TODO: Merge mappings if they are compatible, for text and text/keyword to text/keyword. - - String firstFieldType = new JSONObject(fieldMapping).toString().replaceAll("\"", ""); - String secondFieldType = new JSONObject(visitedMapping).toString().replaceAll("\"", ""); - - String exceptionReason = String.format(Locale.ROOT, "Different mappings are not allowed " - + "for the same field[%s]: found [%s] and [%s] ", - fieldName, firstFieldType, secondFieldType); - - throw new VerificationException(exceptionReason); + private void verifyFieldsMapping(final Map newMapping, final Map mergedMapping) { + newMapping.forEach((fieldName, fieldMapping) -> { + if (!mergedMapping.containsKey(fieldName)) { + mergedMapping.put(fieldName, fieldMapping); + } else { + final Object visitedFieldMapping = mergedMapping.get(fieldName); + if (fieldName != "properties") { + if (!fieldMapping.equals(visitedFieldMapping)) { + String firstFieldType = new JSONObject(fieldMapping).toString().replaceAll("\"", ""); + String secondFieldType = new JSONObject(visitedFieldMapping).toString().replaceAll("\"", ""); + + String exceptionReason = String.format(Locale.ROOT, "Different mappings are not allowed " + + "for the same field[%s]: found [%s] and [%s] ", + fieldName, firstFieldType, secondFieldType); + + throw new VerificationException(exceptionReason); + } + } else { + verifyFieldsMapping((Map) fieldMapping, (Map) visitedFieldMapping); + } } - } + }); } public IndexMappings getMappings(Map indexToType) {