Search in sources :

Example 6 with ClearScrollRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.ClearScrollRequest in project sonarqube by SonarSource.

the class EsTester method getDocuments.

private List<SearchHit> getDocuments(SearchRequest req) {
    req.scroll(new TimeValue(60000));
    req.source().size(100).sort("_doc", SortOrder.ASC);
    SearchResponse response = ES_REST_CLIENT.search(req);
    List<SearchHit> result = newArrayList();
    while (true) {
        Iterables.addAll(result, response.getHits());
        response = ES_REST_CLIENT.scroll(new SearchScrollRequest(response.getScrollId()).scroll(new TimeValue(600000)));
        // Break condition: No hits are returned
        if (response.getHits().getHits().length == 0) {
            ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
            clearScrollRequest.addScrollId(response.getScrollId());
            ES_REST_CLIENT.clearScroll(clearScrollRequest);
            break;
        }
    }
    return result;
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest) TimeValue(org.elasticsearch.core.TimeValue) SearchScrollRequest(org.elasticsearch.action.search.SearchScrollRequest) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 7 with ClearScrollRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.ClearScrollRequest in project sonarqube by SonarSource.

the class BulkIndexer method addDeletion.

public void addDeletion(SearchRequest searchRequest) {
    // TODO to be replaced by delete_by_query that is back in ES5
    searchRequest.scroll(TimeValue.timeValueMinutes(5)).source().sort("_doc", SortOrder.ASC).size(100).fetchSource(false);
    // this search is synchronous. An optimization would be to be non-blocking,
    // but it requires to tracking pending requests in close().
    // Same semaphore can't be reused because of potential deadlock (requires to acquire
    // two locks)
    SearchResponse searchResponse = esClient.search(searchRequest);
    while (true) {
        SearchHit[] hits = searchResponse.getHits().getHits();
        for (SearchHit hit : hits) {
            DocumentField routing = hit.field("_routing");
            DeleteRequest deleteRequest = new DeleteRequest(hit.getIndex(), hit.getType(), hit.getId());
            if (routing != null) {
                deleteRequest.routing(routing.getValue());
            }
            add(deleteRequest);
        }
        String scrollId = searchResponse.getScrollId();
        if (scrollId == null) {
            break;
        }
        searchResponse = esClient.scroll(new SearchScrollRequest(scrollId).scroll(TimeValue.timeValueMinutes(5)));
        if (hits.length == 0) {
            ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
            clearScrollRequest.addScrollId(scrollId);
            esClient.clearScroll(clearScrollRequest);
            break;
        }
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) DocumentField(org.elasticsearch.common.document.DocumentField) ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) SearchScrollRequest(org.elasticsearch.action.search.SearchScrollRequest) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 8 with ClearScrollRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.ClearScrollRequest in project elasticsearch by elastic.

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((Supplier<?>) () -> new ParameterizedMessage("Failed to clear scroll [{}]", scrollId), e);
            onCompletion.run();
        }
    });
}
Also used : Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest) ClearScrollResponse(org.elasticsearch.action.search.ClearScrollResponse) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)

Example 9 with ClearScrollRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.ClearScrollRequest in project elasticsearch by elastic.

the class RestClearScrollActionTests method testParseClearScrollRequestWithUnknownParamThrowsException.

public void testParseClearScrollRequestWithUnknownParamThrowsException() throws Exception {
    XContentParser invalidContent = createParser(XContentFactory.jsonBuilder().startObject().array("scroll_id", "value_1", "value_2").field("unknown", "keyword").endObject());
    ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
    Exception e = expectThrows(IllegalArgumentException.class, () -> RestClearScrollAction.buildFromContent(invalidContent, clearScrollRequest));
    assertThat(e.getMessage(), startsWith("Unknown parameter [unknown]"));
}
Also used : ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 10 with ClearScrollRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.ClearScrollRequest in project sonarqube by SonarSource.

the class ViewIndex method findAllViewUuids.

public List<String> findAllViewUuids() {
    SearchRequest esSearch = EsClient.prepareSearch(ViewIndexDefinition.TYPE_VIEW).source(new SearchSourceBuilder().sort("_doc", SortOrder.ASC).fetchSource(false).size(100).query(matchAllQuery())).scroll(TimeValue.timeValueMinutes(SCROLL_TIME_IN_MINUTES));
    SearchResponse response = esClient.search(esSearch);
    List<String> result = new ArrayList<>();
    while (true) {
        List<SearchHit> hits = newArrayList(response.getHits());
        for (SearchHit hit : hits) {
            result.add(hit.getId());
        }
        String scrollId = response.getScrollId();
        response = esClient.scroll(new SearchScrollRequest().scrollId(scrollId).scroll(TimeValue.timeValueMinutes(SCROLL_TIME_IN_MINUTES)));
        // Break condition: No hits are returned
        if (response.getHits().getHits().length == 0) {
            ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
            clearScrollRequest.addScrollId(scrollId);
            esClient.clearScroll(clearScrollRequest);
            break;
        }
    }
    return result;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest) SearchScrollRequest(org.elasticsearch.action.search.SearchScrollRequest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

ClearScrollRequest (org.elasticsearch.action.search.ClearScrollRequest)10 SearchRequest (org.elasticsearch.action.search.SearchRequest)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)2 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 SearchScrollRequest (org.elasticsearch.action.search.SearchScrollRequest)2 XContentParser (org.elasticsearch.common.xcontent.XContentParser)2 SearchHit (org.elasticsearch.search.SearchHit)2 PrestoException (com.facebook.presto.spi.PrestoException)1 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 Arrays (java.util.Arrays)1 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1 Supplier (org.apache.logging.log4j.util.Supplier)1 Action (org.elasticsearch.action.Action)1 ActionListener (org.elasticsearch.action.ActionListener)1 ActionRequest (org.elasticsearch.action.ActionRequest)1 ActionRequestBuilder (org.elasticsearch.action.ActionRequestBuilder)1 ActionResponse (org.elasticsearch.action.ActionResponse)1 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)1