3434import org .elasticsearch .script .SearchScript ;
3535import org .elasticsearch .search .builder .SearchSourceBuilder ;
3636import org .elasticsearch .search .builder .SearchSourceBuilder .ScriptField ;
37+ import org .elasticsearch .search .fetch .StoredFieldsContext ;
3738import org .elasticsearch .search .fetch .subphase .DocValueFieldsContext ;
3839import org .elasticsearch .search .fetch .subphase .DocValueFieldsFetchSubPhase ;
3940import org .elasticsearch .search .fetch .subphase .FetchSourceContext ;
@@ -137,7 +138,7 @@ public final class InnerHitBuilder extends ToXContentToBytes implements Writeabl
137138 private boolean version ;
138139 private boolean trackScores ;
139140
140- private List < String > storedFieldNames ;
141+ private StoredFieldsContext storedFieldsContext ;
141142 private QueryBuilder query = DEFAULT_INNER_HIT_QUERY ;
142143 private List <SortBuilder <?>> sorts ;
143144 private List <String > docValueFields ;
@@ -156,14 +157,14 @@ private InnerHitBuilder(InnerHitBuilder other) {
156157 explain = other .explain ;
157158 version = other .version ;
158159 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 );
161162 }
162163 if (other .docValueFields != null ) {
163- docValueFields = new ArrayList <>(other .docValueFields );
164+ docValueFields = new ArrayList <> (other .docValueFields );
164165 }
165166 if (other .scriptFields != null ) {
166- scriptFields = new HashSet <>(other .scriptFields );
167+ scriptFields = new HashSet <> (other .scriptFields );
167168 }
168169 if (other .fetchSourceContext != null ) {
169170 fetchSourceContext = new FetchSourceContext (
@@ -210,7 +211,7 @@ public InnerHitBuilder(StreamInput in) throws IOException {
210211 explain = in .readBoolean ();
211212 version = in .readBoolean ();
212213 trackScores = in .readBoolean ();
213- storedFieldNames = ( List < String >) in .readGenericValue ( );
214+ storedFieldsContext = in .readOptionalWriteable ( StoredFieldsContext :: new );
214215 docValueFields = (List <String >) in .readGenericValue ();
215216 if (in .readBoolean ()) {
216217 int size = in .readVInt ();
@@ -248,14 +249,14 @@ public void writeTo(StreamOutput out) throws IOException {
248249 out .writeBoolean (explain );
249250 out .writeBoolean (version );
250251 out .writeBoolean (trackScores );
251- out .writeGenericValue ( storedFieldNames );
252+ out .writeOptionalWriteable ( storedFieldsContext );
252253 out .writeGenericValue (docValueFields );
253254 boolean hasScriptFields = scriptFields != null ;
254255 out .writeBoolean (hasScriptFields );
255256 if (hasScriptFields ) {
256257 out .writeVInt (scriptFields .size ());
257258 for (ScriptField scriptField : scriptFields ) {
258- scriptField .writeTo (out );;
259+ scriptField .writeTo (out );
259260 }
260261 }
261262 out .writeOptionalStreamable (fetchSourceContext );
@@ -343,39 +344,42 @@ public InnerHitBuilder setTrackScores(boolean trackScores) {
343344 /**
344345 * Gets the stored fields to load and return.
345346 *
346- * @deprecated Use {@link InnerHitBuilder#getStoredFieldNames ()} instead.
347+ * @deprecated Use {@link InnerHitBuilder#getStoredFieldsContext ()} instead.
347348 */
348349 @ Deprecated
349350 public List <String > getFieldNames () {
350- return storedFieldNames ;
351+ return storedFieldsContext == null ? null : storedFieldsContext . fieldNames () ;
351352 }
352353
353354 /**
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.
356357 *
357358 * @deprecated Use {@link InnerHitBuilder#setStoredFieldNames(List)} instead.
358359 */
359360 @ Deprecated
360361 public InnerHitBuilder setFieldNames (List <String > fieldNames ) {
361- this .storedFieldNames = fieldNames ;
362- return this ;
362+ return setStoredFieldNames (fieldNames );
363363 }
364364
365365
366366 /**
367- * Gets the stored fields to load and return .
367+ * Gets the stored fields context .
368368 */
369- public List < String > getStoredFieldNames () {
370- return storedFieldNames ;
369+ public StoredFieldsContext getStoredFieldsContext () {
370+ return storedFieldsContext ;
371371 }
372372
373373 /**
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.
376376 */
377377 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+ }
379383 return this ;
380384 }
381385
@@ -564,14 +568,8 @@ private void setupInnerHitsContext(QueryShardContext context, InnerHitsContext.B
564568 innerHitsContext .explain (explain );
565569 innerHitsContext .version (version );
566570 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 );
575573 }
576574 if (docValueFields != null ) {
577575 DocValueFieldsContext docValueFieldsContext = innerHitsContext
@@ -633,16 +631,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
633631 if (fetchSourceContext != null ) {
634632 builder .field (SearchSourceBuilder ._SOURCE_FIELD .getPreferredName (), fetchSourceContext , params );
635633 }
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 );
646636 }
647637 if (docValueFields != null ) {
648638 builder .startArray (SearchSourceBuilder .DOCVALUE_FIELDS_FIELD .getPreferredName ());
@@ -693,7 +683,7 @@ public boolean equals(Object o) {
693683 Objects .equals (explain , that .explain ) &&
694684 Objects .equals (version , that .version ) &&
695685 Objects .equals (trackScores , that .trackScores ) &&
696- Objects .equals (storedFieldNames , that .storedFieldNames ) &&
686+ Objects .equals (storedFieldsContext , that .storedFieldsContext ) &&
697687 Objects .equals (docValueFields , that .docValueFields ) &&
698688 Objects .equals (scriptFields , that .scriptFields ) &&
699689 Objects .equals (fetchSourceContext , that .fetchSourceContext ) &&
@@ -705,7 +695,7 @@ public boolean equals(Object o) {
705695
706696 @ Override
707697 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 ,
709699 docValueFields , scriptFields , fetchSourceContext , sorts , highlightBuilder , query , childInnerHits );
710700 }
711701
0 commit comments