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