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

Commit

Permalink
Fix nested field issue (#277)
Browse files Browse the repository at this point in the history
Previously, our validation logic cannot parse nested fields from the getFieldMapping response because nested fields don't have the full name in the response.  For example, the field nested_host.host only has "host" in the type mapping instead of "nested_host.host".  This PR fixes that by not depending on field name when parsing the name type mapping.
Testing done:
1. manual testing passes.
  • Loading branch information
kaituo authored Oct 19, 2020
1 parent 3a9d6c9 commit 9a8d1f2
Showing 1 changed file with 20 additions and 8 deletions.
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

0 comments on commit 9a8d1f2

Please sign in to comment.