1313import org .apache .lucene .index .LeafReader ;
1414import org .apache .lucene .index .LeafReaderContext ;
1515import org .apache .lucene .index .StoredFieldVisitor ;
16- import org .apache .lucene .util .BytesRef ;
1716import org .elasticsearch .common .CheckedBiConsumer ;
1817import org .elasticsearch .common .bytes .BytesReference ;
1918import org .elasticsearch .common .lucene .index .SequentialStoredFieldsLeafReader ;
19+ import org .elasticsearch .index .mapper .FallbackSyntheticSourceBlockLoader ;
2020import org .elasticsearch .index .mapper .IgnoredSourceFieldMapper ;
2121import org .elasticsearch .search .fetch .StoredFieldsSpec ;
2222
2323import java .io .IOException ;
2424import java .util .ArrayList ;
25+ import java .util .HashSet ;
2526import java .util .List ;
2627import java .util .Map ;
28+ import java .util .Set ;
2729
2830class IgnoredSourceFieldLoader extends StoredFieldLoader {
2931
32+ final Set <String > potentialFieldsInIgnoreSource ;
33+
34+ IgnoredSourceFieldLoader (StoredFieldsSpec spec ) {
35+ Set <String > potentialFieldsInIgnoreSource = new HashSet <>();
36+ for (String requiredStoredField : spec .requiredStoredFields ()) {
37+ if (requiredStoredField .startsWith (IgnoredSourceFieldMapper .NAME )) {
38+ String fieldName = requiredStoredField .substring (IgnoredSourceFieldMapper .NAME .length ());
39+ potentialFieldsInIgnoreSource .addAll (FallbackSyntheticSourceBlockLoader .splitIntoFieldPaths (fieldName ));
40+ }
41+ }
42+ this .potentialFieldsInIgnoreSource = potentialFieldsInIgnoreSource ;
43+ }
44+
3045 @ Override
3146 public LeafStoredFieldLoader getLoader (LeafReaderContext ctx , int [] docs ) throws IOException {
3247 var reader = sequentialReader (ctx );
33- var visitor = new SFV ();
48+ var visitor = new SFV (potentialFieldsInIgnoreSource );
3449 return new LeafStoredFieldLoader () {
3550
3651 private int doc = -1 ;
@@ -73,29 +88,37 @@ public List<String> fieldsToLoad() {
7388
7489 static class SFV extends StoredFieldVisitor {
7590
76- boolean processing ;
91+ boolean done ;
7792 final List <Object > values = new ArrayList <>();
93+ final Set <String > potentialFieldsInIgnoreSource ;
94+
95+ SFV (Set <String > potentialFieldsInIgnoreSource ) {
96+ this .potentialFieldsInIgnoreSource = potentialFieldsInIgnoreSource ;
97+ }
7898
7999 @ Override
80100 public Status needsField (FieldInfo fieldInfo ) throws IOException {
81- if (IgnoredSourceFieldMapper .NAME .equals (fieldInfo .name )) {
82- processing = true ;
83- return Status .YES ;
84- } else if (processing ) {
101+ if (done ) {
85102 return Status .STOP ;
103+ } else if (IgnoredSourceFieldMapper .NAME .equals (fieldInfo .name )) {
104+ return Status .YES ;
105+ } else {
106+ return Status .NO ;
86107 }
87-
88- return Status .NO ;
89108 }
90109
91110 @ Override
92111 public void binaryField (FieldInfo fieldInfo , byte [] value ) throws IOException {
93- values .add (new BytesRef (value ));
112+ var result = IgnoredSourceFieldMapper .decodeIfMatch (value , potentialFieldsInIgnoreSource );
113+ if (result != null ) {
114+ done = true ;
115+ values .add (result );
116+ }
94117 }
95118
96119 void reset () {
97120 values .clear ();
98- processing = false ;
121+ done = false ;
99122 }
100123
101124 }
@@ -104,7 +127,7 @@ static boolean supports(StoredFieldsSpec spec) {
104127 return spec .requiresSource () == false
105128 && spec .requiresMetadata () == false
106129 && spec .requiredStoredFields ().size () == 1
107- && spec .requiredStoredFields ().contains (IgnoredSourceFieldMapper .NAME );
130+ && spec .requiredStoredFields ().iterator (). next (). startsWith (IgnoredSourceFieldMapper .NAME );
108131 }
109132
110133 // TODO: use provided one
0 commit comments