Skip to content

Commit 2349297

Browse files
committed
Reverted the removal of fallback synthetic source in block loader
1 parent 93f8609 commit 2349297

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/MatchOnlyTextFieldMapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ protected BytesRef toBytesRef(Object v) {
585585
public BlockLoader blockLoader(BlockLoaderContext blContext) {
586586
if (isSyntheticSourceEnabled()) {
587587
// if there is no synthetic source delegate, then this match only text field would've created StoredFields for us to use
588-
if (textFieldType.syntheticSourceDelegate().isPresent() == false) {
588+
if (textFieldType.syntheticSourceDelegate().isEmpty()) {
589589
if (storedFieldInBinaryFormat) {
590590
return new BlockStoredFieldsReader.BytesFromBytesRefsBlockLoader(syntheticSourceFallbackFieldName());
591591
} else {
@@ -701,7 +701,7 @@ protected void parseCreateField(DocumentParserContext context) throws IOExceptio
701701
context.addToFieldNames(fieldType().name());
702702

703703
// match_only_text isn't stored, so if synthetic source needs to be supported, we must do something about it
704-
if (fieldType().textFieldType.shouldStoreFieldForSyntheticSource(indexCreatedVersion)) {
704+
if (fieldType().textFieldType.storeFieldForSyntheticSource(indexCreatedVersion)) {
705705
// check if we can use the delegate
706706
if (fieldType().canUseSyntheticSourceDelegateForSyntheticSource(value)) {
707707
return;

server/src/main/java/org/elasticsearch/index/mapper/TextFieldMapper.java

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
7777
import org.elasticsearch.xcontent.ToXContent;
7878
import org.elasticsearch.xcontent.XContentBuilder;
79+
import org.elasticsearch.xcontent.XContentParser;
7980

8081
import java.io.IOException;
8182
import java.util.ArrayList;
@@ -1063,7 +1064,7 @@ public boolean canUseSyntheticSourceDelegateForQueryingEquality(String str) {
10631064
return syntheticSourceDelegate.get().ignoreAbove().isIgnored(str) == false;
10641065
}
10651066

1066-
public boolean shouldStoreFieldForSyntheticSource(final IndexVersion indexCreatedVersion) {
1067+
public boolean storeFieldForSyntheticSource(final IndexVersion indexCreatedVersion) {
10671068
if (multiFieldsNotStoredByDefaultIndexVersionCheck(indexCreatedVersion)) {
10681069
// if we're within a multi field, then supporting synthetic source isn't necessary as that's the responsibility of the
10691070
// parent
@@ -1106,10 +1107,56 @@ protected String delegatingTo() {
11061107
return new BlockStoredFieldsReader.BytesFromStringsBlockLoader(name());
11071108
}
11081109

1110+
// if there is no field to delegate to and this field isn't stored, then fallback to ignored source
1111+
if (isSyntheticSourceEnabled() && syntheticSourceDelegate.isEmpty() && parentField == null) {
1112+
return fallbackSyntheticSourceBlockLoader(blContext);
1113+
}
1114+
1115+
// otherwise, load values from _source (synthetic or not)
11091116
SourceValueFetcher fetcher = SourceValueFetcher.toString(blContext.sourcePaths(name()));
11101117
return new BlockSourceReader.BytesRefsBlockLoader(fetcher, blockReaderDisiLookup(blContext));
11111118
}
11121119

1120+
FallbackSyntheticSourceBlockLoader fallbackSyntheticSourceBlockLoader(BlockLoaderContext blContext) {
1121+
var reader = new FallbackSyntheticSourceBlockLoader.SingleValueReader<BytesRef>(null) {
1122+
@Override
1123+
public void convertValue(Object value, List<BytesRef> accumulator) {
1124+
if (value != null) {
1125+
accumulator.add(new BytesRef(value.toString()));
1126+
}
1127+
}
1128+
1129+
@Override
1130+
protected void parseNonNullValue(XContentParser parser, List<BytesRef> accumulator) throws IOException {
1131+
var text = parser.textOrNull();
1132+
1133+
if (text != null) {
1134+
accumulator.add(new BytesRef(text));
1135+
}
1136+
}
1137+
1138+
@Override
1139+
public void writeToBlock(List<BytesRef> values, BlockLoader.Builder blockBuilder) {
1140+
var bytesRefBuilder = (BlockLoader.BytesRefBuilder) blockBuilder;
1141+
1142+
for (var value : values) {
1143+
bytesRefBuilder.appendBytesRef(value);
1144+
}
1145+
}
1146+
};
1147+
1148+
return new FallbackSyntheticSourceBlockLoader(
1149+
reader,
1150+
name(),
1151+
IgnoredSourceFieldMapper.ignoredSourceFormat(blContext.indexSettings().getIndexVersionCreated())
1152+
) {
1153+
@Override
1154+
public Builder builder(BlockFactory factory, int expectedCount) {
1155+
return factory.bytesRefs(expectedCount);
1156+
}
1157+
};
1158+
}
1159+
11131160
/**
11141161
* Build an iterator of documents that have the field. This mirrors parseCreateField,
11151162
* using whatever
@@ -1420,7 +1467,7 @@ protected void parseCreateField(DocumentParserContext context) throws IOExceptio
14201467
}
14211468

14221469
// store the field if isn't stored yet, and we need it to be stored for synthetic source
1423-
if (fieldType.stored() == false && fieldType().shouldStoreFieldForSyntheticSource(indexCreatedVersion)) {
1470+
if (fieldType.stored() == false && fieldType().storeFieldForSyntheticSource(indexCreatedVersion)) {
14241471
// if we can rely on the synthetic source delegate for synthetic source, then exit as there is nothing to do
14251472
if (fieldType().canUseSyntheticSourceDelegateForSyntheticSource(value)) {
14261473
return;

0 commit comments

Comments
 (0)