Skip to content
Draft
Show file tree
Hide file tree
Changes from 15 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 @@ -368,7 +368,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 @@ -605,7 +605,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.indexSettings());
// 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 +636,7 @@ protected BytesRef storedToBytesRef(Object stored) {
return new SourceValueFetcherSortedBinaryIndexFieldData.Builder(
name(),
CoreValuesSourceType.KEYWORD,
SourceValueFetcher.toString(fieldDataContext.sourcePathsLookup().apply(name())),
SourceValueFetcher.toString(fieldDataContext.sourcePathsLookup().apply(name()), fieldDataContext.indexSettings()),
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.getIndexSettings().getIgnoredSourceFormat()) {
@Override
protected Object parseSourceValue(Object value) {
return objectToFloat(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.index.IndexMode;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.IndexVersion;
import org.elasticsearch.index.IndexVersions;
import org.elasticsearch.index.fielddata.FieldData;
Expand Down Expand Up @@ -263,6 +264,7 @@ public ScaledFloatFieldMapper build(MapperBuilderContext context) {
)
);

@SuppressWarnings("checkstyle:LineLength")
public static final class ScaledFloatFieldType extends SimpleMappedFieldType {

private final double scalingFactor;
Expand Down Expand Up @@ -397,8 +399,7 @@ public Builder builder(BlockFactory factory, int expectedCount) {
}
};
}

ValueFetcher valueFetcher = sourceValueFetcher(blContext.sourcePaths(name()));
var valueFetcher = sourceValueFetcher(blContext.sourcePaths(name()), blContext.indexSettings());
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 @@ -490,7 +491,7 @@ public IndexFieldData.Builder fielddataBuilder(FieldDataContext fieldDataContext
return new SourceValueFetcherSortedDoubleIndexFieldData.Builder(
name(),
valuesSourceType,
sourceValueFetcher(sourcePaths),
sourceValueFetcher(sourcePaths, fieldDataContext.indexSettings()),
searchLookup,
ScaledFloatDocValuesField::new
);
Expand All @@ -504,11 +505,14 @@ 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.getIndexSettings()
);
}

