use of org.elasticsearch.cluster.metadata.MappingMetadata in project metron by apache.
the class ElasticsearchColumnMetadataDao method getColumnMetadata.
@SuppressWarnings("unchecked")
@Override
public Map<String, FieldType> getColumnMetadata(List<String> indices) throws IOException {
Map<String, FieldType> indexColumnMetadata = new HashMap<>();
Map<String, String> previousIndices = new HashMap<>();
Set<String> fieldBlackList = new HashSet<>();
String[] latestIndices = getLatestIndices(indices);
if (latestIndices.length > 0) {
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings = adminClient.indices().getMappings(new GetMappingsRequest().indices(latestIndices)).actionGet().getMappings();
// for each index
for (Object key : mappings.keys().toArray()) {
String indexName = key.toString();
ImmutableOpenMap<String, MappingMetaData> mapping = mappings.get(indexName);
// for each mapping in the index
Iterator<String> mappingIterator = mapping.keysIt();
while (mappingIterator.hasNext()) {
MappingMetaData mappingMetaData = mapping.get(mappingIterator.next());
Map<String, Object> sourceAsMap = mappingMetaData.getSourceAsMap();
if (sourceAsMap.containsKey("properties")) {
Map<String, Map<String, String>> map = (Map<String, Map<String, String>>) sourceAsMap.get("properties");
// for each field in the mapping
for (String field : map.keySet()) {
if (!fieldBlackList.contains(field)) {
FieldType type = toFieldType(map.get(field).get("type"));
if (!indexColumnMetadata.containsKey(field)) {
indexColumnMetadata.put(field, type);
// record the last index in which a field exists, to be able to print helpful error message on type mismatch
previousIndices.put(field, indexName);
} else {
FieldType previousType = indexColumnMetadata.get(field);
if (!type.equals(previousType)) {
String previousIndexName = previousIndices.get(field);
LOG.error(String.format("Field type mismatch: %s.%s has type %s while %s.%s has type %s. Defaulting type to %s.", indexName, field, type.getFieldType(), previousIndexName, field, previousType.getFieldType(), FieldType.OTHER.getFieldType()));
indexColumnMetadata.put(field, FieldType.OTHER);
// the field is defined in multiple indices with different types; ignore the field as type has been set to OTHER
fieldBlackList.add(field);
}
}
}
}
}
}
}
} else {
LOG.info(String.format("Unable to find any latest indices; indices=%s", indices));
}
return indexColumnMetadata;
}
Aggregations