Skip to content

Commit 2b08acd

Browse files
committed
Use ignored source for text fields
1 parent 9e05896 commit 2b08acd

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;
@@ -1434,8 +1433,14 @@ protected void parseCreateField(DocumentParserContext context) throws IOExceptio
14341433
return;
14351434
}
14361435

1437-
final String fieldName = fieldType().syntheticSourceFallbackFieldName();
1438-
context.doc().add(new StoredField(fieldName, value));
1436+
// record this field's value in _ignored_source - synthetic source will fallback to it automatically
1437+
var utfBytes = context.parser().optimizedTextOrNull().bytes();
1438+
var valuesBytesRef = new BytesRef(utfBytes.bytes(), utfBytes.offset(), utfBytes.length());
1439+
context.addIgnoredField(
1440+
new IgnoredSourceFieldMapper.NameValue(fullPath(), fullPath().lastIndexOf(leafName()), valuesBytesRef, context.doc())
1441+
);
1442+
// final String fieldName = fieldType().syntheticSourceFallbackFieldName();
1443+
// context.doc().add(new StoredField(fieldName, value));
14391444
}
14401445
}
14411446

@@ -1603,6 +1608,7 @@ private boolean isIndexed() {
16031608

16041609
@Override
16051610
protected SyntheticSourceSupport syntheticSourceSupport() {
1611+
// if we stored this field in Lucene, then use that for synthetic source
16061612
if (store) {
16071613
return new SyntheticSourceSupport.Native(() -> new StringStoredFieldFieldLoader(fullPath(), leafName()) {
16081614
@Override
@@ -1612,36 +1618,15 @@ protected void write(XContentBuilder b, Object value) throws IOException {
16121618
});
16131619
}
16141620

1615-
if (isIndexed()) {
1616-
return super.syntheticSourceSupport();
1617-
}
1618-
1619-
return new SyntheticSourceSupport.Native(() -> syntheticFieldLoader(fullPath(), leafName()));
1620-
}
1621-
1622-
private SourceLoader.SyntheticFieldLoader syntheticFieldLoader(String fullFieldName, String leafFieldName) {
1623-
// since we don't know whether the delegate field loader can be used for synthetic source until parsing, we
1624-
// need to check both this field and the delegate
1625-
1626-
// first field loader, representing this field
1627-
final String fieldName = fieldType().syntheticSourceFallbackFieldName();
1628-
final var thisFieldLayer = new CompositeSyntheticFieldLoader.StoredFieldLayer(fieldName) {
1629-
@Override
1630-
protected void writeValue(Object value, XContentBuilder b) throws IOException {
1631-
b.value(value.toString());
1632-
}
1633-
};
1634-
1635-
final CompositeSyntheticFieldLoader fieldLoader = new CompositeSyntheticFieldLoader(leafFieldName, fullFieldName, thisFieldLayer);
1636-
1637-
// second loader, representing a delegate field, if one exists
1621+
// otherwise, use the delegate
1622+
// note, the delegate itself might not be stored in Lucene due to various reasons (ex. it tripped ignore_above), in such cases, we
1623+
// should've added this field to ignored_source
16381624
var kwd = TextFieldMapper.SyntheticSourceHelper.getKeywordFieldMapperForSyntheticSource(this);
16391625
if (kwd != null) {
1640-
// merge the two field loaders into one
1641-
return fieldLoader.mergedWith(kwd.syntheticFieldLoader(fullPath(), leafName()));
1626+
return new SyntheticSourceSupport.Native(() -> kwd.syntheticFieldLoader(fullPath(), leafName()));
16421627
}
16431628

1644-
return fieldLoader;
1629+
return super.syntheticSourceSupport();
16451630
}
16461631

16471632
public static class SyntheticSourceHelper {

0 commit comments

Comments
 (0)