Skip to content
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

Search - add “partial_results” summary flag to SearchResponse #47847

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 @@ -181,6 +181,7 @@ public void testSearchResponseToXContent() throws IOException {
.startObject()
.field("took", 0)
.field("timed_out", false)
.field("partial_results", false)
.startObject("_shards")
.field("total", 0)
.field("successful", 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class SearchResponse extends ActionResponse implements StatusToXContentOb
private static final ParseField SCROLL_ID = new ParseField("_scroll_id");
private static final ParseField TOOK = new ParseField("took");
private static final ParseField TIMED_OUT = new ParseField("timed_out");
private static final ParseField PARTIAL = new ParseField("partial_results");
private static final ParseField TERMINATED_EARLY = new ParseField("terminated_early");
private static final ParseField NUM_REDUCE_PHASES = new ParseField("num_reduce_phases");

Expand Down Expand Up @@ -134,6 +135,15 @@ public Suggest getSuggest() {
public boolean isTimedOut() {
return internalResponse.timedOut();
}

/**
* Are the search results potentially missing some data?
*/
public boolean isPartial() {
int unavailableOrFailedShards = getTotalShards() - getSuccessfulShards();
return isTimedOut() || unavailableOrFailedShards > 0 ||
(isTerminatedEarly() != null && isTerminatedEarly()) ;
}

/**
* Has the search operation terminated early due to reaching
Expand Down Expand Up @@ -237,6 +247,7 @@ public XContentBuilder innerToXContent(XContentBuilder builder, Params params) t
}
builder.field(TOOK.getPreferredName(), tookInMillis);
builder.field(TIMED_OUT.getPreferredName(), isTimedOut());
builder.field(PARTIAL.getPreferredName(), isPartial());
if (isTerminatedEarly() != null) {
builder.field(TERMINATED_EARLY.getPreferredName(), isTerminatedEarly());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ public void testBuildSearchResponseAllowPartialFailures() {
assertSame(searchResponse.getSuggest(), internalSearchResponse.suggest());
assertSame(searchResponse.getProfileResults(), internalSearchResponse.profile());
assertSame(searchResponse.getHits(), internalSearchResponse.hits());
assertThat(searchResponse.isPartial(), equalTo(true));
}

public void testSendSearchResponseDisallowPartialFailures() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ public void testToXContent() {
{
expectedString.append("\"took\":0,");
expectedString.append("\"timed_out\":false,");
expectedString.append("\"partial_results\":false,");
expectedString.append("\"_shards\":");
{
expectedString.append("{\"total\":0,");
Expand Down Expand Up @@ -250,6 +251,7 @@ public void testToXContent() {
{
expectedString.append("\"took\":0,");
expectedString.append("\"timed_out\":false,");
expectedString.append("\"partial_results\":false,");
expectedString.append("\"_shards\":");
{
expectedString.append("{\"total\":0,");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ private SearchResponse ensureSearchWasCancelled(ActionFuture<SearchResponse> sea
SearchResponse response = searchResponse.actionGet();
logger.info("Search response {}", response);
assertNotEquals("At least one shard should have failed", 0, response.getFailedShards());
assertThat(response.isPartial(), equalTo(true));
return response;
} catch (SearchPhaseExecutionException ex) {
logger.info("All shards failed with", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public void testSimpleTimeout() throws Exception {
.setAllowPartialSearchResults(true)
.get();
assertThat(searchResponse.isTimedOut(), equalTo(true));
assertThat(searchResponse.isPartial(), equalTo(true));
}

public void testPartialResultsIntolerantTimeout() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void testAllowPartialsWithRedState() throws Exception {
assertThat("Expect no shards skipped", searchResponse.getSkippedShards(), equalTo(0));
assertThat("Expect subset of shards successful", searchResponse.getSuccessfulShards(), lessThan(numShards));
assertThat("Expected total shards", searchResponse.getTotalShards(), equalTo(numShards));
assertThat(searchResponse.isPartial(), equalTo(true));
}

public void testClusterAllowPartialsWithRedState() throws Exception {
Expand All @@ -70,6 +71,7 @@ public void testClusterAllowPartialsWithRedState() throws Exception {
assertThat("Expect no shards skipped", searchResponse.getSkippedShards(), equalTo(0));
assertThat("Expect subset of shards successful", searchResponse.getSuccessfulShards(), lessThan(numShards));
assertThat("Expected total shards", searchResponse.getTotalShards(), equalTo(numShards));
assertThat(searchResponse.isPartial(), equalTo(true));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,13 @@ public void testSimpleQueryStringLenient() throws ExecutionException, Interrupte
assertFailures(searchResponse);
assertHitCount(searchResponse, 1L);
assertSearchHits(searchResponse, "1");
assertThat(searchResponse.isPartial(), equalTo(true));

searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("foo").field("field").lenient(true)).get();
assertNoFailures(searchResponse);
assertHitCount(searchResponse, 1L);
assertSearchHits(searchResponse, "1");
assertThat(searchResponse.isPartial(), equalTo(false));
}

// Issue #7967
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ public static void assertFailures(SearchRequestBuilder searchRequestBuilder, Res
//we can either run into partial or total failures depending on the current number of shards
try {
SearchResponse searchResponse = searchRequestBuilder.get();
assertThat(searchResponse.isPartial(), equalTo(true));
assertThat("Expected shard failures, got none", searchResponse.getShardFailures().length, greaterThan(0));
for (ShardSearchFailure shardSearchFailure : searchResponse.getShardFailures()) {
assertThat(shardSearchFailure.status(), equalTo(restStatus));
Expand Down Expand Up @@ -336,6 +337,8 @@ public static void assertAllSuccessful(SearchResponse response) {
assertNoFailures(response);
assertThat("Expected all shards successful",
response.getSuccessfulShards(), equalTo(response.getTotalShards()));
// Is this a valid assumption for all tests? Do some test assume "success" when there's timeouts or red state?
assertThat(response.isPartial(), equalTo(false));
}

public static void assertHighlight(SearchResponse resp, int hit, String field, int fragment, Matcher<String> matcher) {
Expand Down