Skip to content

Commit 584ba61

Browse files
committed
Remove PROTOTYPE from RescorerBuilders
This changes the serialization order for QueryRescorerBuilder's but that is ok because 5.0.0 doesn't need to be wire compatible with anything.
1 parent 5e8656a commit 584ba61

File tree

5 files changed

+49
-50
lines changed

5 files changed

+49
-50
lines changed

core/src/main/java/org/elasticsearch/search/SearchModule.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ public SearchModule(Settings settings, NamedWriteableRegistry namedWriteableRegi
286286

287287
registerBuiltinFunctionScoreParsers();
288288
registerBuiltinQueryParsers();
289+
registerBuiltinRescorers();
289290
}
290291

291292
public void registerHighlighter(String key, Class<? extends Highlighter> clazz) {
@@ -350,7 +351,6 @@ protected void configure() {
350351
configureSuggesters();
351352
configureFetchSubPhase();
352353
configureShapes();
353-
configureRescorers();
354354
configureSorts();
355355
}
356356

@@ -491,8 +491,8 @@ private void configureShapes() {
491491
}
492492
}
493493

494-
private void configureRescorers() {
495-
namedWriteableRegistry.registerPrototype(RescoreBuilder.class, QueryRescorerBuilder.PROTOTYPE);
494+
private void registerBuiltinRescorers() {
495+
namedWriteableRegistry.register(RescoreBuilder.class, QueryRescorerBuilder.NAME, QueryRescorerBuilder::new);
496496
}
497497

498498
private void configureSorts() {

core/src/main/java/org/elasticsearch/search/rescore/QueryRescoreMode.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,7 @@ public String toString() {
8585

8686
public abstract float combine(float primary, float secondary);
8787

88-
static QueryRescoreMode PROTOTYPE = Total;
89-
90-
@Override
91-
public QueryRescoreMode readFrom(StreamInput in) throws IOException {
88+
public static QueryRescoreMode readFromStream(StreamInput in) throws IOException {
9289
int ordinal = in.readVInt();
9390
if (ordinal < 0 || ordinal >= values().length) {
9491
throw new IOException("Unknown ScoreMode ordinal [" + ordinal + "]");

core/src/main/java/org/elasticsearch/search/rescore/QueryRescorerBuilder.java

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.elasticsearch.common.io.stream.StreamOutput;
2626
import org.elasticsearch.common.xcontent.ObjectParser;
2727
import org.elasticsearch.common.xcontent.XContentBuilder;
28-
import org.elasticsearch.index.query.MatchAllQueryBuilder;
2928
import org.elasticsearch.index.query.QueryBuilder;
3029
import org.elasticsearch.index.query.QueryParseContext;
3130
import org.elasticsearch.index.query.QueryShardContext;
@@ -39,8 +38,6 @@ public class QueryRescorerBuilder extends RescoreBuilder<QueryRescorerBuilder> {
3938

4039
public static final String NAME = "query";
4140

42-
public static final QueryRescorerBuilder PROTOTYPE = new QueryRescorerBuilder(new MatchAllQueryBuilder());
43-
4441
public static final float DEFAULT_RESCORE_QUERYWEIGHT = 1.0f;
4542
public static final float DEFAULT_QUERYWEIGHT = 1.0f;
4643
public static final QueryRescoreMode DEFAULT_SCORE_MODE = QueryRescoreMode.Total;
@@ -77,6 +74,25 @@ public QueryRescorerBuilder(QueryBuilder<?> builder) {
7774
this.queryBuilder = builder;
7875
}
7976

77+
/**
78+
* Read from a stream.
79+
*/
80+
public QueryRescorerBuilder(StreamInput in) throws IOException {
81+
super(in);
82+
queryBuilder = in.readQuery();
83+
scoreMode = QueryRescoreMode.readFromStream(in);
84+
rescoreQueryWeight = in.readFloat();
85+
queryWeight = in.readFloat();
86+
}
87+
88+
@Override
89+
public void doWriteTo(StreamOutput out) throws IOException {
90+
out.writeQuery(queryBuilder);
91+
scoreMode.writeTo(out);
92+
out.writeFloat(rescoreQueryWeight);
93+
out.writeFloat(queryWeight);
94+
}
95+
8096
/**
8197
* @return the query used for this rescore query
8298
*/
@@ -140,9 +156,9 @@ public void doXContent(XContentBuilder builder, Params params) throws IOExceptio
140156
builder.endObject();
141157
}
142158

143-
public QueryRescorerBuilder fromXContent(QueryParseContext parseContext) throws IOException {
144-
InnerBuilder innerBuilder = QUERY_RESCORE_PARSER.parse(parseContext.parser(), new InnerBuilder(), parseContext);
145-
return innerBuilder.build();
159+
public static QueryRescorerBuilder fromXContent(QueryParseContext parseContext) throws IOException {
160+
InnerBuilder innerBuilder = QUERY_RESCORE_PARSER.parse(parseContext.parser(), new InnerBuilder(), parseContext);
161+
return innerBuilder.build();
146162
}
147163

148164
@Override
@@ -181,23 +197,6 @@ public final boolean equals(Object obj) {
181197
Objects.equals(queryBuilder, other.queryBuilder);
182198
}
183199

184-
@Override
185-
public QueryRescorerBuilder doReadFrom(StreamInput in) throws IOException {
186-
QueryRescorerBuilder rescorer = new QueryRescorerBuilder(in.readQuery());
187-
rescorer.setScoreMode(QueryRescoreMode.PROTOTYPE.readFrom(in));
188-
rescorer.setRescoreQueryWeight(in.readFloat());
189-
rescorer.setQueryWeight(in.readFloat());
190-
return rescorer;
191-
}
192-
193-
@Override
194-
public void doWriteTo(StreamOutput out) throws IOException {
195-
out.writeQuery(queryBuilder);
196-
scoreMode.writeTo(out);
197-
out.writeFloat(rescoreQueryWeight);
198-
out.writeFloat(queryWeight);
199-
}
200-
201200
@Override
202201
public String getWriteableName() {
203202
return NAME;
@@ -208,7 +207,7 @@ public String getWriteableName() {
208207
* for the constructor of {@link QueryRescorerBuilder}, but {@link ObjectParser} only
209208
* allows filling properties of an already constructed value.
210209
*/
211-
private class InnerBuilder {
210+
private static class InnerBuilder {
212211

213212
private QueryBuilder<?> queryBuilder;
214213
private float rescoreQueryWeight = DEFAULT_RESCORE_QUERYWEIGHT;

core/src/main/java/org/elasticsearch/search/rescore/RescoreBuilder.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,27 @@ public abstract class RescoreBuilder<RB extends RescoreBuilder<RB>> implements T
4646

4747
private static ParseField WINDOW_SIZE_FIELD = new ParseField("window_size");
4848

49+
/**
50+
* Construct an empty RescoreBuilder.
51+
*/
52+
public RescoreBuilder() {
53+
}
54+
55+
/**
56+
* Read from a stream.
57+
*/
58+
protected RescoreBuilder(StreamInput in) throws IOException {
59+
windowSize = in.readOptionalVInt();
60+
}
61+
62+
@Override
63+
public final void writeTo(StreamOutput out) throws IOException {
64+
out.writeOptionalVInt(this.windowSize);
65+
doWriteTo(out);
66+
}
67+
68+
protected abstract void doWriteTo(StreamOutput out) throws IOException;
69+
4970
@SuppressWarnings("unchecked")
5071
public RB windowSize(int windowSize) {
5172
this.windowSize = windowSize;
@@ -74,7 +95,7 @@ public static RescoreBuilder<?> parseFromXContent(QueryParseContext parseContext
7495
} else if (token == XContentParser.Token.START_OBJECT) {
7596
// we only have QueryRescorer at this point
7697
if (QueryRescorerBuilder.NAME.equals(fieldName)) {
77-
rescorer = QueryRescorerBuilder.PROTOTYPE.fromXContent(parseContext);
98+
rescorer = QueryRescorerBuilder.fromXContent(parseContext);
7899
} else {
79100
throw new ParsingException(parser.getTokenLocation(), "rescore doesn't support rescorer with name [" + fieldName + "]");
80101
}
@@ -128,23 +149,6 @@ public boolean equals(Object obj) {
128149
return Objects.equals(windowSize, other.windowSize);
129150
}
130151

131-
@Override
132-
public RB readFrom(StreamInput in) throws IOException {
133-
RB builder = doReadFrom(in);
134-
builder.windowSize = in.readOptionalVInt();
135-
return builder;
136-
}
137-
138-
protected abstract RB doReadFrom(StreamInput in) throws IOException;
139-
140-
@Override
141-
public void writeTo(StreamOutput out) throws IOException {
142-
doWriteTo(out);
143-
out.writeOptionalVInt(this.windowSize);
144-
}
145-
146-
protected abstract void doWriteTo(StreamOutput out) throws IOException;
147-
148152
@Override
149153
public final String toString() {
150154
try {

core/src/test/java/org/elasticsearch/search/rescore/QueryRescoreBuilderTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ public class QueryRescoreBuilderTests extends ESTestCase {
6969
@BeforeClass
7070
public static void init() {
7171
namedWriteableRegistry = new NamedWriteableRegistry();
72-
namedWriteableRegistry.registerPrototype(RescoreBuilder.class, QueryRescorerBuilder.PROTOTYPE);
7372
indicesQueriesRegistry = new SearchModule(Settings.EMPTY, namedWriteableRegistry).buildQueryParserRegistry();
7473
}
7574

0 commit comments

Comments
 (0)