Skip to content
Draft
Show file tree
Hide file tree
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 @@ -51,6 +51,7 @@
import org.elasticsearch.index.IndexVersion;
import org.elasticsearch.index.mapper.BlockLoader;
import org.elasticsearch.index.mapper.FieldNamesFieldMapper;
import org.elasticsearch.index.mapper.IgnoredSourceFieldMapper;
import org.elasticsearch.index.mapper.IndexType;
import org.elasticsearch.index.mapper.KeywordFieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
Expand Down Expand Up @@ -259,6 +260,11 @@ public String parentField(String field) {
public FieldNamesFieldMapper.FieldNamesFieldType fieldNames() {
return FieldNamesFieldMapper.FieldNamesFieldType.get(true);
}

@Override
public IgnoredSourceFieldMapper.IgnoredSourceFormat ignoredSourceFormat() {
throw new UnsupportedOperationException();
}
});
}
throw new IllegalArgumentException("can't read [" + name + "]");
Expand Down Expand Up @@ -368,7 +374,7 @@ public void benchmark() {
blockFactory,
ByteSizeValue.ofMb(1).getBytes(),
fields(name),
List.of(new ValuesSourceReaderOperator.ShardContext(reader, () -> {
List.of(new ValuesSourceReaderOperator.ShardContext(reader, (sourcePaths) -> {
throw new UnsupportedOperationException("can't load _source here");
}, EsqlPlugin.STORED_FIELDS_SEQUENTIAL_PROPORTION.getDefault(Settings.EMPTY))),
0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.elasticsearch.index.mapper.CompositeSyntheticFieldLoader;
import org.elasticsearch.index.mapper.DocumentParserContext;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.IgnoredSourceFieldMapper;
import org.elasticsearch.index.mapper.KeywordFieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.MapperBuilderContext;
Expand Down Expand Up @@ -605,7 +606,7 @@ protected String delegatingTo() {
}

// fallback to _source (synthetic or not)
SourceValueFetcher fetcher = SourceValueFetcher.toString(blContext.sourcePaths(name()));
SourceValueFetcher fetcher = SourceValueFetcher.toString(blContext.sourcePaths(name()), blContext.ignoredSourceFormat());
// MatchOnlyText never has norms, so we have to use the field names field
BlockSourceReader.LeafIteratorLookup lookup = BlockSourceReader.lookupFromFieldNames(blContext.fieldNames(), name());
return new BlockSourceReader.BytesRefsBlockLoader(fetcher, lookup);
Expand Down Expand Up @@ -636,7 +637,10 @@ protected BytesRef storedToBytesRef(Object stored) {
return new SourceValueFetcherSortedBinaryIndexFieldData.Builder(
name(),
CoreValuesSourceType.KEYWORD,
SourceValueFetcher.toString(fieldDataContext.sourcePathsLookup().apply(name())),
SourceValueFetcher.toString(
fieldDataContext.sourcePathsLookup().apply(name()),
IgnoredSourceFieldMapper.IgnoredSourceFormat.NO_IGNORED_SOURCE
),
fieldDataContext.lookupSupplier().get(),
TextDocValuesField::new
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,12 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format)
if (format != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats.");
}
return sourceValueFetcher(context.isSourceEnabled() ? context.sourcePath(name()) : Collections.emptySet());
return sourceValueFetcher(context);
}

private SourceValueFetcher sourceValueFetcher(Set<String> sourcePaths) {
return new SourceValueFetcher(sourcePaths, nullValue) {
private SourceValueFetcher sourceValueFetcher(SearchExecutionContext context) {
Set<String> sourcePaths = context.isSourceEnabled() ? context.sourcePath(name()) : Collections.emptySet();
return new SourceValueFetcher(sourcePaths, nullValue, context.ignoredSourceFormat()) {
@Override
protected Object parseSourceValue(Object value) {
return objectToFloat(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ public Builder builder(BlockFactory factory, int expectedCount) {
};
}

ValueFetcher valueFetcher = sourceValueFetcher(blContext.sourcePaths(name()));
ValueFetcher valueFetcher = sourceValueFetcher(blContext.sourcePaths(name()), blContext.ignoredSourceFormat());
BlockSourceReader.LeafIteratorLookup lookup = hasDocValues() == false && isStored()
// We only write the field names field if there aren't doc values
? BlockSourceReader.lookupFromFieldNames(blContext.fieldNames(), name())
Expand Down Expand Up @@ -487,10 +487,18 @@ public IndexFieldData.Builder fielddataBuilder(FieldDataContext fieldDataContext
SearchLookup searchLookup = fieldDataContext.lookupSupplier().get();
Set<String> sourcePaths = fieldDataContext.sourcePathsLookup().apply(name());

IgnoredSourceFieldMapper.IgnoredSourceFormat ignoredSourceFormat;
if (isSyntheticSource) {
ignoredSourceFormat = IgnoredSourceFieldMapper.ignoredSourceFormat(
fieldDataContext.indexSettings().getIndexVersionCreated()
);
} else {
ignoredSourceFormat = IgnoredSourceFieldMapper.IgnoredSourceFormat.NO_IGNORED_SOURCE;
}
return new SourceValueFetcherSortedDoubleIndexFieldData.Builder(
name(),
valuesSourceType,
sourceValueFetcher(sourcePaths),
sourceValueFetcher(sourcePaths, ignoredSourceFormat),
searchLookup,
ScaledFloatDocValuesField::new
);
Expand All @@ -504,11 +512,17 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format)
if (format != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats.");
}
return sourceValueFetcher(context.isSourceEnabled() ? context.sourcePath(name()) : Collections.emptySet());
return sourceValueFetcher(
context.isSourceEnabled() ? context.sourcePath(name()) : Collections.emptySet(),
context.ignoredSourceFormat()
);
}

private SourceValueFetcher sourceValueFetcher(Set<String> sourcePaths) {
return new SourceValueFetcher(sourcePaths, nullValue) {
private SourceValueFetcher sourceValueFetcher(
Set<String> sourcePaths,
IgnoredSourceFieldMapper.IgnoredSourceFormat ignoredSourceFormat
) {
return new SourceValueFetcher(sourcePaths, nullValue, ignoredSourceFormat) {
@Override
protected Double parseSourceValue(Object value) {
double doubleValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
Expand Down Expand Up @@ -194,7 +195,7 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format)
return new DocValueFetcher(
docValueFormat(format, null),
context.getForField(this, FielddataOperation.SEARCH),
StoredFieldsSpec.NEEDS_SOURCE // for now we assume runtime fields need source
StoredFieldsSpec.withSourcePaths(context.ignoredSourceFormat(), Set.of(name())) // for now we assume runtime fields need source
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
public abstract class ArraySourceValueFetcher implements ValueFetcher {
private final Set<String> sourcePaths;
private final @Nullable Object nullValue;
private final IgnoredSourceFieldMapper.IgnoredSourceFormat ignoredSourceFormat;

public ArraySourceValueFetcher(String fieldName, SearchExecutionContext context) {
this(fieldName, context, null);
Expand All @@ -43,6 +44,7 @@ public ArraySourceValueFetcher(String fieldName, SearchExecutionContext context)
public ArraySourceValueFetcher(String fieldName, SearchExecutionContext context, Object nullValue) {
this.sourcePaths = context.isSourceEnabled() ? context.sourcePath(fieldName) : Collections.emptySet();
this.nullValue = nullValue;
this.ignoredSourceFormat = context.ignoredSourceFormat();
}

/**
Expand All @@ -52,6 +54,7 @@ public ArraySourceValueFetcher(String fieldName, SearchExecutionContext context,
public ArraySourceValueFetcher(Set<String> sourcePaths, Object nullValue) {
this.sourcePaths = sourcePaths;
this.nullValue = nullValue;
this.ignoredSourceFormat = IgnoredSourceFieldMapper.IgnoredSourceFormat.NO_IGNORED_SOURCE;
}

@Override
Expand All @@ -76,7 +79,7 @@ public List<Object> fetchValues(Source source, int doc, List<Object> ignoredValu

@Override
public StoredFieldsSpec storedFieldsSpec() {
return StoredFieldsSpec.NEEDS_SOURCE;
return StoredFieldsSpec.withSourcePaths(ignoredSourceFormat, sourcePaths);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public final ColumnAtATimeReader columnAtATimeReader(LeafReaderContext context)

@Override
public final StoredFieldsSpec rowStrideStoredFieldSpec() {
return StoredFieldsSpec.NEEDS_SOURCE;
return fetcher.storedFieldsSpec();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;

import static org.elasticsearch.index.mapper.FieldArrayContext.getOffsetsFieldName;

Expand Down Expand Up @@ -203,11 +204,22 @@ private FieldValues<Boolean> scriptValues() {
return null;
}
BooleanFieldScript.Factory scriptFactory = scriptCompiler.compile(script.get(), BooleanFieldScript.CONTEXT);
return scriptFactory == null
? null
: (lookup, ctx, doc, consumer) -> scriptFactory.newFactory(leafName(), script.get().getParams(), lookup, OnScriptError.FAIL)
.newInstance(ctx)
.runForDoc(doc, consumer);
if (scriptFactory == null) {
return null;
}
return new FieldValues<>() {
@Override
public void valuesForDoc(SearchLookup lookup, LeafReaderContext ctx, int doc, Consumer<Boolean> consumer) {
scriptFactory.newFactory(leafName(), script.get().getParams(), lookup, OnScriptError.FAIL)
.newInstance(ctx)
.runForDoc(doc, consumer);
}

@Override
public String name() {
return leafName();
}
};
}
}

Expand Down Expand Up @@ -281,11 +293,17 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format)
if (this.scriptValues != null) {
return FieldValues.valueFetcher(this.scriptValues, context);
}
return sourceValueFetcher(context.isSourceEnabled() ? context.sourcePath(name()) : Collections.emptySet());
return sourceValueFetcher(
context.isSourceEnabled() ? context.sourcePath(name()) : Collections.emptySet(),
context.ignoredSourceFormat()
);
}

private SourceValueFetcher sourceValueFetcher(Set<String> sourcePaths) {
return new SourceValueFetcher(sourcePaths, nullValue) {
private SourceValueFetcher sourceValueFetcher(
Set<String> sourcePaths,
IgnoredSourceFieldMapper.IgnoredSourceFormat ignoredSourceFormat
) {
return new SourceValueFetcher(sourcePaths, nullValue, ignoredSourceFormat) {
@Override
protected Boolean parseSourceValue(Object value) {
if (value instanceof Boolean) {
Expand Down Expand Up @@ -364,7 +382,7 @@ public Builder builder(BlockFactory factory, int expectedCount) {
};
}

ValueFetcher fetcher = sourceValueFetcher(blContext.sourcePaths(name()));
ValueFetcher fetcher = sourceValueFetcher(blContext.sourcePaths(name()), blContext.ignoredSourceFormat());
BlockSourceReader.LeafIteratorLookup lookup = indexType.hasTerms() || isStored()
? BlockSourceReader.lookupFromFieldNames(blContext.fieldNames(), name())
: BlockSourceReader.lookupMatchingAll();
Expand Down Expand Up @@ -427,11 +445,18 @@ public IndexFieldData.Builder fielddataBuilder(FieldDataContext fieldDataContext
if (operation == FielddataOperation.SCRIPT) {
SearchLookup searchLookup = fieldDataContext.lookupSupplier().get();
Set<String> sourcePaths = fieldDataContext.sourcePathsLookup().apply(name());

IgnoredSourceFieldMapper.IgnoredSourceFormat ignoredSourceFormat;
if (isSyntheticSource) {
ignoredSourceFormat = IgnoredSourceFieldMapper.ignoredSourceFormat(
fieldDataContext.indexSettings().getIndexVersionCreated()
);
} else {
ignoredSourceFormat = IgnoredSourceFieldMapper.IgnoredSourceFormat.NO_IGNORED_SOURCE;
}
return new SourceValueFetcherSortedBooleanIndexFieldData.Builder(
name(),
CoreValuesSourceType.BOOLEAN,
sourceValueFetcher(sourcePaths),
sourceValueFetcher(sourcePaths, ignoredSourceFormat),
searchLookup,
BooleanDocValuesField::new
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.LongSupplier;

Expand Down Expand Up @@ -368,15 +369,22 @@ private FieldValues<Long> scriptValues() {
return null;
}
DateFieldScript.Factory factory = scriptCompiler.compile(script.get(), DateFieldScript.CONTEXT);
return factory == null
? null
: (lookup, ctx, doc, consumer) -> factory.newFactory(
leafName(),
script.get().getParams(),
lookup,
buildFormatter(),
OnScriptError.FAIL
).newInstance(ctx).runForDoc(doc, consumer::accept);
if (factory == null) {
return null;
}
return new FieldValues<>() {
@Override
public void valuesForDoc(SearchLookup lookup, LeafReaderContext ctx, int doc, Consumer<Long> consumer) {
factory.newFactory(leafName(), script.get().getParams(), lookup, buildFormatter(), OnScriptError.FAIL)
.newInstance(ctx)
.runForDoc(doc, consumer::accept);
}

@Override
public String name() {
return leafName();
}
};
}

@Override
Expand Down Expand Up @@ -662,8 +670,11 @@ public String parseSourceValue(Object value) {
}

// returns a Long to support source fallback which emulates numeric doc values for dates
private SourceValueFetcher sourceValueFetcher(Set<String> sourcePaths) {
return new SourceValueFetcher(sourcePaths, nullValue) {
private SourceValueFetcher sourceValueFetcher(
Set<String> sourcePaths,
IgnoredSourceFieldMapper.IgnoredSourceFormat ignoredSourceFormat
) {
return new SourceValueFetcher(sourcePaths, nullValue, ignoredSourceFormat) {
@Override
public Long parseSourceValue(Object value) {
String date = value instanceof Number ? NUMBER_FORMAT.format(value) : value.toString();
Expand Down Expand Up @@ -999,7 +1010,10 @@ public Builder builder(BlockFactory factory, int expectedCount) {
BlockSourceReader.LeafIteratorLookup lookup = isStored() || indexType.hasPoints()
? BlockSourceReader.lookupFromFieldNames(blContext.fieldNames(), name())
: BlockSourceReader.lookupMatchingAll();
return new BlockSourceReader.LongsBlockLoader(sourceValueFetcher(blContext.sourcePaths(name())), lookup);
return new BlockSourceReader.LongsBlockLoader(
sourceValueFetcher(blContext.sourcePaths(name()), blContext.ignoredSourceFormat()),
lookup
);
}

private FallbackSyntheticSourceBlockLoader.Reader<?> fallbackSyntheticSourceBlockLoaderReader() {
Expand Down Expand Up @@ -1063,11 +1077,18 @@ public IndexFieldData.Builder fielddataBuilder(FieldDataContext fieldDataContext
if (operation == FielddataOperation.SCRIPT) {
SearchLookup searchLookup = fieldDataContext.lookupSupplier().get();
Set<String> sourcePaths = fieldDataContext.sourcePathsLookup().apply(name());

IgnoredSourceFieldMapper.IgnoredSourceFormat ignoredSourceFormat;
if (isSyntheticSource) {
ignoredSourceFormat = IgnoredSourceFieldMapper.ignoredSourceFormat(
fieldDataContext.indexSettings().getIndexVersionCreated()
);
} else {
ignoredSourceFormat = IgnoredSourceFieldMapper.IgnoredSourceFormat.NO_IGNORED_SOURCE;
}
return new SourceValueFetcherSortedNumericIndexFieldData.Builder(
name(),
resolution.numericType().getValuesSourceType(),
sourceValueFetcher(sourcePaths),
sourceValueFetcher(sourcePaths, ignoredSourceFormat),
searchLookup,
resolution.getDefaultToScriptFieldFactory()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public RowStrideReader rowStrideReader(LeafReaderContext context) throws IOExcep

@Override
public StoredFieldsSpec rowStrideStoredFieldSpec() {
return new StoredFieldsSpec(false, false, Set.of(), new IgnoredFieldsSpec(Set.of(fieldName), ignoredSourceFormat));
return new StoredFieldsSpec(false, false, Set.of(), new IgnoredFieldsSpec(Set.of(fieldName), ignoredSourceFormat), null);
}

@Override
Expand Down
Loading