@Test
  public void testThatNonExistingScrollIdReturnsCorrectException() throws Exception {
    client().prepareIndex("index", "type", "1").setSource("field", "value").execute().get();
    refresh();

    SearchResponse searchResponse =
        client().prepareSearch("index").setSize(1).setScroll("1m").get();
    assertThat(searchResponse.getScrollId(), is(notNullValue()));

    ClearScrollResponse clearScrollResponse =
        client().prepareClearScroll().addScrollId(searchResponse.getScrollId()).get();
    assertThat(clearScrollResponse.isSucceeded(), is(true));

    assertThrows(
        internalCluster().transportClient().prepareSearchScroll(searchResponse.getScrollId()),
        RestStatus.NOT_FOUND);
  }
 @Test
 public void testClearNonExistentScrollId() throws Exception {
   createIndex("idx");
   ClearScrollResponse response =
       client()
           .prepareClearScroll()
           .addScrollId(
               "cXVlcnlUaGVuRmV0Y2g7MzsyOlpBRC1qOUhrUjhhZ0NtQWUxU2FuWlE7MjpRcjRaNEJ2R1JZV1VEMW02ZGF1LW5ROzI6S0xUal9lZDRTd3lWNUhUU2VSb01CQTswOw==")
           .get();
   // Whether we actually clear a scroll, we can't know, since that information isn't serialized in
   // the
   // free search context response, which is returned from each node we want to clear a particular
   // scroll.
   assertThat(response.isSucceeded(), is(true));
   assertThat(response.getNumFreed(), equalTo(0));
   assertThat(response.status(), equalTo(RestStatus.NOT_FOUND));
 }
  @Test
  public void testSimpleScrollQueryThenFetch_clearAllScrollIds() throws Exception {
    client()
        .admin()
        .indices()
        .prepareCreate("test")
        .setSettings(Settings.settingsBuilder().put("index.number_of_shards", 3))
        .execute()
        .actionGet();
    client()
        .admin()
        .cluster()
        .prepareHealth()
        .setWaitForEvents(Priority.LANGUID)
        .setWaitForGreenStatus()
        .execute()
        .actionGet();

    client()
        .admin()
        .cluster()
        .prepareHealth()
        .setWaitForEvents(Priority.LANGUID)
        .setWaitForGreenStatus()
        .execute()
        .actionGet();

    for (int i = 0; i < 100; i++) {
      client()
          .prepareIndex("test", "type1", Integer.toString(i))
          .setSource(jsonBuilder().startObject().field("field", i).endObject())
          .execute()
          .actionGet();
    }

    client().admin().indices().prepareRefresh().execute().actionGet();

    SearchResponse searchResponse1 =
        client()
            .prepareSearch()
            .setQuery(matchAllQuery())
            .setSize(35)
            .setScroll(TimeValue.timeValueMinutes(2))
            .setSearchType(SearchType.QUERY_THEN_FETCH)
            .addSort("field", SortOrder.ASC)
            .execute()
            .actionGet();

    SearchResponse searchResponse2 =
        client()
            .prepareSearch()
            .setQuery(matchAllQuery())
            .setSize(35)
            .setScroll(TimeValue.timeValueMinutes(2))
            .setSearchType(SearchType.QUERY_THEN_FETCH)
            .addSort("field", SortOrder.ASC)
            .execute()
            .actionGet();

    long counter1 = 0;
    long counter2 = 0;

    assertThat(searchResponse1.getHits().getTotalHits(), equalTo(100l));
    assertThat(searchResponse1.getHits().hits().length, equalTo(35));
    for (SearchHit hit : searchResponse1.getHits()) {
      assertThat(((Number) hit.sortValues()[0]).longValue(), equalTo(counter1++));
    }

    assertThat(searchResponse2.getHits().getTotalHits(), equalTo(100l));
    assertThat(searchResponse2.getHits().hits().length, equalTo(35));
    for (SearchHit hit : searchResponse2.getHits()) {
      assertThat(((Number) hit.sortValues()[0]).longValue(), equalTo(counter2++));
    }

    searchResponse1 =
        client()
            .prepareSearchScroll(searchResponse1.getScrollId())
            .setScroll(TimeValue.timeValueMinutes(2))
            .execute()
            .actionGet();

    searchResponse2 =
        client()
            .prepareSearchScroll(searchResponse2.getScrollId())
            .setScroll(TimeValue.timeValueMinutes(2))
            .execute()
            .actionGet();

    assertThat(searchResponse1.getHits().getTotalHits(), equalTo(100l));
    assertThat(searchResponse1.getHits().hits().length, equalTo(35));
    for (SearchHit hit : searchResponse1.getHits()) {
      assertThat(((Number) hit.sortValues()[0]).longValue(), equalTo(counter1++));
    }

    assertThat(searchResponse2.getHits().getTotalHits(), equalTo(100l));
    assertThat(searchResponse2.getHits().hits().length, equalTo(35));
    for (SearchHit hit : searchResponse2.getHits()) {
      assertThat(((Number) hit.sortValues()[0]).longValue(), equalTo(counter2++));
    }

    ClearScrollResponse clearResponse =
        client().prepareClearScroll().addScrollId("_all").execute().actionGet();
    assertThat(clearResponse.isSucceeded(), is(true));
    assertThat(clearResponse.getNumFreed(), greaterThan(0));
    assertThat(clearResponse.status(), equalTo(RestStatus.OK));

    assertThrows(
        internalCluster()
            .transportClient()
            .prepareSearchScroll(searchResponse1.getScrollId())
            .setScroll(TimeValue.timeValueMinutes(2)),
        RestStatus.NOT_FOUND);
    assertThrows(
        internalCluster()
            .transportClient()
            .prepareSearchScroll(searchResponse2.getScrollId())
            .setScroll(TimeValue.timeValueMinutes(2)),
        RestStatus.NOT_FOUND);
  }