Search in sources :

Example 6 with ClearScrollResponse

use of org.opensearch.action.search.ClearScrollResponse in project OpenSearch by opensearch-project.

the class SearchScrollIT method testSimpleScrollQueryThenFetchClearAllScrollIds.

public void testSimpleScrollQueryThenFetchClearAllScrollIds() throws Exception {
    client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put("index.number_of_shards", 3)).get();
    client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().get();
    client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().get();
    for (int i = 0; i < 100; i++) {
        client().prepareIndex("test").setId(Integer.toString(i)).setSource(jsonBuilder().startObject().field("field", i).endObject()).get();
    }
    client().admin().indices().prepareRefresh().get();
    SearchResponse searchResponse1 = client().prepareSearch().setQuery(matchAllQuery()).setSize(35).setScroll(TimeValue.timeValueMinutes(2)).setSearchType(SearchType.QUERY_THEN_FETCH).addSort("field", SortOrder.ASC).get();
    SearchResponse searchResponse2 = client().prepareSearch().setQuery(matchAllQuery()).setSize(35).setScroll(TimeValue.timeValueMinutes(2)).setSearchType(SearchType.QUERY_THEN_FETCH).addSort("field", SortOrder.ASC).get();
    long counter1 = 0;
    long counter2 = 0;
    assertThat(searchResponse1.getHits().getTotalHits().value, 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().value, 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)).get();
    searchResponse2 = client().prepareSearchScroll(searchResponse2.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)).get();
    assertThat(searchResponse1.getHits().getTotalHits().value, 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().value, 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").get();
    assertThat(clearResponse.isSucceeded(), is(true));
    assertThat(clearResponse.getNumFreed(), greaterThan(0));
    assertThat(clearResponse.status(), equalTo(RestStatus.OK));
    assertToXContentResponse(clearResponse, true, clearResponse.getNumFreed());
    assertRequestBuilderThrows(internalCluster().client().prepareSearchScroll(searchResponse1.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)), RestStatus.NOT_FOUND);
    assertRequestBuilderThrows(internalCluster().client().prepareSearchScroll(searchResponse2.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)), RestStatus.NOT_FOUND);
}
Also used : SearchHit(org.opensearch.search.SearchHit) ClearScrollResponse(org.opensearch.action.search.ClearScrollResponse) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 7 with ClearScrollResponse

use of org.opensearch.action.search.ClearScrollResponse in project OpenSearch by opensearch-project.

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)).get();
    client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().get();
    client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().get();
    for (int i = 0; i < 100; i++) {
        client().prepareIndex("test").setId(Integer.toString(i)).setSource(jsonBuilder().startObject().field("field", i).endObject()).get();
    }
    client().admin().indices().prepareRefresh().get();
    SearchResponse searchResponse1 = client().prepareSearch().setQuery(matchAllQuery()).setSize(35).setScroll(TimeValue.timeValueMinutes(2)).setSearchType(SearchType.QUERY_THEN_FETCH).addSort("field", SortOrder.ASC).get();
    SearchResponse searchResponse2 = client().prepareSearch().setQuery(matchAllQuery()).setSize(35).setScroll(TimeValue.timeValueMinutes(2)).setSearchType(SearchType.QUERY_THEN_FETCH).addSort("field", SortOrder.ASC).get();
    long counter1 = 0;
    long counter2 = 0;
    assertThat(searchResponse1.getHits().getTotalHits().value, 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().value, 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)).get();
    searchResponse2 = client().prepareSearchScroll(searchResponse2.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)).get();
    assertThat(searchResponse1.getHits().getTotalHits().value, 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().value, 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()).get();
    assertThat(clearResponse.isSucceeded(), is(true));
    assertThat(clearResponse.getNumFreed(), greaterThan(0));
    assertThat(clearResponse.status(), equalTo(RestStatus.OK));
    assertToXContentResponse(clearResponse, true, clearResponse.getNumFreed());
    assertRequestBuilderThrows(client().prepareSearchScroll(searchResponse1.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)), RestStatus.NOT_FOUND);
    assertRequestBuilderThrows(client().prepareSearchScroll(searchResponse2.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)), RestStatus.NOT_FOUND);
}
Also used : SearchHit(org.opensearch.search.SearchHit) ClearScrollResponse(org.opensearch.action.search.ClearScrollResponse) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 8 with ClearScrollResponse

use of org.opensearch.action.search.ClearScrollResponse in project OpenSearch by opensearch-project.

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(() -> new ParameterizedMessage("Failed to clear scroll [{}]", scrollId), e);
            onCompletion.run();
        }
    });
}
Also used : ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) ClearScrollResponse(org.opensearch.action.search.ClearScrollResponse) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException)

Example 9 with ClearScrollResponse

use of org.opensearch.action.search.ClearScrollResponse in project OpenSearch by opensearch-project.

the class OpenSearchIntegTestCase method clearScroll.

/**
 * Clears the given scroll Ids
 */
public void clearScroll(String... scrollIds) {
    ClearScrollResponse clearResponse = client().prepareClearScroll().setScrollIds(Arrays.asList(scrollIds)).get();
    assertThat(clearResponse.isSucceeded(), equalTo(true));
}
Also used : ClearScrollResponse(org.opensearch.action.search.ClearScrollResponse)

Example 10 with ClearScrollResponse

use of org.opensearch.action.search.ClearScrollResponse in project OpenSearch by opensearch-project.

the class ClearScrollResponseTests method testToXContent.

public void testToXContent() throws IOException {
    ClearScrollResponse clearScrollResponse = new ClearScrollResponse(true, 10);
    try (XContentBuilder builder = JsonXContent.contentBuilder()) {
        clearScrollResponse.toXContent(builder, ToXContent.EMPTY_PARAMS);
    }
    assertEquals(true, clearScrollResponse.isSucceeded());
    assertEquals(10, clearScrollResponse.getNumFreed());
}
Also used : ClearScrollResponse(org.opensearch.action.search.ClearScrollResponse) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Aggregations

ClearScrollResponse (org.opensearch.action.search.ClearScrollResponse)10 SearchResponse (org.opensearch.action.search.SearchResponse)4 ClearScrollRequest (org.opensearch.action.search.ClearScrollRequest)3 SearchHit (org.opensearch.search.SearchHit)3 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)3 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)2 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1 OpenSearchException (org.opensearch.OpenSearchException)1 OpenSearchStatusException (org.opensearch.OpenSearchStatusException)1 ExplainRequest (org.opensearch.action.explain.ExplainRequest)1 FieldCapabilitiesRequest (org.opensearch.action.fieldcaps.FieldCapabilitiesRequest)1 MultiSearchRequest (org.opensearch.action.search.MultiSearchRequest)1 MultiSearchResponse (org.opensearch.action.search.MultiSearchResponse)1 SearchRequest (org.opensearch.action.search.SearchRequest)1 SearchScrollRequest (org.opensearch.action.search.SearchScrollRequest)1 CountRequest (org.opensearch.client.core.CountRequest)1 BytesReference (org.opensearch.common.bytes.BytesReference)1 OpenSearchRejectedExecutionException (org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException)1 XContentParser (org.opensearch.common.xcontent.XContentParser)1 XContentType (org.opensearch.common.xcontent.XContentType)1