Skip to content

Commit

Permalink
Don't rebuild shadowed field lookup on every document (#76023)
Browse files Browse the repository at this point in the history
#75595 added better checks for fields shadowed by runtime fields, so that we
don't index data that would never be searched. However, the shadow lookup
was being rebuilt for every document, which has caused a noticeable regression
in indexing times. This commit reworks things so that this lookup is built once
per mapping update and lives on MappingLookup.
  • Loading branch information
romseygeek authored Aug 3, 2021
1 parent 453877d commit 134dab6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ protected void addDoc(LuceneDocument doc) {
private final Set<String> newFieldsSeen;
private final Map<String, ObjectMapper> dynamicObjectMappers;
private final List<RuntimeField> dynamicRuntimeFields;
private final Set<String> shadowedFields;
private Field version;
private SeqNoFieldMapper.SequenceIDFields seqID;

Expand All @@ -108,7 +107,6 @@ private DocumentParserContext(DocumentParserContext in) {
this.newFieldsSeen = in.newFieldsSeen;
this.dynamicObjectMappers = in.dynamicObjectMappers;
this.dynamicRuntimeFields = in.dynamicRuntimeFields;
this.shadowedFields = in.shadowedFields;
this.version = in.version;
this.seqID = in.seqID;
}
Expand All @@ -129,17 +127,6 @@ protected DocumentParserContext(MappingLookup mappingLookup,
this.newFieldsSeen = new HashSet<>();
this.dynamicObjectMappers = new HashMap<>();
this.dynamicRuntimeFields = new ArrayList<>();
this.shadowedFields = buildShadowedFields(mappingLookup);
}

private static Set<String> buildShadowedFields(MappingLookup lookup) {
Set<String> shadowedFields = new HashSet<>();
for (RuntimeField runtimeField : lookup.getMapping().getRoot().runtimeFields()) {
for (MappedFieldType mft : runtimeField.asMappedFieldTypes()) {
shadowedFields.add(mft.name());
}
}
return shadowedFields;
}

public final IndexSettings indexSettings() {
Expand Down Expand Up @@ -243,7 +230,7 @@ public final List<Mapper> getDynamicMappers() {
}

public final boolean isShadowed(String field) {
return shadowedFields.contains(field);
return mappingLookup.isShadowed(field);
}

public final ObjectMapper getObjectMapper(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -51,6 +52,7 @@ private CacheKey() {}
private final Map<String, NamedAnalyzer> indexAnalyzersMap = new HashMap<>();
private final List<FieldMapper> indexTimeScriptMappers = new ArrayList<>();
private final Mapping mapping;
private final Set<String> shadowedFields;

/**
* Creates a new {@link MappingLookup} instance by parsing the provided mapping and extracting its field definitions.
Expand Down Expand Up @@ -157,6 +159,13 @@ private MappingLookup(Mapping mapping,
}
}

this.shadowedFields = new HashSet<>();
for (RuntimeField runtimeField : mapping.getRoot().runtimeFields()) {
for (MappedFieldType mft : runtimeField.asMappedFieldTypes()) {
shadowedFields.add(mft.name());
}
}

this.fieldTypeLookup = new FieldTypeLookup(mappers, aliasMappers, mapping.getRoot().runtimeFields());
this.indexTimeLookup = new FieldTypeLookup(mappers, aliasMappers, Collections.emptyList());
this.fieldMappers = Collections.unmodifiableMap(fieldMappers);
Expand Down Expand Up @@ -199,6 +208,13 @@ public Iterable<Mapper> fieldMappers() {
return fieldMappers.values();
}

/**
* @return {@code true} if the given field is shadowed by a runtime field
*/
public boolean isShadowed(String field) {
return shadowedFields.contains(field);
}

void checkLimits(IndexSettings settings) {
checkFieldLimit(settings.getMappingTotalFieldsLimit());
checkObjectDepthLimit(settings.getMappingDepthLimit());
Expand Down

0 comments on commit 134dab6

Please sign in to comment.