34
34
import org .elasticsearch .script .SearchScript ;
35
35
import org .elasticsearch .search .builder .SearchSourceBuilder ;
36
36
import org .elasticsearch .search .builder .SearchSourceBuilder .ScriptField ;
37
+ import org .elasticsearch .search .fetch .StoredFieldsContext ;
37
38
import org .elasticsearch .search .fetch .subphase .DocValueFieldsContext ;
38
39
import org .elasticsearch .search .fetch .subphase .DocValueFieldsFetchSubPhase ;
39
40
import org .elasticsearch .search .fetch .subphase .FetchSourceContext ;
@@ -137,7 +138,7 @@ public final class InnerHitBuilder extends ToXContentToBytes implements Writeabl
137
138
private boolean version ;
138
139
private boolean trackScores ;
139
140
140
- private List < String > storedFieldNames ;
141
+ private StoredFieldsContext storedFieldsContext ;
141
142
private QueryBuilder query = DEFAULT_INNER_HIT_QUERY ;
142
143
private List <SortBuilder <?>> sorts ;
143
144
private List <String > docValueFields ;
@@ -156,14 +157,14 @@ private InnerHitBuilder(InnerHitBuilder other) {
156
157
explain = other .explain ;
157
158
version = other .version ;
158
159
trackScores = other .trackScores ;
159
- if (other .storedFieldNames != null ) {
160
- storedFieldNames = new ArrayList <> (other .storedFieldNames );
160
+ if (other .storedFieldsContext != null ) {
161
+ storedFieldsContext = new StoredFieldsContext (other .storedFieldsContext );
161
162
}
162
163
if (other .docValueFields != null ) {
163
- docValueFields = new ArrayList <>(other .docValueFields );
164
+ docValueFields = new ArrayList <> (other .docValueFields );
164
165
}
165
166
if (other .scriptFields != null ) {
166
- scriptFields = new HashSet <>(other .scriptFields );
167
+ scriptFields = new HashSet <> (other .scriptFields );
167
168
}
168
169
if (other .fetchSourceContext != null ) {
169
170
fetchSourceContext = new FetchSourceContext (
@@ -210,7 +211,7 @@ public InnerHitBuilder(StreamInput in) throws IOException {
210
211
explain = in .readBoolean ();
211
212
version = in .readBoolean ();
212
213
trackScores = in .readBoolean ();
213
- storedFieldNames = ( List < String >) in .readGenericValue ( );
214
+ storedFieldsContext = in .readOptionalWriteable ( StoredFieldsContext :: new );
214
215
docValueFields = (List <String >) in .readGenericValue ();
215
216
if (in .readBoolean ()) {
216
217
int size = in .readVInt ();
@@ -248,14 +249,14 @@ public void writeTo(StreamOutput out) throws IOException {
248
249
out .writeBoolean (explain );
249
250
out .writeBoolean (version );
250
251
out .writeBoolean (trackScores );
251
- out .writeGenericValue ( storedFieldNames );
252
+ out .writeOptionalWriteable ( storedFieldsContext );
252
253
out .writeGenericValue (docValueFields );
253
254
boolean hasScriptFields = scriptFields != null ;
254
255
out .writeBoolean (hasScriptFields );
255
256
if (hasScriptFields ) {
256
257
out .writeVInt (scriptFields .size ());
257
258
for (ScriptField scriptField : scriptFields ) {
258
- scriptField .writeTo (out );;
259
+ scriptField .writeTo (out );
259
260
}
260
261
}
261
262
out .writeOptionalStreamable (fetchSourceContext );
@@ -343,39 +344,42 @@ public InnerHitBuilder setTrackScores(boolean trackScores) {
343
344
/**
344
345
* Gets the stored fields to load and return.
345
346
*
346
- * @deprecated Use {@link InnerHitBuilder#getStoredFieldNames ()} instead.
347
+ * @deprecated Use {@link InnerHitBuilder#getStoredFieldsContext ()} instead.
347
348
*/
348
349
@ Deprecated
349
350
public List <String > getFieldNames () {
350
- return storedFieldNames ;
351
+ return storedFieldsContext == null ? null : storedFieldsContext . fieldNames () ;
351
352
}
352
353
353
354
/**
354
- * Sets the stored fields to load and return. If none
355
- * are specified, the source of the document will be returned.
355
+ * Sets the stored fields to load and return.
356
+ * If none are specified, the source of the document will be returned.
356
357
*
357
358
* @deprecated Use {@link InnerHitBuilder#setStoredFieldNames(List)} instead.
358
359
*/
359
360
@ Deprecated
360
361
public InnerHitBuilder setFieldNames (List <String > fieldNames ) {
361
- this .storedFieldNames = fieldNames ;
362
- return this ;
362
+ return setStoredFieldNames (fieldNames );
363
363
}
364
364
365
365
366
366
/**
367
- * Gets the stored fields to load and return .
367
+ * Gets the stored fields context .
368
368
*/
369
- public List < String > getStoredFieldNames () {
370
- return storedFieldNames ;
369
+ public StoredFieldsContext getStoredFieldsContext () {
370
+ return storedFieldsContext ;
371
371
}
372
372
373
373
/**
374
- * Sets the stored fields to load and return. If none
375
- * are specified, the source of the document will be returned.
374
+ * Sets the stored fields to load and return.
375
+ * If none are specified, the source of the document will be returned.
376
376
*/
377
377
public InnerHitBuilder setStoredFieldNames (List <String > fieldNames ) {
378
- this .storedFieldNames = fieldNames ;
378
+ if (storedFieldsContext == null ) {
379
+ storedFieldsContext = StoredFieldsContext .fromList (fieldNames );
380
+ } else {
381
+ storedFieldsContext .addFieldNames (fieldNames );
382
+ }
379
383
return this ;
380
384
}
381
385
@@ -564,14 +568,8 @@ private void setupInnerHitsContext(QueryShardContext context, InnerHitsContext.B
564
568
innerHitsContext .explain (explain );
565
569
innerHitsContext .version (version );
566
570
innerHitsContext .trackScores (trackScores );
567
- if (storedFieldNames != null ) {
568
- if (storedFieldNames .isEmpty ()) {
569
- innerHitsContext .emptyFieldNames ();
570
- } else {
571
- for (String fieldName : storedFieldNames ) {
572
- innerHitsContext .fieldNames ().add (fieldName );
573
- }
574
- }
571
+ if (storedFieldsContext != null ) {
572
+ innerHitsContext .storedFieldsContext (storedFieldsContext );
575
573
}
576
574
if (docValueFields != null ) {
577
575
DocValueFieldsContext docValueFieldsContext = innerHitsContext
@@ -633,16 +631,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
633
631
if (fetchSourceContext != null ) {
634
632
builder .field (SearchSourceBuilder ._SOURCE_FIELD .getPreferredName (), fetchSourceContext , params );
635
633
}
636
- if (storedFieldNames != null ) {
637
- if (storedFieldNames .size () == 1 ) {
638
- builder .field (SearchSourceBuilder .STORED_FIELDS_FIELD .getPreferredName (), storedFieldNames .get (0 ));
639
- } else {
640
- builder .startArray (SearchSourceBuilder .STORED_FIELDS_FIELD .getPreferredName ());
641
- for (String fieldName : storedFieldNames ) {
642
- builder .value (fieldName );
643
- }
644
- builder .endArray ();
645
- }
634
+ if (storedFieldsContext != null ) {
635
+ storedFieldsContext .toXContent (SearchSourceBuilder .STORED_FIELDS_FIELD .getPreferredName (), builder );
646
636
}
647
637
if (docValueFields != null ) {
648
638
builder .startArray (SearchSourceBuilder .DOCVALUE_FIELDS_FIELD .getPreferredName ());
@@ -693,7 +683,7 @@ public boolean equals(Object o) {
693
683
Objects .equals (explain , that .explain ) &&
694
684
Objects .equals (version , that .version ) &&
695
685
Objects .equals (trackScores , that .trackScores ) &&
696
- Objects .equals (storedFieldNames , that .storedFieldNames ) &&
686
+ Objects .equals (storedFieldsContext , that .storedFieldsContext ) &&
697
687
Objects .equals (docValueFields , that .docValueFields ) &&
698
688
Objects .equals (scriptFields , that .scriptFields ) &&
699
689
Objects .equals (fetchSourceContext , that .fetchSourceContext ) &&
@@ -705,7 +695,7 @@ public boolean equals(Object o) {
705
695
706
696
@ Override
707
697
public int hashCode () {
708
- return Objects .hash (name , nestedPath , parentChildType , from , size , explain , version , trackScores , storedFieldNames ,
698
+ return Objects .hash (name , nestedPath , parentChildType , from , size , explain , version , trackScores , storedFieldsContext ,
709
699
docValueFields , scriptFields , fetchSourceContext , sorts , highlightBuilder , query , childInnerHits );
710
700
}
711
701
0 commit comments