Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
fix: allow the nest field missing in the multi-index query
Browse files Browse the repository at this point in the history
  • Loading branch information
LS committed Feb 7, 2020
1 parent 77efb0b commit 221047d
Showing 1 changed file with 27 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,43 +261,45 @@ private void checkMappingCompatibility(TermFieldScope scope, Map<String, String>
final FieldMappings fieldMappings;

if (indexMappings.size() > 1) {
Map<String, Map<String, Object>> mergedMapping = new HashMap<>();
Map<String, Object> mergedMapping = new HashMap<>();

for (FieldMappings f : indexMappings) {
Map<String, Map<String, Object>> m = f.data();
m.forEach((k, v) -> verifySingleFieldMapping(k, v, mergedMapping));
verifyFieldsMapping(f.mapping(""), mergedMapping);
}

fieldMappings = new FieldMappings(mergedMapping);
Map<String, Map<String, Object>> finalMapping = new HashMap<>();
mergedMapping.forEach((k, v) -> finalMapping.put(k, (Map<String, Object>) 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<String, Object> fieldMapping,
final Map<String, Map<String, Object>> mergedMapping) {

if (!mergedMapping.containsKey(fieldName)) {
mergedMapping.put(fieldName, fieldMapping);
} else {

final Map<String, Object> 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<String, Object> newMapping, final Map<String, Object> 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<String, Object>) fieldMapping, (Map<String, Object>) visitedFieldMapping);
}
}
}
});
}

public IndexMappings getMappings(Map<String, String> indexToType) {
Expand Down

0 comments on commit 221047d

Please sign in to comment.