Skip to content

Commit da7cf74

Browse files
committed
Use ignored source for text fields
1 parent 92cc55c commit da7cf74

File tree

2 files changed

+18
-36
lines changed

2 files changed

+18
-36
lines changed

plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldMapper.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.apache.lucene.document.FieldType;
2323
import org.apache.lucene.document.StoredField;
2424
import org.apache.lucene.index.IndexOptions;
25-
import org.apache.lucene.util.BytesRef;
2625
import org.elasticsearch.ElasticsearchParseException;
2726
import org.elasticsearch.index.IndexVersion;
2827
import org.elasticsearch.index.analysis.AnalyzerScope;
@@ -552,14 +551,14 @@ public Map<String, NamedAnalyzer> indexAnalyzers() {
552551

553552
@Override
554553
protected void parseCreateField(DocumentParserContext context) throws IOException {
555-
final var value = context.parser().optimizedTextOrNull();
554+
final String value = context.parser().textOrNull();
556555

557556
if (value == null) {
558557
return;
559558
}
560559

561560
if (fieldType.indexOptions() != IndexOptions.NONE || fieldType.stored()) {
562-
Field field = new Field(mappedFieldType.name(), value.string(), fieldType);
561+
Field field = new Field(mappedFieldType.name(), value, fieldType);
563562
context.doc().add(field);
564563
if (fieldType.omitNorms()) {
565564
context.addToFieldNames(fieldType().name());
@@ -569,15 +568,13 @@ protected void parseCreateField(DocumentParserContext context) throws IOExceptio
569568
// else to support synthetic source
570569

571570
// if we can rely on the synthetic source delegate for synthetic source, then return
572-
if (fieldType().canUseSyntheticSourceDelegateForSyntheticSource(value.string())) {
571+
if (fieldType().canUseSyntheticSourceDelegateForSyntheticSource(value)) {
573572
return;
574573
}
575574

576575
// otherwise, we need to store a copy of this value so that synthetic source can load it
577-
var utfBytes = value.bytes();
578-
var bytesRef = new BytesRef(utfBytes.bytes(), utfBytes.offset(), utfBytes.length());
579576
final String fieldName = fieldType().syntheticSourceFallbackFieldName();
580-
context.doc().add(new StoredField(fieldName, bytesRef));
577+
context.doc().add(new StoredField(fieldName, value));
581578
}
582579
}
583580

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

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.apache.lucene.analysis.tokenattributes.TermToBytesRefAttribute;
2222
import org.apache.lucene.document.Field;
2323
import org.apache.lucene.document.FieldType;
24-
import org.apache.lucene.document.StoredField;
2524
import org.apache.lucene.index.IndexOptions;
2625
import org.apache.lucene.index.Term;
2726
import org.apache.lucene.queries.intervals.Intervals;
@@ -1459,8 +1458,14 @@ protected void parseCreateField(DocumentParserContext context) throws IOExceptio
14591458
return;
14601459
}
14611460

1462-
final String fieldName = fieldType().syntheticSourceFallbackFieldName();
1463-
context.doc().add(new StoredField(fieldName, value));
1461+
// record this field's value in _ignored_source - synthetic source will fallback to it automatically
1462+
var utfBytes = context.parser().optimizedTextOrNull().bytes();
1463+
var valuesBytesRef = new BytesRef(utfBytes.bytes(), utfBytes.offset(), utfBytes.length());
1464+
context.addIgnoredField(
1465+
new IgnoredSourceFieldMapper.NameValue(fullPath(), fullPath().lastIndexOf(leafName()), valuesBytesRef, context.doc())
1466+
);
1467+
// final String fieldName = fieldType().syntheticSourceFallbackFieldName();
1468+
// context.doc().add(new StoredField(fieldName, value));
14641469
}
14651470
}
14661471

@@ -1628,6 +1633,7 @@ private boolean isIndexed() {
16281633

16291634
@Override
16301635
protected SyntheticSourceSupport syntheticSourceSupport() {
1636+
// if we stored this field in Lucene, then use that for synthetic source
16311637
if (store) {
16321638
return new SyntheticSourceSupport.Native(() -> new StringStoredFieldFieldLoader(fullPath(), leafName()) {
16331639
@Override
@@ -1637,36 +1643,15 @@ protected void write(XContentBuilder b, Object value) throws IOException {
16371643
});
16381644
}
16391645

1640-
if (isIndexed()) {
1641-
return super.syntheticSourceSupport();
1642-
}
1643-
1644-
return new SyntheticSourceSupport.Native(() -> syntheticFieldLoader(fullPath(), leafName()));
1645-
}
1646-
1647-
private SourceLoader.SyntheticFieldLoader syntheticFieldLoader(String fullFieldName, String leafFieldName) {
1648-
// since we don't know whether the delegate field loader can be used for synthetic source until parsing, we
1649-
// need to check both this field and the delegate
1650-
1651-
// first field loader, representing this field
1652-
final String fieldName = fieldType().syntheticSourceFallbackFieldName();
1653-
final var thisFieldLayer = new CompositeSyntheticFieldLoader.StoredFieldLayer(fieldName) {
1654-
@Override
1655-
protected void writeValue(Object value, XContentBuilder b) throws IOException {
1656-
b.value(value.toString());
1657-
}
1658-
};
1659-
1660-
final CompositeSyntheticFieldLoader fieldLoader = new CompositeSyntheticFieldLoader(leafFieldName, fullFieldName, thisFieldLayer);
1661-
1662-
// second loader, representing a delegate field, if one exists
1646+
// otherwise, use the delegate
1647+
// note, the delegate itself might not be stored in Lucene due to various reasons (ex. it tripped ignore_above), in such cases, we
1648+
// should've added this field to ignored_source
16631649
var kwd = TextFieldMapper.SyntheticSourceHelper.getKeywordFieldMapperForSyntheticSource(this);
16641650
if (kwd != null) {
1665-
// merge the two field loaders into one
1666-
return fieldLoader.mergedWith(kwd.syntheticFieldLoader(fullPath(), leafName()));
1651+
return new SyntheticSourceSupport.Native(() -> kwd.syntheticFieldLoader(fullPath(), leafName()));
16671652
}
16681653

1669-
return fieldLoader;
1654+
return super.syntheticSourceSupport();
16701655
}
16711656

16721657
public static class SyntheticSourceHelper {

0 commit comments

Comments
 (0)