3131import org .elasticsearch .common .Strings ;
3232import org .elasticsearch .common .io .stream .StreamInput ;
3333import org .elasticsearch .common .io .stream .StreamOutput ;
34+ import org .elasticsearch .common .settings .Settings ;
35+ import org .elasticsearch .common .unit .TimeValue ;
3436import org .elasticsearch .common .xcontent .XContentBuilder ;
3537import org .elasticsearch .index .IndexService ;
3638import org .elasticsearch .index .query .AbstractQueryBuilder ;
4446import org .elasticsearch .search .builder .SearchSourceBuilder ;
4547import org .elasticsearch .search .fetch .ShardFetchRequest ;
4648import org .elasticsearch .search .internal .AliasFilter ;
49+ import org .elasticsearch .search .internal .SearchContext ;
4750import org .elasticsearch .search .internal .ShardSearchLocalRequest ;
4851import org .elasticsearch .search .query .QuerySearchResultProvider ;
4952import org .elasticsearch .test .ESSingleNodeTestCase ;
5962import static java .util .Collections .singletonList ;
6063import static org .elasticsearch .action .support .WriteRequest .RefreshPolicy .IMMEDIATE ;
6164import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertAcked ;
65+ import static org .hamcrest .CoreMatchers .equalTo ;
6266import static org .hamcrest .Matchers .is ;
6367import static org .hamcrest .Matchers .notNullValue ;
6468
@@ -74,6 +78,11 @@ protected Collection<Class<? extends Plugin>> getPlugins() {
7478 return pluginList (FailOnRewriteQueryPlugin .class );
7579 }
7680
81+ @ Override
82+ protected Settings nodeSettings () {
83+ return Settings .builder ().put ("search.default_search_timeout" , "5s" ).build ();
84+ }
85+
7786 public void testClearOnClose () throws ExecutionException , InterruptedException {
7887 createIndex ("index" );
7988 client ().prepareIndex ("index" , "type" , "1" ).setSource ("field" , "value" ).setRefreshPolicy (IMMEDIATE ).get ();
@@ -197,6 +206,40 @@ public void onFailure(Exception e) {
197206 }
198207 }
199208
209+ public void testTimeout () throws IOException {
210+ createIndex ("index" );
211+ final SearchService service = getInstanceFromNode (SearchService .class );
212+ final IndicesService indicesService = getInstanceFromNode (IndicesService .class );
213+ final IndexService indexService = indicesService .indexServiceSafe (resolveIndex ("index" ));
214+ final IndexShard indexShard = indexService .getShard (0 );
215+ final SearchContext contextWithDefaultTimeout = service .createContext (
216+ new ShardSearchLocalRequest (
217+ indexShard .shardId (),
218+ 1 ,
219+ SearchType .DEFAULT ,
220+ new SearchSourceBuilder (),
221+ new String [0 ],
222+ false ,
223+ new AliasFilter (null , Strings .EMPTY_ARRAY )),
224+ null );
225+ // the search context should inherit the default timeout
226+ assertThat (contextWithDefaultTimeout .timeout (), equalTo (TimeValue .timeValueSeconds (5 )));
227+
228+ final long seconds = randomIntBetween (6 , 10 );
229+ final SearchContext context = service .createContext (
230+ new ShardSearchLocalRequest (
231+ indexShard .shardId (),
232+ 1 ,
233+ SearchType .DEFAULT ,
234+ new SearchSourceBuilder ().timeout (TimeValue .timeValueSeconds (seconds )),
235+ new String [0 ],
236+ false ,
237+ new AliasFilter (null , Strings .EMPTY_ARRAY )),
238+ null );
239+ // the search context should inherit the query timeout
240+ assertThat (context .timeout (), equalTo (TimeValue .timeValueSeconds (seconds )));
241+ }
242+
200243 public static class FailOnRewriteQueryPlugin extends Plugin implements SearchPlugin {
201244 @ Override
202245 public List <QuerySpec <?>> getQueries () {
0 commit comments