Search in sources :

Example 1 with SearchScrollRequest

use of org.elasticsearch.action.search.SearchScrollRequest in project elasticsearch by elastic.

the class RestSearchScrollAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    String scrollId = request.param("scroll_id");
    SearchScrollRequest searchScrollRequest = new SearchScrollRequest();
    searchScrollRequest.scrollId(scrollId);
    String scroll = request.param("scroll");
    if (scroll != null) {
        searchScrollRequest.scroll(new Scroll(parseTimeValue(scroll, null, "scroll")));
    }
    request.withContentOrSourceParamParserOrNull(xContentParser -> {
        if (xContentParser != null) {
            try {
                buildFromContent(xContentParser, searchScrollRequest);
            } catch (IOException e) {
                throw new IllegalArgumentException("Failed to parse request body", e);
            }
        }
    });
    return channel -> client.searchScroll(searchScrollRequest, new RestStatusToXContentListener<>(channel));
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestStatusToXContentListener(org.elasticsearch.rest.action.RestStatusToXContentListener) Scroll(org.elasticsearch.search.Scroll) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) XContentParser(org.elasticsearch.common.xcontent.XContentParser) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) TimeValue(org.elasticsearch.common.unit.TimeValue) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) TimeValue.parseTimeValue(org.elasticsearch.common.unit.TimeValue.parseTimeValue) SearchScrollRequest(org.elasticsearch.action.search.SearchScrollRequest) Scroll(org.elasticsearch.search.Scroll) IOException(java.io.IOException) SearchScrollRequest(org.elasticsearch.action.search.SearchScrollRequest)

Example 2 with SearchScrollRequest

use of org.elasticsearch.action.search.SearchScrollRequest in project elasticsearch by elastic.

the class RestSearchScrollActionTests method testParseSearchScrollRequest.

public void testParseSearchScrollRequest() throws Exception {
    XContentParser content = createParser(XContentFactory.jsonBuilder().startObject().field("scroll_id", "SCROLL_ID").field("scroll", "1m").endObject());
    SearchScrollRequest searchScrollRequest = new SearchScrollRequest();
    RestSearchScrollAction.buildFromContent(content, searchScrollRequest);
    assertThat(searchScrollRequest.scrollId(), equalTo("SCROLL_ID"));
    assertThat(searchScrollRequest.scroll().keepAlive(), equalTo(TimeValue.parseTimeValue("1m", null, "scroll")));
}
Also used : XContentParser(org.elasticsearch.common.xcontent.XContentParser) SearchScrollRequest(org.elasticsearch.action.search.SearchScrollRequest)

Example 3 with SearchScrollRequest

use of org.elasticsearch.action.search.SearchScrollRequest in project graylog2-server by Graylog2.

the class ScrollResultES7 method nextSearchResult.

private SearchResponse nextSearchResult() throws IOException {
    final SearchScrollRequest scrollRequest = new SearchScrollRequest(this.scrollId);
    scrollRequest.scroll(TimeValue.parseTimeValue(this.scroll, DEFAULT_SCROLL, "scroll time"));
    return client.executeWithIOException((c, requestOptions) -> c.scroll(scrollRequest, requestOptions), "Unable to retrieve next chunk from search: ");
}
Also used : SearchScrollRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchScrollRequest)

Example 4 with SearchScrollRequest

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

use of org.elasticsearch.action.search.SearchScrollRequest 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)

Aggregations

SearchScrollRequest (org.elasticsearch.action.search.SearchScrollRequest)9 SearchResponse (org.elasticsearch.action.search.SearchResponse)4 SearchHit (org.elasticsearch.search.SearchHit)4 ClearScrollRequest (org.elasticsearch.action.search.ClearScrollRequest)3 XContentParser (org.elasticsearch.common.xcontent.XContentParser)3 Test (org.junit.Test)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 TestSupport (com.hazelcast.jet.core.test.TestSupport)1 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)1 QuickTest (com.hazelcast.test.annotation.QuickTest)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 TotalHits (org.apache.lucene.search.TotalHits)1 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)1 SearchRequest (org.elasticsearch.action.search.SearchRequest)1 NodeClient (org.elasticsearch.client.node.NodeClient)1 BytesArray (org.elasticsearch.common.bytes.BytesArray)1 DocumentField (org.elasticsearch.common.document.DocumentField)1 Settings (org.elasticsearch.common.settings.Settings)1 Text (org.elasticsearch.common.text.Text)1