Search in sources :

Example 1 with ClearScrollRequest

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

the class RequestConverters method clearScroll.

static Request clearScroll(ClearScrollRequest clearScrollRequest) throws IOException {
    Request request = new Request(HttpDelete.METHOD_NAME, "/_search/scroll");
    request.setEntity(createEntity(clearScrollRequest, REQUEST_BODY_CONTENT_TYPE));
    return request;
}
Also used : BulkRequest(org.opensearch.action.bulk.BulkRequest) WriteRequest(org.opensearch.action.support.WriteRequest) AbstractBulkByScrollRequest(org.opensearch.index.reindex.AbstractBulkByScrollRequest) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) CountRequest(org.opensearch.client.core.CountRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) RankEvalRequest(org.opensearch.index.rankeval.RankEvalRequest) GetSourceRequest(org.opensearch.client.core.GetSourceRequest) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) DocWriteRequest(org.opensearch.action.DocWriteRequest) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest) ExplainRequest(org.opensearch.action.explain.ExplainRequest) SearchRequest(org.opensearch.action.search.SearchRequest) DeleteStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest) GetRequest(org.opensearch.action.get.GetRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ClusterHealthRequest(org.opensearch.action.admin.cluster.health.ClusterHealthRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest)

Example 2 with ClearScrollRequest

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

the class RestClearScrollActionTests method testBodyParamsOverrideQueryStringParams.

public void testBodyParamsOverrideQueryStringParams() throws Exception {
    SetOnce<Boolean> scrollCalled = new SetOnce<>();
    try (NodeClient nodeClient = new NoOpNodeClient(this.getTestName()) {

        @Override
        public void clearScroll(ClearScrollRequest request, ActionListener<ClearScrollResponse> listener) {
            scrollCalled.set(true);
            assertThat(request.getScrollIds(), hasSize(1));
            assertThat(request.getScrollIds().get(0), equalTo("BODY"));
        }
    }) {
        RestClearScrollAction action = new RestClearScrollAction();
        RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withParams(Collections.singletonMap("scroll_id", "QUERY_STRING")).withContent(new BytesArray("{\"scroll_id\": [\"BODY\"]}"), XContentType.JSON).build();
        FakeRestChannel channel = new FakeRestChannel(request, false, 0);
        action.handleRequest(request, channel, nodeClient);
        assertThat(scrollCalled.get(), equalTo(true));
    }
}
Also used : NodeClient(org.opensearch.client.node.NodeClient) NoOpNodeClient(org.opensearch.test.client.NoOpNodeClient) BytesArray(org.opensearch.common.bytes.BytesArray) SetOnce(org.apache.lucene.util.SetOnce) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) RestClearScrollAction(org.opensearch.rest.action.search.RestClearScrollAction) RestRequest(org.opensearch.rest.RestRequest) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) ActionListener(org.opensearch.action.ActionListener) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) NoOpNodeClient(org.opensearch.test.client.NoOpNodeClient) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel)

Example 3 with ClearScrollRequest

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

the class RequestConvertersTests method testClearScroll.

public void testClearScroll() throws IOException {
    ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
    int numScrolls = randomIntBetween(1, 10);
    for (int i = 0; i < numScrolls; i++) {
        clearScrollRequest.addScrollId(randomAlphaOfLengthBetween(5, 10));
    }
    Request request = RequestConverters.clearScroll(clearScrollRequest);
    assertEquals(HttpDelete.METHOD_NAME, request.getMethod());
    assertEquals("/_search/scroll", request.getEndpoint());
    assertEquals(0, request.getParameters().size());
    assertToXContentBody(clearScrollRequest, request.getEntity());
    assertEquals(REQUEST_BODY_CONTENT_TYPE.mediaTypeWithoutParameters(), request.getEntity().getContentType().getValue());
}
Also used : RandomSearchRequestGenerator.randomSearchRequest(org.opensearch.search.RandomSearchRequestGenerator.randomSearchRequest) MasterNodeRequest(org.opensearch.action.support.master.MasterNodeRequest) WriteRequest(org.opensearch.action.support.WriteRequest) AbstractBulkByScrollRequest(org.opensearch.index.reindex.AbstractBulkByScrollRequest) RatedRequest(org.opensearch.index.rankeval.RatedRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) AcknowledgedRequest(org.opensearch.action.support.master.AcknowledgedRequest) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) GetSourceRequest(org.opensearch.client.core.GetSourceRequest) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) DocWriteRequest(org.opensearch.action.DocWriteRequest) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest) ExplainRequest(org.opensearch.action.explain.ExplainRequest) SearchRequest(org.opensearch.action.search.SearchRequest) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) BulkRequest(org.opensearch.action.bulk.BulkRequest) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) ReplicationRequest(org.opensearch.action.support.replication.ReplicationRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) CountRequest(org.opensearch.client.core.CountRequest) BulkShardRequest(org.opensearch.action.bulk.BulkShardRequest) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) RankEvalRequest(org.opensearch.index.rankeval.RankEvalRequest) DeleteStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest) GetRequest(org.opensearch.action.get.GetRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest)

Example 4 with ClearScrollRequest

use of org.opensearch.action.search.ClearScrollRequest 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 5 with ClearScrollRequest

use of org.opensearch.action.search.ClearScrollRequest 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)

Aggregations

ClearScrollRequest (org.opensearch.action.search.ClearScrollRequest)10 SearchRequest (org.opensearch.action.search.SearchRequest)5 BulkRequest (org.opensearch.action.bulk.BulkRequest)4 ActionListener (org.opensearch.action.ActionListener)3 ExplainRequest (org.opensearch.action.explain.ExplainRequest)3 FieldCapabilitiesRequest (org.opensearch.action.fieldcaps.FieldCapabilitiesRequest)3 ClearScrollResponse (org.opensearch.action.search.ClearScrollResponse)3 MultiSearchRequest (org.opensearch.action.search.MultiSearchRequest)3 SearchScrollRequest (org.opensearch.action.search.SearchScrollRequest)3 CountRequest (org.opensearch.client.core.CountRequest)3 MultiSearchTemplateRequest (org.opensearch.script.mustache.MultiSearchTemplateRequest)3 SearchTemplateRequest (org.opensearch.script.mustache.SearchTemplateRequest)3 ActionRequest (org.opensearch.action.ActionRequest)2 ActionResponse (org.opensearch.action.ActionResponse)2 ActionType (org.opensearch.action.ActionType)2 DocWriteRequest (org.opensearch.action.DocWriteRequest)2 DeleteStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest)2 GetStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest)2 PutStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest)2 DeleteRequest (org.opensearch.action.delete.DeleteRequest)2