in aws-kendra-index/src/main/java/software/amazon/kendra/index/Translator.java [216:264]
static List<DocumentMetadataConfiguration> translateToSdkDocumentMetadataConfigurationList(
List<software.amazon.kendra.index.DocumentMetadataConfiguration> curr,
List<software.amazon.kendra.index.DocumentMetadataConfiguration> prev) throws TranslatorValidationException {
Map<String, String> previousMetadataNames = new HashMap<>();
if (prev != null && !prev.isEmpty()) {
previousMetadataNames = prev.stream().collect(Collectors.toMap(x -> x.getName(), x -> x.getType()));
}
Set<String> currMetadataNames = new HashSet<>();
if (curr != null && !curr.isEmpty()) {
currMetadataNames = curr.stream().map(x -> x.getName()).collect(Collectors.toSet());
}
List<DocumentMetadataConfiguration> sdkDefaultAttributes = new ArrayList<>();
for (Map.Entry<String, String> entry : previousMetadataNames.entrySet()) {
// If the attribute is a reserved one (i.e. it is prefixed with "_") ...
if (entry.getKey().startsWith("_")) {
// and it's not in the requested CloudFormation template,
// then provide the default value. This allows customers to add and remove
// reserved attributes. When removed, we set/reset the attribute to it's default
if (!currMetadataNames.contains(entry.getKey())) {
sdkDefaultAttributes.add(
DocumentMetadataConfiguration
.builder()
.name(entry.getKey())
.type(entry.getValue())
.search((Search) null)
.relevance((Relevance) null)
.build());
}
} else {
// otherwise it's a custom field. We don't allow customers to remove
// custom fields from their CloudFormation template so check for that here.
if (!currMetadataNames.contains(entry.getKey())) {
throw new TranslatorValidationException(
String.format("Custom attribute %s cannot be removed", entry.getKey()));
}
}
}
// Document metadata configuration directly defined/requested in the CloudFormation template
List<DocumentMetadataConfiguration> sdkAttributesDefinedInCFTemplate =
translateToSdkDocumentMetadataConfigurationList(curr);
return Stream.concat(
sdkAttributesDefinedInCFTemplate.stream(),
sdkDefaultAttributes.stream())
.collect(Collectors.toList());
}