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