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

Fix nested field issue #277

Merged
merged 1 commit into from
Oct 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -287,25 +287,37 @@ private void validateCategoricalField(String detectorId) {
// example getMappingsResponse:
// GetFieldMappingsResponse{mappings={server-metrics={_doc={service=FieldMappingMetadata{fullName='service',
// source=org.elasticsearch.common.bytes.BytesArray@7ba87dbd}}}}}
// for nested field, it would be
// GetFieldMappingsResponse{mappings={server-metrics={_doc={host_nest.host2=FieldMappingMetadata{fullName='host_nest.host2',
// source=org.elasticsearch.common.bytes.BytesArray@8fb4de08}}}}}
boolean foundField = false;
Map<String, Map<String, Map<String, FieldMappingMetadata>>> mappingsByIndex = getMappingsResponse.mappings();

for (Map<String, Map<String, FieldMappingMetadata>> mappingsByType : mappingsByIndex.values()) {
for (Map<String, FieldMappingMetadata> mappingsByField : mappingsByType.values()) {
for (Map.Entry<String, FieldMappingMetadata> field2Metadata : mappingsByField.entrySet()) {
// example output:
// host_nest.host2=FieldMappingMetadata{fullName='host_nest.host2',
// source=org.elasticsearch.common.bytes.BytesArray@8fb4de08}
FieldMappingMetadata fieldMetadata = field2Metadata.getValue();

if (fieldMetadata != null) {
Object metadata = fieldMetadata.sourceAsMap().get(categoryField0);
if (metadata != null && metadata instanceof Map) {
foundField = true;
Map<String, Object> metadataMap = (Map<String, Object>) metadata;
String typeName = (String) metadataMap.get(CommonName.TYPE);
if (!typeName.equals(CommonName.KEYWORD_TYPE) && !typeName.equals(CommonName.IP_TYPE)) {
listener.onFailure(new IllegalArgumentException(CATEGORICAL_FIELD_TYPE_ERR_MSG));
return;
// sourceAsMap returns sth like {host2={type=keyword}} with host2 being a nested field
Map<String, Object> fieldMap = fieldMetadata.sourceAsMap();
if (fieldMap != null) {
for (Object type : fieldMap.values()) {
if (type != null && type instanceof Map) {
foundField = true;
Map<String, Object> metadataMap = (Map<String, Object>) type;
String typeName = (String) metadataMap.get(CommonName.TYPE);
if (!typeName.equals(CommonName.KEYWORD_TYPE) && !typeName.equals(CommonName.IP_TYPE)) {
listener.onFailure(new IllegalArgumentException(CATEGORICAL_FIELD_TYPE_ERR_MSG));
return;
}
}
}
}

}
}
}
Expand Down