Skip to content

Commit 3ac64c1

Browse files
jimczijtibshirani
authored andcommitted
Fix total hits serialization of the search response (#36290)
This change removes the custom serialization of the total hits and reuses the shard's serialization of Lucene#read/writeTopDocs in the client code. This also removes the incorrect assertion that trips randomly in bwc tests. Closes #36284
1 parent 3652f90 commit 3ac64c1

File tree

2 files changed

+5
-17
lines changed

2 files changed

+5
-17
lines changed

server/src/main/java/org/elasticsearch/common/lucene/Lucene.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ public static boolean exists(IndexSearcher searcher, Query query) throws IOExcep
298298
return false;
299299
}
300300

301-
private static TotalHits readTotalHits(StreamInput in) throws IOException {
301+
public static TotalHits readTotalHits(StreamInput in) throws IOException {
302302
long totalHits = in.readVLong();
303303
TotalHits.Relation totalHitsRelation = TotalHits.Relation.EQUAL_TO;
304304
if (in.getVersion().onOrAfter(org.elasticsearch.Version.V_7_0_0)) {
@@ -418,7 +418,7 @@ public static ScoreDoc readScoreDoc(StreamInput in) throws IOException {
418418

419419
private static final Class<?> GEO_DISTANCE_SORT_TYPE_CLASS = LatLonDocValuesField.newDistanceSort("some_geo_field", 0, 0).getClass();
420420

421-
private static void writeTotalHits(StreamOutput out, TotalHits totalHits) throws IOException {
421+
public static void writeTotalHits(StreamOutput out, TotalHits totalHits) throws IOException {
422422
out.writeVLong(totalHits.value);
423423
if (out.getVersion().onOrAfter(org.elasticsearch.Version.V_7_0_0)) {
424424
out.writeEnum(totalHits.relation);

server/src/main/java/org/elasticsearch/search/SearchHits.java

+3-15
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121

2222
import org.apache.lucene.search.TotalHits;
2323
import org.apache.lucene.search.TotalHits.Relation;
24-
import org.elasticsearch.Version;
2524
import org.elasticsearch.common.Nullable;
2625
import org.elasticsearch.common.io.stream.StreamInput;
2726
import org.elasticsearch.common.io.stream.StreamOutput;
2827
import org.elasticsearch.common.io.stream.Streamable;
2928
import org.elasticsearch.common.io.stream.Writeable;
29+
import org.elasticsearch.common.lucene.Lucene;
3030
import org.elasticsearch.common.xcontent.ToXContentFragment;
3131
import org.elasticsearch.common.xcontent.XContentBuilder;
3232
import org.elasticsearch.common.xcontent.XContentParser;
@@ -276,14 +276,7 @@ private static class Total implements Writeable, ToXContentFragment {
276276
final TotalHits in;
277277

278278
Total(StreamInput in) throws IOException {
279-
final long value = in.readVLong();
280-
final Relation relation;
281-
if (in.getVersion().onOrAfter(Version.V_7_0_0)) {
282-
relation = in.readEnum(Relation.class);
283-
} else {
284-
relation = Relation.EQUAL_TO;
285-
}
286-
this.in = new TotalHits(value, relation);
279+
this.in = Lucene.readTotalHits(in);
287280
}
288281

289282
Total(TotalHits in) {
@@ -306,12 +299,7 @@ public int hashCode() {
306299

307300
@Override
308301
public void writeTo(StreamOutput out) throws IOException {
309-
out.writeVLong(in.value);
310-
if (out.getVersion().onOrAfter(Version.V_7_0_0)) {
311-
out.writeEnum(in.relation);
312-
} else {
313-
assert in.relation == Relation.EQUAL_TO;
314-
}
302+
Lucene.writeTotalHits(out, in);
315303
}
316304

317305
@Override

0 commit comments

Comments
 (0)