Skip to content
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 @@ -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;
Expand Down Expand Up @@ -659,15 +658,12 @@ private static List<EsIndex> buildIndices(
}
}

List<String> resolvedIndices = new ArrayList<>(asList(fieldCapsResponse.getIndices()));
int mapSize = CollectionUtils.mapSize(resolvedIndices.size() + resolvedAliases.size());
Map<String, Fields> indices = Maps.newLinkedHashMapWithExpectedSize(mapSize);
final List<String> resolvedIndices = arrayAsArrayList(fieldCapsResponse.getIndices());
Map<String, Fields> 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<Entry<String, Map<String, FieldCapabilities>>> sortedFields = new TreeSet<>(
Collections.reverseOrder(Comparator.comparing(Entry::getKey))
);
Set<Entry<String, Map<String, FieldCapabilities>>> sortedFields = new TreeSet<>(Collections.reverseOrder(Entry.comparingByKey()));
final Map<String, Map<String, FieldCapabilities>> fieldCaps = fieldCapsResponse.get();
sortedFields.addAll(fieldCaps.entrySet());

Expand All @@ -682,29 +678,18 @@ private static List<EsIndex> buildIndices(
// apply verification for fields belonging to index aliases
Map<String, InvalidMappedField> invalidFieldsForAliases = getInvalidFieldsForAliases(fieldName, types, aliases);

// filter unmapped
FieldCapabilities unmapped = types.get(UNMAPPED);
Set<String> unmappedIndices = unmapped != null ? new HashSet<>(asList(unmapped.indices())) : emptySet();

// check each type
for (Entry<String, FieldCapabilities> 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<String> concreteIndices = null;
final List<String> 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;
}
Expand All @@ -727,11 +712,7 @@ private static List<EsIndex> 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) {
Expand Down