Skip to content

Commit

Permalink
Issue #543: fix mapping for nested fields
Browse files Browse the repository at this point in the history
  • Loading branch information
mrk-vi committed Aug 28, 2023
1 parent f308a96 commit 06e3c9b
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 285 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,106 +201,25 @@ public static Map<MappingsKey, Object> docTypesToMappings(Collection<DocType> do
);
}

public static Map<MappingsKey, Object> docTypesHierarchicalToMappings(
Collection<DocType> docTypes) {

return docTypes
.stream()
.map(DocType::getDocTypeFields)
.flatMap(Collection::stream)
.collect(
Collectors.collectingAndThen(
Collectors.toCollection(LinkedList::new),
MappingsUtil::docTypeFieldsHierarchicalToMappings)
);
}

public static Map<MappingsKey, Object> docTypeFieldToMappings(DocTypeField docTypeField) {
return docTypeFieldsToMappings(Set.of(docTypeField));
}

public static Map<MappingsKey, Object> docTypeFieldsToMappings(
Collection<DocTypeField> docTypeFields) {

return createMappingsFlat_(docTypeFields);
}

public static Map<MappingsKey, Object> docTypeFieldsHierarchicalToMappings(
Collection<DocTypeField> docTypeFields) {

return createMappingsHierachical_(
docTypeFields, new LinkedHashMap<>(), "", new MappingsKey("properties"));
}

private static Map<MappingsKey, Object> createMappingsFlat_(
Collection<DocTypeField> docTypeFields) {

LinkedHashMap<MappingsKey, Object> acc = new LinkedHashMap<>();

for (DocTypeField docTypeField : docTypeFields) {

String fieldName = docTypeField.getFieldName();

FieldType fieldType = docTypeField.getFieldType();

boolean isObject = fieldType == FieldType.OBJECT || fieldType == FieldType.I18N;
boolean isKeyword = fieldType == FieldType.KEYWORD;

String[] fieldNamesArray = fieldName.split("\\.");

Map<MappingsKey, Object> current = (Map<MappingsKey, Object>)
acc.computeIfAbsent(new MappingsKey("properties"), k -> new LinkedHashMap<>());

for (int i = 0; i < fieldNamesArray.length; i++) {

String currentFieldName = fieldNamesArray[i];

boolean isLast = i == fieldNamesArray.length - 1;
boolean isFields =
(fieldType == FieldType.KEYWORD || fieldType == FieldType.SEARCH_AS_YOU_TYPE)
&& i == fieldNamesArray.length - 2;

current = (Map<MappingsKey, Object>) current.computeIfAbsent(
new MappingsKey(currentFieldName), k -> new LinkedHashMap<>());

if (!isLast) {
current = (Map<MappingsKey, Object>) current.computeIfAbsent(
new MappingsKey(isFields ? "fields" : "properties"),
k -> new LinkedHashMap<>());
}

if (isLast) {

if (!isObject) {
current.put(new MappingsKey("type"), fieldType.getType());

Analyzer analyzer = docTypeField.getAnalyzer();

if (analyzer != null) {
current.put(new MappingsKey("analyzer"), analyzer.getName());
}

String fieldConfig = docTypeField.getJsonConfig();

if (fieldConfig != null) {
JsonObject fieldConfigJson = new JsonObject(fieldConfig);
for (Map.Entry<String, Object> entry : fieldConfigJson) {
current.putIfAbsent(
new MappingsKey(entry.getKey()), entry.getValue());
}
}
}

}

}

}

return acc;
return createMappings_(
docTypeFields
.stream()
.filter(docTypeField -> docTypeField.getParentDocTypeField() == null)
.collect(Collectors.toList()),
new LinkedHashMap<>(),
"",
new MappingsKey("properties")
);
}

private static Map<MappingsKey, Object> createMappingsHierachical_(
private static Map<MappingsKey, Object> createMappings_(
Collection<DocTypeField> docTypeFields,
Map<MappingsKey, Object> acc, String accPath, MappingsKey nextKey) {

Expand Down Expand Up @@ -355,7 +274,7 @@ private static Map<MappingsKey, Object> createMappingsHierachical_(
Set<DocTypeField> subDocTypeFields = docTypeField.getSubDocTypeFields();

if (subDocTypeFields != null) {
createMappingsHierachical_(
createMappings_(
subDocTypeFields,
current,
accPath.isEmpty()
Expand Down
Loading

0 comments on commit 06e3c9b

Please sign in to comment.