2222import org .elasticsearch .xcontent .XContentParser ;
2323import org .elasticsearch .xpack .searchbusinessrules .PinnedQueryBuilder ;
2424import org .elasticsearch .xpack .searchbusinessrules .SpecifiedDocument ;
25+ import org .elasticsearch .common .io .stream .StreamInput ;
26+ import org .elasticsearch .common .io .stream .StreamOutput ;
27+ import org .elasticsearch .TransportVersions ;
2528
2629import java .io .IOException ;
2730import java .util .ArrayList ;
@@ -76,13 +79,15 @@ public static PinnedRetrieverBuilder fromXContent(XContentParser parser, Retriev
7679 }
7780 }
7881
79- private final List <String > ids ;
80- private final List <SpecifiedDocument > docs ;
82+ final List <String > ids ;
83+ final List <SpecifiedDocument > docs ;
84+ String type ;
8185
8286 public PinnedRetrieverBuilder (List <String > ids , List <SpecifiedDocument > docs , RetrieverBuilder retrieverBuilder , int rankWindowSize ) {
8387 super (new ArrayList <>(), rankWindowSize );
8488 this .ids = ids != null ? ids : new ArrayList <>();
8589 this .docs = docs != null ? docs : new ArrayList <>();
90+ this .type = NAME ;
8691 addChild (new PinnedRetrieverBuilderWrapper (retrieverBuilder ));
8792 }
8893
@@ -97,10 +102,24 @@ public PinnedRetrieverBuilder(
97102 super (retrieverSource , rankWindowSize );
98103 this .ids = ids ;
99104 this .docs = docs ;
105+ this .type = NAME ;
100106 this .retrieverName = retrieverName ;
101107 this .preFilterQueryBuilders = preFilterQueryBuilders ;
102108 }
103109
110+ public PinnedRetrieverBuilder (StreamInput in ) throws IOException {
111+ super (new ArrayList <>(), in .readInt ());
112+ this .type = in .readString ();
113+ if (in .getTransportVersion ().onOrAfter (TransportVersions .PINNED_RETRIEVER )) {
114+ this .ids = in .readOptionalStringCollectionAsList ();
115+ this .docs = in .readBoolean () ? in .readCollectionAsList (SpecifiedDocument ::new ) : new ArrayList <>();
116+ } else {
117+ // For older versions, initialize with empty lists
118+ this .ids = new ArrayList <>();
119+ this .docs = new ArrayList <>();
120+ }
121+ }
122+
104123 @ Override
105124 public String getName () {
106125 return NAME ;
@@ -135,18 +154,19 @@ protected SearchSourceBuilder finalizeSourceBuilder(SearchSourceBuilder source)
135154
136155 @ Override
137156 public void doToXContent (XContentBuilder builder , Params params ) throws IOException {
138- if (ids != null && ids .isEmpty () == false ) {
139- builder .array (IDS_FIELD .getPreferredName (), ids .toArray ());
157+ builder .field ("type" , type );
158+ builder .field ("rank_window_size" , rankWindowSize ());
159+ if (ids .isEmpty () == false ) {
160+ builder .field ("ids" , ids );
140161 }
141- if (docs != null && docs .isEmpty () == false ) {
142- builder .startArray (DOCS_FIELD . getPreferredName () );
162+ if (docs .isEmpty () == false ) {
163+ builder .startArray ("docs" );
143164 for (SpecifiedDocument doc : docs ) {
144- builder . value ( doc );
165+ doc . toXContent ( builder , params );
145166 }
146167 builder .endArray ();
147168 }
148169 builder .field (RETRIEVER_FIELD .getPreferredName (), innerRetrievers .getFirst ().retriever ());
149- builder .field (RANK_WINDOW_SIZE_FIELD .getPreferredName (), rankWindowSize );
150170 }
151171
152172 @ Override
@@ -170,12 +190,12 @@ protected RankDoc[] combineInnerRetrieverResults(List<ScoreDoc[]> rankResults, b
170190 @ Override
171191 public boolean doEquals (Object o ) {
172192 PinnedRetrieverBuilder that = (PinnedRetrieverBuilder ) o ;
173- return super .doEquals (o ) && Objects .equals (ids , that .ids ) && Objects .equals (docs , that .docs );
193+ return super .doEquals (o ) && Objects .equals (ids , that .ids ) && Objects .equals (docs , that .docs ) && Objects . equals ( type , that . type ) ;
174194 }
175195
176196 @ Override
177197 public int doHashCode () {
178- return Objects .hash (super .doHashCode (), ids , docs );
198+ return Objects .hash (super .doHashCode (), ids , docs , type );
179199 }
180200
181201 /**
@@ -202,4 +222,26 @@ public QueryBuilder explainQuery() {
202222 return createPinnedQuery (in .explainQuery ());
203223 }
204224 }
225+
226+ public void writeTo (StreamOutput out ) throws IOException {
227+ out .writeInt (rankWindowSize ());
228+ out .writeString (type );
229+ if (out .getTransportVersion ().onOrAfter (TransportVersions .PINNED_RETRIEVER )) {
230+ out .writeOptionalStringCollection (ids );
231+ if (docs .isEmpty ()) {
232+ out .writeBoolean (false );
233+ } else {
234+ out .writeBoolean (true );
235+ out .writeVInt (docs .size ());
236+ for (SpecifiedDocument doc : docs ) {
237+ doc .writeTo (out );
238+ }
239+ }
240+ } else {
241+ // For older versions, write empty collections
242+ out .writeOptionalStringCollection (new ArrayList <>());
243+ out .writeBoolean (false );
244+ }
245+ }
246+
205247}
0 commit comments