Skip to content

Commit

Permalink
Initial commit of new “partial_results” summary flag
Browse files Browse the repository at this point in the history
Relates to #47700
  • Loading branch information
markharwood committed Oct 10, 2019
1 parent a4a7967 commit 54ed18e
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 0 deletions.
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() + getSkippedShards());
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 @@ -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

0 comments on commit 54ed18e

Please sign in to comment.