diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolver.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolver.java index 5b5657e8773ea..a01d3f5af6e33 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolver.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolver.java @@ -59,13 +59,12 @@ import java.util.function.Supplier; import java.util.regex.Pattern; -import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static java.util.Collections.emptyMap; -import static java.util.Collections.emptySet; import static org.elasticsearch.action.ActionListener.wrap; import static org.elasticsearch.common.Strings.hasText; import static org.elasticsearch.common.regex.Regex.simpleMatch; +import static org.elasticsearch.common.util.CollectionUtils.arrayAsArrayList; import static org.elasticsearch.transport.RemoteClusterAware.buildRemoteIndexName; import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME; import static org.elasticsearch.xpack.ql.type.DataTypes.KEYWORD; @@ -659,15 +658,12 @@ private static List buildIndices( } } - List resolvedIndices = new ArrayList<>(asList(fieldCapsResponse.getIndices())); - int mapSize = CollectionUtils.mapSize(resolvedIndices.size() + resolvedAliases.size()); - Map indices = Maps.newLinkedHashMapWithExpectedSize(mapSize); + final List resolvedIndices = arrayAsArrayList(fieldCapsResponse.getIndices()); + Map indices = Maps.newLinkedHashMapWithExpectedSize(resolvedIndices.size() + resolvedAliases.size()); Pattern pattern = javaRegex != null ? Pattern.compile(javaRegex) : null; // sort fields in reverse order to build the field hierarchy - Set>> sortedFields = new TreeSet<>( - Collections.reverseOrder(Comparator.comparing(Entry::getKey)) - ); + Set>> sortedFields = new TreeSet<>(Collections.reverseOrder(Entry.comparingByKey())); final Map> fieldCaps = fieldCapsResponse.get(); sortedFields.addAll(fieldCaps.entrySet()); @@ -682,29 +678,18 @@ private static List buildIndices( // apply verification for fields belonging to index aliases Map invalidFieldsForAliases = getInvalidFieldsForAliases(fieldName, types, aliases); - // filter unmapped - FieldCapabilities unmapped = types.get(UNMAPPED); - Set unmappedIndices = unmapped != null ? new HashSet<>(asList(unmapped.indices())) : emptySet(); - // check each type for (Entry typeEntry : types.entrySet()) { + if (UNMAPPED.equals(typeEntry.getKey())) { + continue; + } FieldCapabilities typeCap = typeEntry.getValue(); String[] capIndices = typeCap.indices(); // compute the actual indices - if any are specified, take into account the unmapped indices - List concreteIndices = null; + final List concreteIndices; if (capIndices != null) { - if (unmappedIndices.isEmpty()) { - concreteIndices = new ArrayList<>(asList(capIndices)); - } else { - concreteIndices = new ArrayList<>(capIndices.length); - for (String capIndex : capIndices) { - // add only indices that have a mapping - if (unmappedIndices.contains(capIndex) == false) { - concreteIndices.add(capIndex); - } - } - } + concreteIndices = arrayAsArrayList(capIndices); } else { concreteIndices = resolvedIndices; } @@ -727,11 +712,7 @@ private static List buildIndices( // TODO is split still needed? if (pattern == null || pattern.matcher(splitQualifiedIndex(index).v2()).matches() || isIndexAlias) { String indexName = isIndexAlias ? index : indexNameProcessor.apply(index); - Fields indexFields = indices.get(indexName); - if (indexFields == null) { - indexFields = new Fields(); - indices.put(indexName, indexFields); - } + Fields indexFields = indices.computeIfAbsent(indexName, k -> new Fields()); EsField field = indexFields.flattedMapping.get(fieldName); boolean createField = false; if (isIndexAlias == false) {