|
21 | 21 | import org.apache.lucene.analysis.tokenattributes.TermToBytesRefAttribute; |
22 | 22 | import org.apache.lucene.document.Field; |
23 | 23 | import org.apache.lucene.document.FieldType; |
| 24 | +import org.apache.lucene.document.StoredField; |
24 | 25 | import org.apache.lucene.index.IndexOptions; |
25 | 26 | import org.apache.lucene.index.Term; |
26 | 27 | import org.apache.lucene.queries.intervals.Intervals; |
@@ -1459,7 +1460,8 @@ protected void parseCreateField(DocumentParserContext context) throws IOExceptio |
1459 | 1460 | } |
1460 | 1461 |
|
1461 | 1462 | // otherwise, store this field in Lucene so that synthetic source can load it |
1462 | | - context.storeFieldForSyntheticSource(fullPath(), leafName(), context.encodeFlattenedToken(), context.doc()); |
| 1463 | + final String fieldName = fieldType().syntheticSourceFallbackFieldName(); |
| 1464 | + context.doc().add(new StoredField(fieldName, value)); |
1463 | 1465 | } |
1464 | 1466 | } |
1465 | 1467 |
|
@@ -1637,15 +1639,32 @@ protected void write(XContentBuilder b, Object value) throws IOException { |
1637 | 1639 | }); |
1638 | 1640 | } |
1639 | 1641 |
|
1640 | | - // otherwise, use the delegate |
1641 | | - // note, the delegate itself might not be stored in Lucene due to various reasons (ex. it tripped ignore_above), in such cases, we |
1642 | | - // should've added this field to ignored_source |
| 1642 | + return new SyntheticSourceSupport.Native(() -> syntheticFieldLoader(fullPath(), leafName())); |
| 1643 | + } |
| 1644 | + |
| 1645 | + private SourceLoader.SyntheticFieldLoader syntheticFieldLoader(String fullFieldName, String leafFieldName) { |
| 1646 | + // since we don't know whether the delegate field loader can be used for synthetic source until parsing, we |
| 1647 | + // need to check both this field and the delegate |
| 1648 | + |
| 1649 | + // first field loader, representing this field |
| 1650 | + final String fieldName = fieldType().syntheticSourceFallbackFieldName(); |
| 1651 | + final var thisFieldLayer = new CompositeSyntheticFieldLoader.StoredFieldLayer(fieldName) { |
| 1652 | + @Override |
| 1653 | + protected void writeValue(Object value, XContentBuilder b) throws IOException { |
| 1654 | + b.value(value.toString()); |
| 1655 | + } |
| 1656 | + }; |
| 1657 | + |
| 1658 | + final CompositeSyntheticFieldLoader fieldLoader = new CompositeSyntheticFieldLoader(leafFieldName, fullFieldName, thisFieldLayer); |
| 1659 | + |
| 1660 | + // second loader, representing a delegate field, if one exists |
1643 | 1661 | var kwd = TextFieldMapper.SyntheticSourceHelper.getKeywordFieldMapperForSyntheticSource(this); |
1644 | 1662 | if (kwd != null) { |
1645 | | - return new SyntheticSourceSupport.Native(() -> kwd.syntheticFieldLoader(fullPath(), leafName())); |
| 1663 | + // merge the two field loaders into one |
| 1664 | + return fieldLoader.mergedWith(kwd.syntheticFieldLoader(fullPath(), leafName())); |
1646 | 1665 | } |
1647 | 1666 |
|
1648 | | - return super.syntheticSourceSupport(); |
| 1667 | + return fieldLoader; |
1649 | 1668 | } |
1650 | 1669 |
|
1651 | 1670 | public static class SyntheticSourceHelper { |
|
0 commit comments