Search in sources :

Example 1 with ClearScrollResponse

use of org.elasticsearch.action.search.ClearScrollResponse in project elasticsearch by elastic.

the class SearchScrollIT method testClearNonExistentScrollId.

public void testClearNonExistentScrollId() throws Exception {
    createIndex("idx");
    ClearScrollResponse response = client().prepareClearScroll().addScrollId("DnF1ZXJ5VGhlbkZldGNoAwAAAAAAAAABFnRtLWMyRzBqUUQyNk1uM0xDTjJ4S0EAAAAAAAAAARYzNkhxbWFTYVFVNmgxTGQyYUZVYV9nAAAAAAAAAAEWdVcxNWZmRGZSVFN2V0xMUGF2NGx1Zw==").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));
    assertToXContentResponse(response, true, response.getNumFreed());
}
Also used : ClearScrollResponse(org.elasticsearch.action.search.ClearScrollResponse)

Example 2 with ClearScrollResponse

use of org.elasticsearch.action.search.ClearScrollResponse in project elasticsearch by elastic.

the class SearchScrollIT method testThatNonExistingScrollIdReturnsCorrectException.

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);
}
Also used : ClearScrollResponse(org.elasticsearch.action.search.ClearScrollResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 3 with ClearScrollResponse

use of org.elasticsearch.action.search.ClearScrollResponse in project elasticsearch by elastic.

the class ClientScrollableHitSource method clearScroll.

@Override
public void clearScroll(String scrollId, Runnable onCompletion) {
    ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
    clearScrollRequest.addScrollId(scrollId);
    /*
         * Unwrap the client so we don't set our task as the parent. If we *did* set our ID then the clear scroll would be cancelled as
         * if this task is cancelled. But we want to clear the scroll regardless of whether or not the main request was cancelled.
         */
    client.unwrap().clearScroll(clearScrollRequest, new ActionListener<ClearScrollResponse>() {

        @Override
        public void onResponse(ClearScrollResponse response) {
            logger.debug("Freed [{}] contexts", response.getNumFreed());
            onCompletion.run();
        }

        @Override
        public void onFailure(Exception e) {
            logger.warn((Supplier<?>) () -> new ParameterizedMessage("Failed to clear scroll [{}]", scrollId), e);
            onCompletion.run();
        }
    });
}
Also used : Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest) ClearScrollResponse(org.elasticsearch.action.search.ClearScrollResponse) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)

Example 4 with ClearScrollResponse

use of org.elasticsearch.action.search.ClearScrollResponse in project elasticsearch by elastic.

the class SearchScrollIT method testSimpleScrollQueryThenFetchClearAllScrollIds.

public void testSimpleScrollQueryThenFetchClearAllScrollIds() throws Exception {
    client().admin().indices().prepareCreate("test").setSettings(Settings.builder().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().getHits().length, equalTo(35));
    for (SearchHit hit : searchResponse1.getHits()) {
        assertThat(((Number) hit.getSortValues()[0]).longValue(), equalTo(counter1++));
    }
    assertThat(searchResponse2.getHits().getTotalHits(), equalTo(100L));
    assertThat(searchResponse2.getHits().getHits().length, equalTo(35));
    for (SearchHit hit : searchResponse2.getHits()) {
        assertThat(((Number) hit.getSortValues()[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().getHits().length, equalTo(35));
    for (SearchHit hit : searchResponse1.getHits()) {
        assertThat(((Number) hit.getSortValues()[0]).longValue(), equalTo(counter1++));
    }
    assertThat(searchResponse2.getHits().getTotalHits(), equalTo(100L));
    assertThat(searchResponse2.getHits().getHits().length, equalTo(35));
    for (SearchHit hit : searchResponse2.getHits()) {
        assertThat(((Number) hit.getSortValues()[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));
    assertToXContentResponse(clearResponse, true, clearResponse.getNumFreed());
    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);
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) ClearScrollResponse(org.elasticsearch.action.search.ClearScrollResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 5 with ClearScrollResponse

use of org.elasticsearch.action.search.ClearScrollResponse in project elasticsearch by elastic.

the class SearchScrollIT method testSimpleScrollQueryThenFetch_clearScrollIds.

public void testSimpleScrollQueryThenFetch_clearScrollIds() throws Exception {
    client().admin().indices().prepareCreate("test").setSettings(Settings.builder().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().getHits().length, equalTo(35));
    for (SearchHit hit : searchResponse1.getHits()) {
        assertThat(((Number) hit.getSortValues()[0]).longValue(), equalTo(counter1++));
    }
    assertThat(searchResponse2.getHits().getTotalHits(), equalTo(100L));
    assertThat(searchResponse2.getHits().getHits().length, equalTo(35));
    for (SearchHit hit : searchResponse2.getHits()) {
        assertThat(((Number) hit.getSortValues()[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().getHits().length, equalTo(35));
    for (SearchHit hit : searchResponse1.getHits()) {
        assertThat(((Number) hit.getSortValues()[0]).longValue(), equalTo(counter1++));
    }
    assertThat(searchResponse2.getHits().getTotalHits(), equalTo(100L));
    assertThat(searchResponse2.getHits().getHits().length, equalTo(35));
    for (SearchHit hit : searchResponse2.getHits()) {
        assertThat(((Number) hit.getSortValues()[0]).longValue(), equalTo(counter2++));
    }
    ClearScrollResponse clearResponse = client().prepareClearScroll().addScrollId(searchResponse1.getScrollId()).addScrollId(searchResponse2.getScrollId()).execute().actionGet();
    assertThat(clearResponse.isSucceeded(), is(true));
    assertThat(clearResponse.getNumFreed(), greaterThan(0));
    assertThat(clearResponse.status(), equalTo(RestStatus.OK));
    assertToXContentResponse(clearResponse, true, clearResponse.getNumFreed());
    assertThrows(client().prepareSearchScroll(searchResponse1.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)), RestStatus.NOT_FOUND);
    assertThrows(client().prepareSearchScroll(searchResponse2.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)), RestStatus.NOT_FOUND);
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) ClearScrollResponse(org.elasticsearch.action.search.ClearScrollResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

ClearScrollResponse (org.elasticsearch.action.search.ClearScrollResponse)7 SearchResponse (org.elasticsearch.action.search.SearchResponse)3 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)3 SearchHit (org.elasticsearch.search.SearchHit)2 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1 Supplier (org.apache.logging.log4j.util.Supplier)1 ClearScrollRequest (org.elasticsearch.action.search.ClearScrollRequest)1 EsRejectedExecutionException (org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)1