2121import org .apache .lucene .analysis .tokenattributes .TermToBytesRefAttribute ;
2222import org .apache .lucene .document .Field ;
2323import org .apache .lucene .document .FieldType ;
24- import org .apache .lucene .document .StoredField ;
2524import org .apache .lucene .index .IndexOptions ;
2625import org .apache .lucene .index .Term ;
2726import 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