private SourceValueFetcher sourceValueFetcher(Set<String> sourcePaths) {
return new SourceValueFetcher(sourcePaths, nullValue) {
private SourceValueFetcher sourceValueFetcher(Set<String> sourcePaths, IndexSettings indexSettings) {
return new SourceValueFetcher(sourcePaths, nullValue, indexSettings.getIgnoredSourceFormat()) {
@Override
protected Double parseSourceValue(Object value) {
double doubleValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class MatchOnlyTextFieldTypeTests extends FieldTypeTestCase {

Expand Down Expand Up @@ -315,6 +316,7 @@ public void testBlockLoaderDoesNotUseSyntheticSourceDelegateWhenIgnoreAboveIsSet
// when
MappedFieldType.BlockLoaderContext blContext = mock(MappedFieldType.BlockLoaderContext.class);
doReturn(FieldNamesFieldMapper.FieldNamesFieldType.get(false)).when(blContext).fieldNames();
when(blContext.indexSettings()).thenReturn(indexSettings);
BlockLoader blockLoader = ft.blockLoader(blContext);

// then
Expand Down Expand Up @@ -362,6 +364,7 @@ public void testBlockLoaderDoesNotUseSyntheticSourceDelegateWhenIgnoreAboveIsSet

// when
MappedFieldType.BlockLoaderContext blContext = mock(MappedFieldType.BlockLoaderContext.class);
when(blContext.indexSettings()).thenReturn(indexSettings);
doReturn(FieldNamesFieldMapper.FieldNamesFieldType.get(false)).when(blContext).fieldNames();
BlockLoader blockLoader = ft.blockLoader(blContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ protected void assertFetch(MapperService mapperService, String field, Object val
.build(new IndexFieldDataCache.None(), new NoneCircuitBreakerService())
);
SearchExecutionContext searchExecutionContext = mock(SearchExecutionContext.class);
when(searchExecutionContext.getIndexSettings()).thenReturn(mapperService.getIndexSettings());
when(searchExecutionContext.isSourceEnabled()).thenReturn(true);
when(searchExecutionContext.sourcePath(field)).thenReturn(Set.of(field));
when(searchExecutionContext.getForField(ft, fdt)).thenAnswer(inv -> fieldDataLookup(mapperService).apply(ft, () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1813,6 +1813,14 @@ public SourceFieldMapper.Mode getIndexMappingSourceMode() {
return indexMappingSourceMode;
}

public IgnoredSourceFieldMapper.IgnoredSourceFormat getIgnoredSourceFormat() {
if (getIndexMappingSourceMode() == SourceFieldMapper.Mode.SYNTHETIC) {
return IgnoredSourceFieldMapper.ignoredSourceFormat(getIndexVersionCreated());
} else {
return IgnoredSourceFieldMapper.IgnoredSourceFormat.NO_IGNORED_SOURCE;
}
}

/**
* @return Whether recovery source should be enabled if needed.
* Note that this is a node setting, and this setting is not sourced from index settings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.elasticsearch.common.geo.GeometryFormatterFactory;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.core.CheckedConsumer;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.query.SearchExecutionContext;
import org.elasticsearch.xcontent.DeprecationHandler;
import org.elasticsearch.xcontent.NamedXContentRegistry;
Expand Down Expand Up @@ -172,9 +173,9 @@ protected Object parseSourceValue(Object value) {
};
}

public ValueFetcher valueFetcher(Set<String> sourcePaths, T nullValue, String format) {
public SourceBasedValueFetcher valueFetcher(Set<String> sourcePaths, T nullValue, String format, IndexSettings indexSettings) {
Function<List<T>, List<Object>> formatter = getFormatter(format != null ? format : GeometryFormatterFactory.GEOJSON);
return new ArraySourceValueFetcher(sourcePaths, nullValueAsSource(nullValue)) {
return new ArraySourceValueFetcher(sourcePaths, nullValueAsSource(nullValue), indexSettings.getIgnoredSourceFormat()) {
@Override
protected Object parseSourceValue(Object value) {
final List<T> values = new ArrayList<>();
Expand All @@ -185,7 +186,7 @@ protected Object parseSourceValue(Object value) {
}

protected BlockLoader blockLoaderFromSource(BlockLoaderContext blContext) {
ValueFetcher fetcher = valueFetcher(blContext.sourcePaths(name()), nullValue, GeometryFormatterFactory.WKB);
var fetcher = valueFetcher(blContext.sourcePaths(name()), nullValue, GeometryFormatterFactory.WKB, blContext.indexSettings());
// TODO consider optimization using BlockSourceReader.lookupFromFieldNames(blContext.fieldNames(), name())
return new BlockSourceReader.GeometriesBlockLoader(fetcher, BlockSourceReader.lookupMatchingAll());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package org.elasticsearch.index.mapper;

import org.elasticsearch.core.Nullable;
import org.elasticsearch.index.mapper.IgnoredSourceFieldMapper.IgnoredSourceFormat;
import org.elasticsearch.index.query.SearchExecutionContext;
import org.elasticsearch.search.fetch.StoredFieldsSpec;
import org.elasticsearch.search.lookup.Source;
Expand All @@ -27,9 +28,10 @@
* array values in parsing. Field types should use this class if their corresponding
* mapper returns true for {@link FieldMapper#parsesArrayValue()}.
*/
public abstract class ArraySourceValueFetcher implements ValueFetcher {
public abstract class ArraySourceValueFetcher implements SourceBasedValueFetcher {
private final Set<String> sourcePaths;
private final @Nullable Object nullValue;
private final IgnoredSourceFormat ignoredSourceFormat;

public ArraySourceValueFetcher(String fieldName, SearchExecutionContext context) {
this(fieldName, context, null);
Expand All @@ -43,15 +45,17 @@ 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.getIndexSettings().getIgnoredSourceFormat();
}

/**
* @param sourcePaths The paths to pull source values from
* @param nullValue An optional substitute value if the _source value is `null`
*/
public ArraySourceValueFetcher(Set<String> sourcePaths, Object nullValue) {
public ArraySourceValueFetcher(Set<String> sourcePaths, Object nullValue, IgnoredSourceFormat ignoredSourceFormat) {
this.sourcePaths = sourcePaths;
this.nullValue = nullValue;
this.ignoredSourceFormat = ignoredSourceFormat;
}

@Override
Expand All @@ -76,7 +80,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 @@ -95,7 +95,7 @@ private abstract static class SourceBlockLoader implements BlockLoader {
protected final ValueFetcher fetcher;
private final LeafIteratorLookup lookup;

private SourceBlockLoader(ValueFetcher fetcher, LeafIteratorLookup lookup) {
private SourceBlockLoader(SourceBasedValueFetcher fetcher, LeafIteratorLookup lookup) {
this.fetcher = fetcher;
this.lookup = lookup;
}
Expand All @@ -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 Expand Up @@ -143,7 +143,7 @@ public final String toString() {
* Load {@code boolean}s from {@code _source}.
*/
public static class BooleansBlockLoader extends SourceBlockLoader {
public BooleansBlockLoader(ValueFetcher fetcher, LeafIteratorLookup lookup) {
public BooleansBlockLoader(SourceBasedValueFetcher fetcher, LeafIteratorLookup lookup) {
super(fetcher, lookup);
}

Expand Down Expand Up @@ -183,7 +183,7 @@ public String toString() {
* Load {@link BytesRef}s from {@code _source}.
*/
public static class BytesRefsBlockLoader extends SourceBlockLoader {
public BytesRefsBlockLoader(ValueFetcher fetcher, LeafIteratorLookup lookup) {
public BytesRefsBlockLoader(SourceBasedValueFetcher fetcher, LeafIteratorLookup lookup) {
super(fetcher, lookup);
}

Expand All @@ -204,7 +204,7 @@ protected String name() {
}

public static class GeometriesBlockLoader extends SourceBlockLoader {
public GeometriesBlockLoader(ValueFetcher fetcher, LeafIteratorLookup lookup) {
public GeometriesBlockLoader(SourceBasedValueFetcher fetcher, LeafIteratorLookup lookup) {
super(fetcher, lookup);
}

Expand Down Expand Up @@ -267,7 +267,7 @@ public String toString() {
* Load {@code double}s from {@code _source}.
*/
public static class DoublesBlockLoader extends SourceBlockLoader {
public DoublesBlockLoader(ValueFetcher fetcher, LeafIteratorLookup lookup) {
public DoublesBlockLoader(SourceBasedValueFetcher fetcher, LeafIteratorLookup lookup) {
super(fetcher, lookup);
}

Expand Down Expand Up @@ -309,7 +309,7 @@ public String toString() {
public static class DenseVectorBlockLoader extends SourceBlockLoader {
private final int dimensions;

public DenseVectorBlockLoader(ValueFetcher fetcher, LeafIteratorLookup lookup, int dimensions) {
public DenseVectorBlockLoader(SourceBasedValueFetcher fetcher, LeafIteratorLookup lookup, int dimensions) {
super(fetcher, lookup);
this.dimensions = dimensions;
}
Expand Down Expand Up @@ -350,7 +350,7 @@ public String toString() {
* Load {@code int}s from {@code _source}.
*/
public static class IntsBlockLoader extends SourceBlockLoader {
public IntsBlockLoader(ValueFetcher fetcher, LeafIteratorLookup lookup) {
public IntsBlockLoader(SourceBasedValueFetcher fetcher, LeafIteratorLookup lookup) {
super(fetcher, lookup);
}

Expand Down Expand Up @@ -390,7 +390,7 @@ public String toString() {
* Load {@code long}s from {@code _source}.
*/
public static class LongsBlockLoader extends SourceBlockLoader {
public LongsBlockLoader(ValueFetcher fetcher, LeafIteratorLookup lookup) {
public LongsBlockLoader(SourceBasedValueFetcher fetcher, LeafIteratorLookup lookup) {
super(fetcher, lookup);
}

Expand Down Expand Up @@ -430,7 +430,7 @@ public String toString() {
* Load {@code ip}s from {@code _source}.
*/
public static class IpsBlockLoader extends SourceBlockLoader {
public IpsBlockLoader(ValueFetcher fetcher, LeafIteratorLookup lookup) {
public IpsBlockLoader(SourceBasedValueFetcher fetcher, LeafIteratorLookup lookup) {
super(fetcher, lookup);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.core.Booleans;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.IndexVersion;
import org.elasticsearch.index.IndexVersions;
import org.elasticsearch.index.analysis.NamedAnalyzer;
Expand Down Expand Up @@ -281,11 +282,14 @@ 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.getIndexSettings()
);
}

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

ValueFetcher fetcher = sourceValueFetcher(blContext.sourcePaths(name()));
var fetcher = sourceValueFetcher(blContext.sourcePaths(name()), blContext.indexSettings());
BlockSourceReader.LeafIteratorLookup lookup = indexType.hasTerms() || isStored()
? BlockSourceReader.lookupFromFieldNames(blContext.fieldNames(), name())
: BlockSourceReader.lookupMatchingAll();
Expand Down Expand Up @@ -431,7 +435,7 @@ public IndexFieldData.Builder fielddataBuilder(FieldDataContext fieldDataContext
return new SourceValueFetcherSortedBooleanIndexFieldData.Builder(
name(),
CoreValuesSourceType.BOOLEAN,
sourceValueFetcher(sourcePaths),
sourceValueFetcher(sourcePaths, fieldDataContext.indexSettings()),
searchLookup,
BooleanDocValuesField::new
);
Expand Down
Loading