Search in sources :

Example 1 with ClearScrollResponse

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

the class SearchScrollIT method testThatNonExistingScrollIdReturnsCorrectException.

public void testThatNonExistingScrollIdReturnsCorrectException() throws Exception {
    client().prepareIndex("index").setId("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));
    assertRequestBuilderThrows(internalCluster().client().prepareSearchScroll(searchResponse.getScrollId()), RestStatus.NOT_FOUND);
}
Also used : ClearScrollResponse(org.opensearch.action.search.ClearScrollResponse) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 2 with ClearScrollResponse

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

the class ClearScrollResponseTests method testToAndFromXContent.

public void testToAndFromXContent() throws IOException {
    XContentType xContentType = randomFrom(XContentType.values());
    ClearScrollResponse originalResponse = createTestItem();
    BytesReference originalBytes = toShuffledXContent(originalResponse, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean());
    ClearScrollResponse parsedResponse;
    try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
        parsedResponse = ClearScrollResponse.fromXContent(parser);
    }
    assertEquals(originalResponse.isSucceeded(), parsedResponse.isSucceeded());
    assertEquals(originalResponse.getNumFreed(), parsedResponse.getNumFreed());
    BytesReference parsedBytes = XContentHelper.toXContent(parsedResponse, xContentType, randomBoolean());
    assertToXContentEquivalent(originalBytes, parsedBytes, xContentType);
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) XContentType(org.opensearch.common.xcontent.XContentType) ClearScrollResponse(org.opensearch.action.search.ClearScrollResponse) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 3 with ClearScrollResponse

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

the class SearchIT method testSearchScroll.

public void testSearchScroll() throws Exception {
    for (int i = 0; i < 100; i++) {
        XContentBuilder builder = jsonBuilder().startObject().field("field", i).endObject();
        Request doc = new Request(HttpPut.METHOD_NAME, "/test/_doc/" + Integer.toString(i));
        doc.setJsonEntity(Strings.toString(builder));
        client().performRequest(doc);
    }
    client().performRequest(new Request(HttpPost.METHOD_NAME, "/test/_refresh"));
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(35).sort("field", SortOrder.ASC);
    SearchRequest searchRequest = new SearchRequest("test").scroll(TimeValue.timeValueMinutes(2)).source(searchSourceBuilder);
    SearchResponse searchResponse = execute(searchRequest, highLevelClient()::search, highLevelClient()::searchAsync);
    try {
        long counter = 0;
        assertSearchHeader(searchResponse);
        assertThat(searchResponse.getHits().getTotalHits().value, equalTo(100L));
        assertThat(searchResponse.getHits().getHits().length, equalTo(35));
        for (SearchHit hit : searchResponse.getHits()) {
            assertThat(((Number) hit.getSortValues()[0]).longValue(), equalTo(counter++));
        }
        searchResponse = execute(new SearchScrollRequest(searchResponse.getScrollId()).scroll(TimeValue.timeValueMinutes(2)), highLevelClient()::scroll, highLevelClient()::scrollAsync);
        assertThat(searchResponse.getHits().getTotalHits().value, equalTo(100L));
        assertThat(searchResponse.getHits().getHits().length, equalTo(35));
        for (SearchHit hit : searchResponse.getHits()) {
            assertEquals(counter++, ((Number) hit.getSortValues()[0]).longValue());
        }
        searchResponse = execute(new SearchScrollRequest(searchResponse.getScrollId()).scroll(TimeValue.timeValueMinutes(2)), highLevelClient()::scroll, highLevelClient()::scrollAsync);
        assertThat(searchResponse.getHits().getTotalHits().value, equalTo(100L));
        assertThat(searchResponse.getHits().getHits().length, equalTo(30));
        for (SearchHit hit : searchResponse.getHits()) {
            assertEquals(counter++, ((Number) hit.getSortValues()[0]).longValue());
        }
    } finally {
        ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
        clearScrollRequest.addScrollId(searchResponse.getScrollId());
        ClearScrollResponse clearScrollResponse = execute(clearScrollRequest, highLevelClient()::clearScroll, highLevelClient()::clearScrollAsync);
        assertThat(clearScrollResponse.getNumFreed(), greaterThan(0));
        assertTrue(clearScrollResponse.isSucceeded());
        SearchScrollRequest scrollRequest = new SearchScrollRequest(searchResponse.getScrollId()).scroll(TimeValue.timeValueMinutes(2));
        OpenSearchStatusException exception = expectThrows(OpenSearchStatusException.class, () -> execute(scrollRequest, highLevelClient()::scroll, highLevelClient()::scrollAsync));
        assertEquals(RestStatus.NOT_FOUND, exception.status());
        assertThat(exception.getRootCause(), instanceOf(OpenSearchException.class));
        OpenSearchException rootCause = (OpenSearchException) exception.getRootCause();
        assertThat(rootCause.getMessage(), containsString("No search context found for"));
    }
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) SearchHit(org.opensearch.search.SearchHit) CountRequest(org.opensearch.client.core.CountRequest) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest) ExplainRequest(org.opensearch.action.explain.ExplainRequest) SearchRequest(org.opensearch.action.search.SearchRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) OpenSearchException(org.opensearch.OpenSearchException) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) ClearScrollResponse(org.opensearch.action.search.ClearScrollResponse) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) OpenSearchStatusException(org.opensearch.OpenSearchStatusException)

Example 4 with ClearScrollResponse

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

the class RestHighLevelClientTests method testClearScroll.

public void testClearScroll() throws IOException {
    ClearScrollResponse mockClearScrollResponse = new ClearScrollResponse(randomBoolean(), randomIntBetween(0, Integer.MAX_VALUE));
    mockResponse(mockClearScrollResponse);
    ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
    clearScrollRequest.addScrollId(randomAlphaOfLengthBetween(5, 10));
    ClearScrollResponse clearScrollResponse = restHighLevelClient.clearScroll(clearScrollRequest, RequestOptions.DEFAULT);
    assertEquals(mockClearScrollResponse.isSucceeded(), clearScrollResponse.isSucceeded());
    assertEquals(mockClearScrollResponse.getNumFreed(), clearScrollResponse.getNumFreed());
}
Also used : ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) ClearScrollResponse(org.opensearch.action.search.ClearScrollResponse)

Example 5 with ClearScrollResponse

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

the class SearchScrollIT method testClearNonExistentScrollId.

public void testClearNonExistentScrollId() throws Exception {
    createIndex("idx");
    ClearScrollResponse response = client().prepareClearScroll().addScrollId("DnF1ZXJ5VGhlbkZldGNoAwAAAAAAAAABFnRtLWMyRzBqUUQyNk1uM0xDTjJ4S0EAAAAAAAAAARYzNkhxbWFTYVFVNmgxTGQyYUZVYV9nAA" + "AAAAAAAAEWdVcxNWZmRGZSVFN2V0xMUGF2NGx1Zw==").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.opensearch.action.search.ClearScrollResponse)

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