Skip to content

Commit

Permalink
Make hits.total an object in the search response (#35849)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jimczi authored Dec 5, 2018
1 parent c4181dc commit 18866c4
Show file tree
Hide file tree
Showing 491 changed files with 2,950 additions and 1,362 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.elasticsearch.plugin.noop.action.search;

import org.apache.lucene.search.TotalHits;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
Expand Down Expand Up @@ -47,10 +48,10 @@ public TransportNoopSearchAction(TransportService transportService, ActionFilter
protected void doExecute(Task task, SearchRequest request, ActionListener<SearchResponse> listener) {
listener.onResponse(new SearchResponse(new InternalSearchResponse(
new SearchHits(
new SearchHit[0], 0L, 0.0f),
new SearchHit[0], new TotalHits(0L, TotalHits.Relation.EQUAL_TO), 0.0f),
new InternalAggregations(Collections.emptyList()),
new Suggest(Collections.emptyList()),
new SearchProfileShardResults(Collections.emptyMap()), false, false, 1), "", 1, 1, 0, 0, ShardSearchFailure.EMPTY_ARRAY,
SearchResponse.Clusters.EMPTY));
new SearchProfileShardResults(Collections.emptyMap()), false, false, 1),
"", 1, 1, 0, 0, ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void testIndexFollowing() throws Exception {

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

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

SearchRequest followerSearchRequest = new SearchRequest("follower");
SearchResponse followerSearchResponse = highLevelClient().search(followerSearchRequest, RequestOptions.DEFAULT);
assertThat(followerSearchResponse.getHits().getTotalHits(), equalTo(1L));
assertThat(followerSearchResponse.getHits().getTotalHits().value, equalTo(1L));
});

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

SearchRequest followerSearchRequest = new SearchRequest("follower");
SearchResponse followerSearchResponse = highLevelClient().search(followerSearchRequest, RequestOptions.DEFAULT);
assertThat(followerSearchResponse.getHits().getTotalHits(), equalTo(2L));
assertThat(followerSearchResponse.getHits().getTotalHits().value, equalTo(2L));
});

// Need to pause prior to unfollowing it:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ public void testDeleteByQuery() throws Exception {
assertEquals(0, bulkResponse.getSearchFailures().size());
assertEquals(
2,
highLevelClient().search(new SearchRequest(sourceIndex), RequestOptions.DEFAULT).getHits().totalHits
highLevelClient().search(new SearchRequest(sourceIndex), RequestOptions.DEFAULT).getHits().getTotalHits().value
);
}
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public void testSearchScroll() throws IOException {
SearchResponse searchResponse = restHighLevelClient.scroll(
new SearchScrollRequest(randomAlphaOfLengthBetween(5, 10)), RequestOptions.DEFAULT);
assertEquals(mockSearchResponse.getScrollId(), searchResponse.getScrollId());
assertEquals(0, searchResponse.getHits().totalHits);
assertEquals(0, searchResponse.getHits().getTotalHits().value);
assertEquals(5, searchResponse.getTotalShards());
assertEquals(5, searchResponse.getSuccessfulShards());
assertEquals(100, searchResponse.getTook().getMillis());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public void testPutStartAndGetRollupJob() throws Exception {
assertBusy(() -> {
SearchResponse searchResponse = highLevelClient().search(new SearchRequest(rollupIndex), RequestOptions.DEFAULT);
assertEquals(0, searchResponse.getFailedShards());
assertEquals(1L, searchResponse.getHits().getTotalHits());
assertEquals(1L, searchResponse.getHits().getTotalHits().value);

SearchHit searchHit = searchResponse.getHits().getAt(0);
Map<String, Object> source = searchHit.getSourceAsMap();
Expand Down
Loading

0 comments on commit 18866c4

Please sign in to comment.