Skip to content

Always set total hits to null when track total hits is false #36285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ static final class TopDocsStats {
TopDocsStats(boolean trackTotalHits) {
this.trackTotalHits = trackTotalHits;
this.totalHits = 0;
this.totalHitsRelation = trackTotalHits ? Relation.EQUAL_TO : Relation.GREATER_THAN_OR_EQUAL_TO;
this.totalHitsRelation = Relation.EQUAL_TO;
}

TotalHits getTotalHits() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public static boolean exists(IndexSearcher searcher, Query query) throws IOExcep
return false;
}

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

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

private static void writeTotalHits(StreamOutput out, TotalHits totalHits) throws IOException {
public static void writeTotalHits(StreamOutput out, TotalHits totalHits) throws IOException {
out.writeVLong(totalHits.value);
if (out.getVersion().onOrAfter(org.elasticsearch.Version.V_7_0_0)) {
out.writeEnum(totalHits.relation);
Expand Down
17 changes: 3 additions & 14 deletions server/src/main/java/org/elasticsearch/search/SearchHits.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
Expand Down Expand Up @@ -276,14 +277,7 @@ private static class Total implements Writeable, ToXContentFragment {
final TotalHits in;

Total(StreamInput in) throws IOException {
final long value = in.readVLong();
final Relation relation;
if (in.getVersion().onOrAfter(Version.V_7_0_0)) {
relation = in.readEnum(Relation.class);
} else {
relation = Relation.EQUAL_TO;
}
this.in = new TotalHits(value, relation);
this.in = Lucene.readTotalHits(in);
}

Total(TotalHits in) {
Expand All @@ -306,12 +300,7 @@ public int hashCode() {

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVLong(in.value);
if (out.getVersion().onOrAfter(Version.V_7_0_0)) {
out.writeEnum(in.relation);
} else {
assert in.relation == Relation.EQUAL_TO;
}
Lucene.writeTotalHits(out, in);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,11 @@ private EmptyTopDocsCollectorContext(IndexReader reader, Query query,
this.hitCountSupplier = () -> new TotalHits(hitCountCollector.getTotalHits(), TotalHits.Relation.EQUAL_TO);
} else {
this.collector = new EarlyTerminatingCollector(hitCountCollector, 0, false);
this.hitCountSupplier = () -> new TotalHits(hitCount, TotalHits.Relation.EQUAL_TO);
this.hitCountSupplier = () -> new TotalHits(hitCount, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO);
}
} else {
this.collector = new EarlyTerminatingCollector(new TotalHitCountCollector(), 0, false);
// for bwc hit count is set to 0, it will be converted to -1 by the coordinating node
this.hitCountSupplier = () -> new TotalHits(0, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO);
this.hitCountSupplier = () -> new TotalHits(0, TotalHits.Relation.EQUAL_TO);
}
}

Expand Down Expand Up @@ -222,8 +221,7 @@ private SimpleTopDocsCollectorContext(IndexReader reader,
} else {
topDocsCollector = createCollector(sortAndFormats, numHits, searchAfter, 1); // don't compute hit counts via the collector
topDocsSupplier = new CachedSupplier<>(topDocsCollector::topDocs);
if (hitCount == -1) {
assert trackTotalHits == false;
if (trackTotalHits == false) {
totalHitsSupplier = () -> new TotalHits(0, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO);
} else {
totalHitsSupplier = () -> new TotalHits(hitCount, TotalHits.Relation.EQUAL_TO);
Expand Down