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;
}
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));
}
}
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());
}
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"));
}
}
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());
}
Aggregations