Skip to content

Commit 9335b3a

Browse files
committed
Search: Add a timed_out element indicating if the search request timed out, closes elastic#592.
1 parent ff6d725 commit 9335b3a

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

modules/elasticsearch/src/main/java/org/elasticsearch/action/search/SearchResponse.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@ public Facets getFacets() {
9494
return facets();
9595
}
9696

97+
/**
98+
* Has the search operation timed out.
99+
*/
100+
public boolean timedOut() {
101+
return internalResponse.timedOut();
102+
}
103+
104+
/**
105+
* Has the search operation timed out.
106+
*/
107+
public boolean isTimedOut() {
108+
return timedOut();
109+
}
110+
97111
/**
98112
* How long the search took.
99113
*/
@@ -205,13 +219,15 @@ static final class Fields {
205219
static final XContentBuilderString SHARD = new XContentBuilderString("shard");
206220
static final XContentBuilderString REASON = new XContentBuilderString("reason");
207221
static final XContentBuilderString TOOK = new XContentBuilderString("took");
222+
static final XContentBuilderString TIMED_OUT = new XContentBuilderString("timed_out");
208223
}
209224

210225
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
211226
if (scrollId != null) {
212227
builder.field(Fields._SCROLL_ID, scrollId);
213228
}
214229
builder.field(Fields.TOOK, tookInMillis);
230+
builder.field(Fields.TIMED_OUT, timedOut());
215231
builder.startObject(Fields._SHARDS);
216232
builder.field(Fields.TOTAL, totalShards());
217233
builder.field(Fields.SUCCESSFUL, successfulShards());

modules/elasticsearch/src/main/java/org/elasticsearch/search/controller/SearchPhaseController.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,11 @@ public InternalSearchResponse merge(ShardDoc[] sortedDocs, Map<SearchShardTarget
223223
// count the total (we use the query result provider here, since we might not get any hits (we scrolled past them))
224224
long totalHits = 0;
225225
float maxScore = Float.NEGATIVE_INFINITY;
226+
boolean timedOut = false;
226227
for (QuerySearchResultProvider queryResultProvider : queryResults.values()) {
228+
if (queryResultProvider.queryResult().searchTimedOut()) {
229+
timedOut = true;
230+
}
227231
totalHits += queryResultProvider.queryResult().topDocs().totalHits;
228232
if (!Float.isNaN(queryResultProvider.queryResult().topDocs().getMaxScore())) {
229233
maxScore = Math.max(maxScore, queryResultProvider.queryResult().topDocs().getMaxScore());
@@ -265,7 +269,8 @@ public InternalSearchResponse merge(ShardDoc[] sortedDocs, Map<SearchShardTarget
265269
}
266270
}
267271
}
272+
268273
InternalSearchHits searchHits = new InternalSearchHits(hits.toArray(new InternalSearchHit[hits.size()]), totalHits, maxScore);
269-
return new InternalSearchResponse(searchHits, facets);
274+
return new InternalSearchResponse(searchHits, facets, timedOut);
270275
}
271276
}

modules/elasticsearch/src/main/java/org/elasticsearch/search/internal/InternalSearchResponse.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,19 @@ public class InternalSearchResponse implements Streamable, ToXContent {
4141

4242
private InternalFacets facets;
4343

44+
private boolean timedOut;
45+
4446
private InternalSearchResponse() {
4547
}
4648

47-
public InternalSearchResponse(InternalSearchHits hits, InternalFacets facets) {
49+
public InternalSearchResponse(InternalSearchHits hits, InternalFacets facets, boolean timedOut) {
4850
this.hits = hits;
4951
this.facets = facets;
52+
this.timedOut = timedOut;
53+
}
54+
55+
public boolean timedOut() {
56+
return this.timedOut;
5057
}
5158

5259
public SearchHits hits() {
@@ -75,6 +82,7 @@ public static InternalSearchResponse readInternalSearchResponse(StreamInput in)
7582
if (in.readBoolean()) {
7683
facets = InternalFacets.readFacets(in);
7784
}
85+
timedOut = in.readBoolean();
7886
}
7987

8088
@Override public void writeTo(StreamOutput out) throws IOException {
@@ -85,5 +93,6 @@ public static InternalSearchResponse readInternalSearchResponse(StreamInput in)
8593
out.writeBoolean(true);
8694
facets.writeTo(out);
8795
}
96+
out.writeBoolean(timedOut);
8897
}
8998
}

0 commit comments

Comments
 (0)