Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -37,6 +37,7 @@
<suppress files="qa[/\\]rolling-upgrade[/\\]src[/\\]javaRestTest[/\\]java[/\\]org[/\\]elasticsearch[/\\]upgrades[/\\]TsdbIT.java" checks="LineLength" />
<suppress files="qa[/\\]rolling-upgrade[/\\]src[/\\]javaRestTest[/\\]java[/\\]org[/\\]elasticsearch[/\\]upgrades[/\\]TsdbIndexingRollingUpgradeIT.java" checks="LineLength" />
<suppress files="qa[/\\]rolling-upgrade[/\\]src[/\\]javaRestTest[/\\]java[/\\]org[/\\]elasticsearch[/\\]upgrades[/\\]LogsdbIndexingRollingUpgradeIT.java" checks="LineLength" />
<suppress files="plugin[/\\]logsdb[/\\]qa[/\\]rolling-upgrade[/\\]src[/\\]javaRestTest[/\\]java[/\\]org[/\\]elasticsearch[/\\]upgrades[/\\]MatchOnlyTextRollingUpgradeIT.java" checks="LineLength" />

<!-- Gradle requires inputs to be seriablizable -->
<suppress files="build-tools-internal[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]gradle[/\\]internal[/\\]precommit[/\\]TestingConventionRule.java" checks="RegexpSinglelineJava" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,37 @@ public Query phrasePrefixQuery(TokenStream stream, int slop, int maxExpansions,
return toQuery(query, queryShardContext);
}

private static class BytesFromMixedStringsBytesRefBlockLoader extends BlockStoredFieldsReader.StoredFieldsBlockLoader {
BytesFromMixedStringsBytesRefBlockLoader(String field) {
super(field);
}

@Override
public Builder builder(BlockFactory factory, int expectedCount) {
return factory.bytesRefs(expectedCount);
}

@Override
public RowStrideReader rowStrideReader(LeafReaderContext context) throws IOException {
return new BlockStoredFieldsReader.Bytes(field) {
private final BytesRef scratch = new BytesRef();

@Override
protected BytesRef toBytesRef(Object v) {
if (v instanceof BytesRef) {
return (BytesRef) v;
} else {
return BlockSourceReader.toBytesRef(scratch, (String) v);
}
}
};
}
}

@Override
public BlockLoader blockLoader(BlockLoaderContext blContext) {
if (textFieldType.isSyntheticSource()) {
return new BlockStoredFieldsReader.BytesFromBytesRefsBlockLoader(storedFieldNameForSyntheticSource());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before I forget. In a follow up, let's add index version for this? So that we can only use BytesFromMixedStringsBytesRefBlockLoader when we need to and otherwise use BlockStoredFieldsReader.BytesFromBytesRefsBlockLoader.

return new BytesFromMixedStringsBytesRefBlockLoader(storedFieldNameForSyntheticSource());
}
SourceValueFetcher fetcher = SourceValueFetcher.toString(blContext.sourcePaths(name()));
// MatchOnlyText never has norms, so we have to use the field names field
Expand All @@ -386,7 +413,12 @@ public IndexFieldData.Builder fielddataBuilder(FieldDataContext fieldDataContext
) {
@Override
protected BytesRef storedToBytesRef(Object stored) {
return (BytesRef) stored;
if (stored instanceof BytesRef storedBytes) {
return storedBytes;
} else {
assert stored instanceof String;
return new BytesRef(stored.toString());
}
}
};
}
Expand Down Expand Up @@ -477,7 +509,12 @@ protected SyntheticSourceSupport syntheticSourceSupport() {
() -> new StringStoredFieldFieldLoader(fieldType().storedFieldNameForSyntheticSource(), fieldType().name(), leafName()) {
@Override
protected void write(XContentBuilder b, Object value) throws IOException {
b.value(((BytesRef) value).utf8ToString());
if (value instanceof BytesRef valueBytes) {
b.value(valueBytes.utf8ToString());
} else {
assert value instanceof String;
b.value(value.toString());
}
}
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
package org.elasticsearch.index.mapper.extras;

import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.DocValuesType;
import org.apache.lucene.index.IndexOptions;
Expand All @@ -21,6 +24,7 @@
import org.apache.lucene.tests.analysis.CannedTokenStream;
import org.apache.lucene.tests.analysis.Token;
import org.apache.lucene.tests.index.RandomIndexWriter;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.Strings;
import org.elasticsearch.core.Tuple;
import org.elasticsearch.index.IndexSettings;
Expand Down Expand Up @@ -350,4 +354,29 @@ public void testStoreParameterDefaultsSyntheticSourceTextFieldIsMultiField() thr
assertThat(fields, empty());
}
}

public void testLoadSyntheticSourceFromStringOrBytesRef() throws IOException {
DocumentMapper mapper = createSytheticSourceMapperService(mapping(b -> {
b.startObject("field1").field("type", "match_only_text").endObject();
b.startObject("field2").field("type", "match_only_text").endObject();
})).documentMapper();
try (Directory directory = newDirectory()) {
RandomIndexWriter iw = indexWriterForSyntheticSource(directory);

LuceneDocument document = new LuceneDocument();
document.add(new StringField("field1", "foo", Field.Store.NO));
document.add(new StoredField("field1._original", "foo"));

document.add(new StringField("field2", "bar", Field.Store.NO));
document.add(new StoredField("field2._original", new BytesRef("bar")));

iw.addDocument(document);
iw.close();

try (DirectoryReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) {
String syntheticSource = syntheticSource(mapper, null, indexReader, 0);
assertEquals("{\"field1\":\"foo\",\"field2\":\"bar\"}", syntheticSource);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ public String toString() {
/**
* Convert a {@link String} into a utf-8 {@link BytesRef}.
*/
static BytesRef toBytesRef(BytesRef scratch, String v) {
public static BytesRef toBytesRef(BytesRef scratch, String v) {
int len = UnicodeUtil.maxUTF8Length(v.length());
if (scratch.bytes.length < len) {
scratch.bytes = new byte[len];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public boolean canReuse(int startingDocID) {
return true;
}

private abstract static class StoredFieldsBlockLoader implements BlockLoader {
public abstract static class StoredFieldsBlockLoader implements BlockLoader {
protected final String field;

StoredFieldsBlockLoader(String field) {
public StoredFieldsBlockLoader(String field) {
this.field = field;
}

Expand Down Expand Up @@ -112,10 +112,10 @@ protected BytesRef toBytesRef(Object v) {
}
}

private abstract static class Bytes extends BlockStoredFieldsReader {
public abstract static class Bytes extends BlockStoredFieldsReader {
private final String field;

Bytes(String field) {
public Bytes(String field) {
this.field = field;
}

Expand Down
Loading