Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Counted keyword synthetic source inherit keep #120426

Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ static Mapping createDynamicUpdate(DocumentParserContext context) {
for (RuntimeField runtimeField : context.getDynamicRuntimeFields()) {
rootBuilder.addRuntimeField(runtimeField);
}
RootObjectMapper root = rootBuilder.build(MapperBuilderContext.root(context.mappingLookup().isSourceSynthetic(), false));
RootObjectMapper root = rootBuilder.build(
MapperBuilderContext.root(context.mappingLookup().isSourceSynthetic(), context.sourceKeepModeFromIndexSettings(), false)
);
return context.mappingLookup().getMapping().mappingUpdate(root);
}

Expand Down Expand Up @@ -450,7 +452,7 @@ static void parseObjectOrField(DocumentParserContext context, Mapper mapper) thr
parseObjectOrNested(context.createFlattenContext(currentFieldName));
context.path().add(currentFieldName);
} else {
var sourceKeepMode = getSourceKeepMode(context, fieldMapper.sourceKeepMode());
var sourceKeepMode = fieldMapper.sourceKeepMode();
if (context.canAddIgnoredField()
&& (fieldMapper.syntheticSourceMode() == FieldMapper.SyntheticSourceMode.FALLBACK
|| sourceKeepMode == Mapper.SourceKeepMode.ALL
Expand Down Expand Up @@ -698,7 +700,7 @@ private static void parseNonDynamicArray(
boolean fieldWithFallbackSyntheticSource = false;
boolean fieldWithStoredArraySource = false;
if (mapper instanceof FieldMapper fieldMapper) {
mode = getSourceKeepMode(context, fieldMapper.sourceKeepMode());
mode = fieldMapper.sourceKeepMode();
fieldWithFallbackSyntheticSource = fieldMapper.syntheticSourceMode() == FieldMapper.SyntheticSourceMode.FALLBACK;
fieldWithStoredArraySource = mode != Mapper.SourceKeepMode.NONE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -781,15 +781,17 @@ public final MapperBuilderContext createDynamicMapperBuilderContext() {
if (objectMapper instanceof PassThroughObjectMapper passThroughObjectMapper) {
containsDimensions = passThroughObjectMapper.containsDimensions();
}
return new MapperBuilderContext(

var contextParams = new MapperBuilderContext.MapperBuilderContextParams(
p,
mappingLookup.isSourceSynthetic(),
sourceKeepModeFromIndexSettings(),
mappingLookup.isDataStreamTimestampFieldEnabled(),
containsDimensions,
dynamic,
MergeReason.MAPPING_UPDATE,
false
MergeReason.MAPPING_UPDATE
);
return new MapperBuilderContext(contextParams, false);
}

public abstract XContentParser parser();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,30 @@ public abstract class FieldMapper extends Mapper {
/**
* @param multiFields sub fields of this mapper
* @param copyTo copyTo fields of this mapper
* @param sourceKeepMode mode for storing the field source in synthetic source mode
* @param sourceKeepMode mode for storing the field source in synthetic source mode
* @param hasScript whether a script is defined for the field
* @param onScriptError the behaviour for when the defined script fails at runtime
*/
protected record BuilderParams(
MultiFields multiFields,
CopyTo copyTo,
Optional<SourceKeepMode> sourceKeepMode,
SourceKeepMode sourceKeepMode,
Optional<SourceKeepMode> localSourceKeepMode,
boolean hasScript,
OnScriptError onScriptError
) {
public static BuilderParams empty() {
return empty;
}

private static final BuilderParams empty = new BuilderParams(MultiFields.empty(), CopyTo.empty(), Optional.empty(), false, null);
private static final BuilderParams empty = new BuilderParams(
MultiFields.empty(),
CopyTo.empty(),
SourceKeepMode.NONE,
Optional.empty(),
false,
null
);
}

protected final MappedFieldType mappedFieldType;
Expand Down Expand Up @@ -146,10 +154,14 @@ public MultiFields multiFields() {
return builderParams.multiFields;
}

public Optional<SourceKeepMode> sourceKeepMode() {
public SourceKeepMode sourceKeepMode() {
return builderParams.sourceKeepMode;
}

public Optional<SourceKeepMode> localSourceKeepMode() {
return builderParams.localSourceKeepMode;
}

/**
* Will this field ignore malformed values for this field and accept the
* document ({@code true}) or will it reject documents with malformed
Expand Down Expand Up @@ -434,8 +446,8 @@ protected void doXContentBody(XContentBuilder builder, Params params) throws IOE
getMergeBuilder().toXContent(builder, params);
builderParams.multiFields.toXContent(builder, params);
builderParams.copyTo.toXContent(builder);
if (builderParams.sourceKeepMode.isPresent()) {
builderParams.sourceKeepMode.get().toXContent(builder);
if (builderParams.localSourceKeepMode.isPresent()) {
builderParams.localSourceKeepMode.get().toXContent(builder);
}
}

Expand Down Expand Up @@ -1387,7 +1399,14 @@ public Builder init(FieldMapper initializer) {
}

protected BuilderParams builderParams(Mapper.Builder mainFieldBuilder, MapperBuilderContext context) {
return new BuilderParams(multiFieldsBuilder.build(mainFieldBuilder, context), copyTo, sourceKeepMode, hasScript, onScriptError);
return new BuilderParams(
multiFieldsBuilder.build(mainFieldBuilder, context),
copyTo,
sourceKeepMode.orElseGet(context::sourceKeepMode),
sourceKeepMode,
hasScript,
onScriptError
);
}

protected void merge(FieldMapper in, Conflicts conflicts, MapperMergeContext mapperMergeContext) {
Expand All @@ -1399,7 +1418,7 @@ protected void merge(FieldMapper in, Conflicts conflicts, MapperMergeContext map
multiFieldsBuilder.update(newSubField, childContext);
}
this.copyTo = in.builderParams.copyTo;
this.sourceKeepMode = in.builderParams.sourceKeepMode;
this.sourceKeepMode = in.builderParams.localSourceKeepMode;
validate();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.elasticsearch.index.mapper.MapperService.MergeReason;

import java.util.Objects;
import java.util.Optional;

/**
* Holds context for building Mapper objects from their Builders
Expand All @@ -24,37 +25,75 @@ public class MapperBuilderContext {
* The root context, to be used when building a tree of mappers
*/
public static MapperBuilderContext root(boolean isSourceSynthetic, boolean isDataStream) {
return root(isSourceSynthetic, isDataStream, MergeReason.MAPPING_UPDATE);
return root(isSourceSynthetic, Mapper.SourceKeepMode.NONE, isDataStream, MergeReason.MAPPING_UPDATE);
}

public static MapperBuilderContext root(boolean isSourceSynthetic, Mapper.SourceKeepMode sourceKeepMode, boolean isDataStream) {
return root(isSourceSynthetic, sourceKeepMode, isDataStream, MergeReason.MAPPING_UPDATE);
}

public static MapperBuilderContext root(boolean isSourceSynthetic, boolean isDataStream, MergeReason mergeReason) {
return new MapperBuilderContext(null, isSourceSynthetic, isDataStream, false, ObjectMapper.Defaults.DYNAMIC, mergeReason, false);
return root(isSourceSynthetic, Mapper.SourceKeepMode.NONE, isDataStream, mergeReason);
}

public static MapperBuilderContext root(
boolean isSourceSynthetic,
Mapper.SourceKeepMode sourceKeepMode,
boolean isDataStream,
MergeReason mergeReason
) {
var params = new MapperBuilderContextParams(
null,
isSourceSynthetic,
sourceKeepMode,
isDataStream,
false,
ObjectMapper.Defaults.DYNAMIC,
mergeReason
);
return new MapperBuilderContext(params, false);
}

private final String path;
private final boolean isSourceSynthetic;
private final Mapper.SourceKeepMode sourceKeepMode;
private final boolean isDataStream;
private final boolean parentObjectContainsDimensions;
private final ObjectMapper.Dynamic dynamic;
private final MergeReason mergeReason;
private final boolean inNestedContext;

MapperBuilderContext(
/**
* @param path the full name of the field, taking into account parent objects
* @param isSourceSynthetic is the {@code _source} field being reconstructed on the fly?
* @param isDataStream are these mappings being built for a data stream index?
* @param parentObjectContainsDimensions are these field mappings being built dimensions?
* @param dynamic strategy for handling dynamic mappings in this context
* @param mergeReason the merge reason to use when merging mappers while building the mapper
*/
public record MapperBuilderContextParams(
String path,
boolean isSourceSynthetic,
Mapper.SourceKeepMode sourceKeepMode,
boolean isDataStream,
boolean parentObjectContainsDimensions,
ObjectMapper.Dynamic dynamic,
MergeReason mergeReason,
boolean inNestedContext
MergeReason mergeReason
) {
Objects.requireNonNull(dynamic, "dynamic must not be null");
this.path = path;
this.isSourceSynthetic = isSourceSynthetic;
this.isDataStream = isDataStream;
this.parentObjectContainsDimensions = parentObjectContainsDimensions;
this.dynamic = dynamic;
this.mergeReason = mergeReason;
public MapperBuilderContextParams {
Objects.requireNonNull(dynamic, "dynamic must not be null");
}
}

MapperBuilderContext(MapperBuilderContextParams params, boolean inNestedContext) {
Objects.requireNonNull(params, "params must not be null");
this.path = params.path;
this.isSourceSynthetic = params.isSourceSynthetic;
this.sourceKeepMode = params.sourceKeepMode;
this.isDataStream = params.isDataStream;
this.parentObjectContainsDimensions = params.parentObjectContainsDimensions;
this.dynamic = params.dynamic;
this.mergeReason = params.mergeReason;
this.inNestedContext = inNestedContext;
}

Expand All @@ -66,7 +105,23 @@ public static MapperBuilderContext root(boolean isSourceSynthetic, boolean isDat
* @return a new MapperBuilderContext with this context as its parent
*/
public MapperBuilderContext createChildContext(String name, @Nullable ObjectMapper.Dynamic dynamic) {
return createChildContext(name, this.parentObjectContainsDimensions, dynamic);
return createChildContext(name, this.parentObjectContainsDimensions, dynamic, Optional.empty());
}

/**
* Creates a new MapperBuilderContext that is a child of this context
*
* @param name the name of the child context
* @param dynamic strategy for handling dynamic mappings in this context
* @param sourceKeepMode the synthetic_source_keep mapping setting configured on the child
* @return a new MapperBuilderContext with this context as its parent
*/
public MapperBuilderContext createChildContext(
String name,
@Nullable ObjectMapper.Dynamic dynamic,
Optional<Mapper.SourceKeepMode> sourceKeepMode
) {
return createChildContext(name, this.parentObjectContainsDimensions, dynamic, sourceKeepMode);
}

/**
Expand All @@ -75,22 +130,42 @@ public MapperBuilderContext createChildContext(String name, @Nullable ObjectMapp
* @param name the name of the child context
* @param dynamic strategy for handling dynamic mappings in this context
* @param parentObjectContainsDimensions whether the parent object contains dimensions
* @param sourceKeepMode the synthetic_source_keep mapping setting configured on the child
* @return a new MapperBuilderContext with this context as its parent
*/
public MapperBuilderContext createChildContext(
String name,
boolean parentObjectContainsDimensions,
@Nullable ObjectMapper.Dynamic dynamic
@Nullable ObjectMapper.Dynamic dynamic,
Optional<Mapper.SourceKeepMode> sourceKeepMode
) {
return new MapperBuilderContext(
var params = new MapperBuilderContextParams(
buildFullName(name),
this.isSourceSynthetic,
sourceKeepMode.orElseGet(this::sourceKeepMode),
this.isDataStream,
parentObjectContainsDimensions,
getDynamic(dynamic),
this.mergeReason,
isInNestedContext()
this.mergeReason
);

return new MapperBuilderContext(params, isInNestedContext());
}

/**
* Creates a new MapperBuilderContext that is a child of this context
*
* @param name the name of the child context
* @param dynamic strategy for handling dynamic mappings in this context
* @param parentObjectContainsDimensions whether the parent object contains dimensions
* @return a new MapperBuilderContext with this context as its parent
*/
public MapperBuilderContext createChildContext(
String name,
boolean parentObjectContainsDimensions,
@Nullable ObjectMapper.Dynamic dynamic
) {
return createChildContext(name, parentObjectContainsDimensions, dynamic, Optional.empty());
}

protected ObjectMapper.Dynamic getDynamic(@Nullable ObjectMapper.Dynamic dynamic) {
Expand All @@ -114,6 +189,13 @@ public boolean isSourceSynthetic() {
return isSourceSynthetic;
}

/**
* Should the {@code _source} field be stored to avoid synthetic source modifications?
*/
public Mapper.SourceKeepMode sourceKeepMode() {
return sourceKeepMode;
}

/**
* Are these mappings being built for a data stream index?
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import org.elasticsearch.index.mapper.MapperService.MergeReason;

import java.util.Optional;

/**
* Holds context used when merging mappings.
* As the merge process also involves building merged {@link Mapper.Builder}s,
Expand All @@ -29,9 +31,22 @@ private MapperMergeContext(MapperBuilderContext mapperBuilderContext, NewFieldsB
/**
* The root context, to be used when merging a tree of mappers
*/
public static MapperMergeContext root(
boolean isSourceSynthetic,
Mapper.SourceKeepMode sourceKeepMode,
boolean isDataStream,
MergeReason mergeReason,
long newFieldsBudget
) {
return new MapperMergeContext(
MapperBuilderContext.root(isSourceSynthetic, sourceKeepMode, isDataStream, mergeReason),
NewFieldsBudget.of(newFieldsBudget)
);
}

public static MapperMergeContext root(boolean isSourceSynthetic, boolean isDataStream, MergeReason mergeReason, long newFieldsBudget) {
return new MapperMergeContext(
MapperBuilderContext.root(isSourceSynthetic, isDataStream, mergeReason),
MapperBuilderContext.root(isSourceSynthetic, Mapper.SourceKeepMode.NONE, isDataStream, mergeReason),
NewFieldsBudget.of(newFieldsBudget)
);
}
Expand All @@ -50,10 +65,27 @@ public static MapperMergeContext from(MapperBuilderContext mapperBuilderContext,
* Creates a new {@link MapperMergeContext} with a child {@link MapperBuilderContext}.
* The child {@link MapperMergeContext} context will share the same field limit.
* @param name the name of the child context
* @param dynamic strategy for handling dynamic mappings in this context
* @param sourceKeepMode the synthetic_source_keep mapping setting configured on the child
* @return a new {@link MapperMergeContext} with this context as its parent
*/
public MapperMergeContext createChildContext(
String name,
ObjectMapper.Dynamic dynamic,
Optional<Mapper.SourceKeepMode> sourceKeepMode
) {
return createChildContext(mapperBuilderContext.createChildContext(name, dynamic, sourceKeepMode));
}

/**
* Creates a new {@link MapperMergeContext} with a child {@link MapperBuilderContext}.
* The child {@link MapperMergeContext} context will share the same field limit.
* @param name the name of the child context
* @param dynamic strategy for handling dynamic mappings in this context
* @return a new {@link MapperMergeContext} with this context as its parent
*/
public MapperMergeContext createChildContext(String name, ObjectMapper.Dynamic dynamic) {
return createChildContext(mapperBuilderContext.createChildContext(name, dynamic));
return createChildContext(mapperBuilderContext.createChildContext(name, dynamic, Optional.empty()));
}

/**
Expand Down
Loading