Skip to content

Commit e868dd6

Browse files
committed
introduced versioning and build is successful
1 parent 8162a65 commit e868dd6

File tree

3 files changed

+91
-11
lines changed

3 files changed

+91
-11
lines changed

server/src/main/java/org/elasticsearch/TransportVersions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ static TransportVersion def(int id) {
214214
public static final TransportVersion ESQL_REMOVE_AGGREGATE_TYPE = def(9_045_0_00);
215215
public static final TransportVersion ADD_PROJECT_ID_TO_DSL_ERROR_INFO = def(9_046_0_00);
216216
public static final TransportVersion SEMANTIC_TEXT_CHUNKING_CONFIG = def(9_047_00_0);
217-
217+
public static final TransportVersion PINNED_RETRIEVER = def(9_048_0_00);
218218
/*
219219
* STOP! READ THIS FIRST! No, really,
220220
* ____ _____ ___ ____ _ ____ _____ _ ____ _____ _ _ ___ ____ _____ ___ ____ ____ _____ _

x-pack/plugin/search-business-rules/src/main/java/org/elasticsearch/xpack/searchbusinessrules/retriever/PinnedRetrieverBuilder.java

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import org.elasticsearch.xcontent.XContentParser;
2323
import org.elasticsearch.xpack.searchbusinessrules.PinnedQueryBuilder;
2424
import 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

2629
import java.io.IOException;
2730
import 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
}

x-pack/plugin/search-business-rules/src/test/java/org/elasticsearch/xpack/searchbusinessrules/retriever/PinnedRetrieverBuilderTests.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import org.elasticsearch.xcontent.XContentParser;
2424
import org.elasticsearch.xcontent.json.JsonXContent;
2525
import org.elasticsearch.xpack.searchbusinessrules.SpecifiedDocument;
26+
import org.elasticsearch.common.io.stream.BytesStreamOutput;
27+
import org.elasticsearch.common.io.stream.StreamInput;
28+
import org.elasticsearch.TransportVersions;
2629

2730
import java.io.IOException;
2831
import java.util.ArrayList;
@@ -31,6 +34,8 @@
3134
import static org.elasticsearch.search.rank.RankBuilder.DEFAULT_RANK_WINDOW_SIZE;
3235
import static org.hamcrest.Matchers.equalTo;
3336
import static org.hamcrest.Matchers.instanceOf;
37+
import static org.junit.Assert.assertEquals;
38+
import static org.junit.Assert.assertTrue;
3439

3540
public class PinnedRetrieverBuilderTests extends AbstractXContentTestCase<PinnedRetrieverBuilder> {
3641

@@ -158,4 +163,37 @@ public void testPinnedRetrieverParsing() throws IOException {
158163
}
159164
}
160165
}
166+
167+
public void testBwcSerialization() throws IOException {
168+
PinnedRetrieverBuilder builder = createRandomPinnedRetrieverBuilder();
169+
170+
try (BytesStreamOutput output = new BytesStreamOutput()) {
171+
builder.writeTo(output);
172+
173+
try (StreamInput input = output.bytes().streamInput()) {
174+
PinnedRetrieverBuilder deserialized = new PinnedRetrieverBuilder(input);
175+
// The ids and docs fields should be empty in older versions
176+
assertTrue(deserialized.ids.isEmpty());
177+
assertTrue(deserialized.docs.isEmpty());
178+
assertEquals(builder.rankWindowSize(), deserialized.rankWindowSize());
179+
assertEquals(builder.type, deserialized.type);
180+
}
181+
}
182+
183+
// Test serialization with current version
184+
try (BytesStreamOutput output = new BytesStreamOutput()) {
185+
output.setTransportVersion(TransportVersions.PINNED_RETRIEVER);
186+
builder.writeTo(output);
187+
188+
try (StreamInput input = output.bytes().streamInput()) {
189+
input.setTransportVersion(TransportVersions.PINNED_RETRIEVER);
190+
PinnedRetrieverBuilder deserialized = new PinnedRetrieverBuilder(input);
191+
assertEquals(builder, deserialized);
192+
assertEquals(builder.ids, deserialized.ids);
193+
assertEquals(builder.docs, deserialized.docs);
194+
assertEquals(builder.rankWindowSize(), deserialized.rankWindowSize());
195+
assertEquals(builder.type, deserialized.type);
196+
}
197+
}
198+
}
161199
}

0 commit comments

Comments
 (0)