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 ;
@@ -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