Skip to content

Commit 18866c4

Browse files
authored
Make hits.total an object in the search response (#35849)
This commit changes the format of the `hits.total` in the search response to be an object with a `value` and a `relation`. The `value` indicates the number of hits that match the query and the `relation` indicates whether the number is accurate (in which case the relation is equals to `eq`) or a lower bound of the total (in which case it is equals to `gte`). This change also adds a parameter called `rest_total_hits_as_int` that can be used in the search APIs to opt out from this change (retrieve the total hits as a number in the rest response). Note that currently all search responses are accurate (`track_total_hits: true`) or they don't contain `hits.total` (`track_total_hits: true`). We'll add a way to get a lower bound of the total hits in a follow up (to allow numbers to be passed to `track_total_hits`). Relates #33028
1 parent c4181dc commit 18866c4

File tree

491 files changed

+2950
-1362
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

491 files changed

+2950
-1362
lines changed

client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/search/TransportNoopSearchAction.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.elasticsearch.plugin.noop.action.search;
2020

21+
import org.apache.lucene.search.TotalHits;
2122
import org.elasticsearch.action.ActionListener;
2223
import org.elasticsearch.action.search.SearchRequest;
2324
import org.elasticsearch.action.search.SearchResponse;
@@ -47,10 +48,10 @@ public TransportNoopSearchAction(TransportService transportService, ActionFilter
4748
protected void doExecute(Task task, SearchRequest request, ActionListener<SearchResponse> listener) {
4849
listener.onResponse(new SearchResponse(new InternalSearchResponse(
4950
new SearchHits(
50-
new SearchHit[0], 0L, 0.0f),
51+
new SearchHit[0], new TotalHits(0L, TotalHits.Relation.EQUAL_TO), 0.0f),
5152
new InternalAggregations(Collections.emptyList()),
5253
new Suggest(Collections.emptyList()),
53-
new SearchProfileShardResults(Collections.emptyMap()), false, false, 1), "", 1, 1, 0, 0, ShardSearchFailure.EMPTY_ARRAY,
54-
SearchResponse.Clusters.EMPTY));
54+
new SearchProfileShardResults(Collections.emptyMap()), false, false, 1),
55+
"", 1, 1, 0, 0, ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY));
5556
}
5657
}

client/rest-high-level/src/test/java/org/elasticsearch/client/CCRIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public void testIndexFollowing() throws Exception {
104104

105105
SearchRequest leaderSearchRequest = new SearchRequest("leader");
106106
SearchResponse leaderSearchResponse = highLevelClient().search(leaderSearchRequest, RequestOptions.DEFAULT);
107-
assertThat(leaderSearchResponse.getHits().getTotalHits(), equalTo(1L));
107+
assertThat(leaderSearchResponse.getHits().getTotalHits().value, equalTo(1L));
108108

109109
assertBusy(() -> {
110110
CcrStatsRequest ccrStatsRequest = new CcrStatsRequest();
@@ -118,7 +118,7 @@ public void testIndexFollowing() throws Exception {
118118

119119
SearchRequest followerSearchRequest = new SearchRequest("follower");
120120
SearchResponse followerSearchResponse = highLevelClient().search(followerSearchRequest, RequestOptions.DEFAULT);
121-
assertThat(followerSearchResponse.getHits().getTotalHits(), equalTo(1L));
121+
assertThat(followerSearchResponse.getHits().getTotalHits().value, equalTo(1L));
122122
});
123123

124124
PauseFollowRequest pauseFollowRequest = new PauseFollowRequest("follower");
@@ -143,7 +143,7 @@ public void testIndexFollowing() throws Exception {
143143

144144
SearchRequest followerSearchRequest = new SearchRequest("follower");
145145
SearchResponse followerSearchResponse = highLevelClient().search(followerSearchRequest, RequestOptions.DEFAULT);
146-
assertThat(followerSearchResponse.getHits().getTotalHits(), equalTo(2L));
146+
assertThat(followerSearchResponse.getHits().getTotalHits().value, equalTo(2L));
147147
});
148148

149149
// Need to pause prior to unfollowing it:

client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ public void testDeleteByQuery() throws Exception {
882882
assertEquals(0, bulkResponse.getSearchFailures().size());
883883
assertEquals(
884884
2,
885-
highLevelClient().search(new SearchRequest(sourceIndex), RequestOptions.DEFAULT).getHits().totalHits
885+
highLevelClient().search(new SearchRequest(sourceIndex), RequestOptions.DEFAULT).getHits().getTotalHits().value
886886
);
887887
}
888888
{

client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public void testSearchScroll() throws IOException {
174174
SearchResponse searchResponse = restHighLevelClient.scroll(
175175
new SearchScrollRequest(randomAlphaOfLengthBetween(5, 10)), RequestOptions.DEFAULT);
176176
assertEquals(mockSearchResponse.getScrollId(), searchResponse.getScrollId());
177-
assertEquals(0, searchResponse.getHits().totalHits);
177+
assertEquals(0, searchResponse.getHits().getTotalHits().value);
178178
assertEquals(5, searchResponse.getTotalShards());
179179
assertEquals(5, searchResponse.getSuccessfulShards());
180180
assertEquals(100, searchResponse.getTook().getMillis());

client/rest-high-level/src/test/java/org/elasticsearch/client/RollupIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public void testPutStartAndGetRollupJob() throws Exception {
189189
assertBusy(() -> {
190190
SearchResponse searchResponse = highLevelClient().search(new SearchRequest(rollupIndex), RequestOptions.DEFAULT);
191191
assertEquals(0, searchResponse.getFailedShards());
192-
assertEquals(1L, searchResponse.getHits().getTotalHits());
192+
assertEquals(1L, searchResponse.getHits().getTotalHits().value);
193193

194194
SearchHit searchHit = searchResponse.getHits().getAt(0);
195195
Map<String, Object> source = searchHit.getSourceAsMap();

0 commit comments

Comments
 (0)