@Override public String toString() { return String.format("ES search scroll request for scroll id '%s'", super.request().scroll()); } }
SearchScrollRequest searchScrollRequest = new SearchScrollRequest(scrollState.getScrollId()) .scroll(scrollTimeout); request.next(searchScrollRequest);
@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) { // NOTE: if rest request with xcontent body has request parameters, values parsed from request body have the precedence try { searchScrollRequest.fromXContent(xContentParser); } catch (IOException e) { throw new IllegalArgumentException("Failed to parse request body", e); } }}); return channel -> client.searchScroll(searchScrollRequest, new RestStatusToXContentListener<>(channel)); }
public <T> Page<T> continueScroll(@Nullable String scrollId, long scrollTimeInMillis, Class<T> clazz) { SearchScrollRequest request = new SearchScrollRequest(scrollId); request.scroll(TimeValue.timeValueMillis(scrollTimeInMillis)); SearchResponse response; try { response = client.searchScroll(request); } catch (IOException e) { throw new ElasticsearchException("Error for search request with scroll: " + request.toString(), e); } return resultsMapper.mapResults(response, clazz, Pageable.unpaged()); }
@Override protected void doStartNextScroll(String scrollId, TimeValue extraKeepAlive, Consumer<? super Response> onResponse) { searchWithRetry(listener -> { SearchScrollRequest request = new SearchScrollRequest(); // Add the wait time into the scroll timeout so it won't timeout while we wait for throttling request.scrollId(scrollId).scroll(timeValueNanos(firstSearchRequest.scroll().keepAlive().nanos() + extraKeepAlive.nanos())); client.searchScroll(request, listener); }, r -> consume(r, onResponse)); }
/** * Parse a search scroll request from a request body provided through the REST layer. * Values that are already be set and are also found while parsing will be overridden. */ public void fromXContent(XContentParser parser) throws IOException { if (parser.nextToken() != XContentParser.Token.START_OBJECT) { throw new IllegalArgumentException("Malformed content, must start with an object"); } else { XContentParser.Token token; String currentFieldName = null; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if ("scroll_id".equals(currentFieldName) && token == XContentParser.Token.VALUE_STRING) { scrollId(parser.text()); } else if ("scroll".equals(currentFieldName) && token == XContentParser.Token.VALUE_STRING) { scroll(new Scroll(TimeValue.parseTimeValue(parser.text(), null, "scroll"))); } else { throw new IllegalArgumentException("Unknown parameter [" + currentFieldName + "] in request body or parameter is of the wrong type[" + token + "] "); } } } } }
/** * Creates a search scroll request allowing to continue searching a previous search request. * * @param scrollId The scroll id representing the scrollable search * @return The search scroll request * @see org.elasticsearch.client.Client#searchScroll(org.elasticsearch.action.search.SearchScrollRequest) */ public static SearchScrollRequest searchScrollRequest(String scrollId) { return new SearchScrollRequest(scrollId); }
/** * The scroll id to use to continue scrolling. */ public SearchScrollRequestBuilder setScrollId(String scrollId) { request.scrollId(scrollId); return this; }
@Override public Task createTask(long id, String type, String action, TaskId parentTaskId, Map<String, String> headers) { return new SearchTask(id, type, action, getDescription(), parentTaskId, headers); }
void executeScroll(final String scrollId) { try { logger.trace("executing scroll request [{}]", scrollId); scrollAction.execute(new SearchScrollRequest(request).scrollId(scrollId).scroll(request.scroll()), new ActionListener<SearchResponse>() { @Override public void onResponse(SearchResponse scrollResponse) { deleteHits(scrollId, scrollResponse); } @Override public void onFailure(Throwable e) { logger.error("scroll request [{}] failed, scrolling document(s) is stopped", e, scrollId); finishHim(scrollId, hasTimedOut(), e); } }); } catch (Throwable t) { logger.error("unable to execute scroll request [{}]", t, scrollId); finishHim(scrollId, false, t); } }
protected final void sendResponse(SearchPhaseController.ReducedQueryPhase queryPhase, final AtomicArray<? extends SearchPhaseResult> fetchResults) { try { final InternalSearchResponse internalResponse = searchPhaseController.merge(true, queryPhase, fetchResults.asList(), fetchResults::get); // the scroll ID never changes we always return the same ID. This ID contains all the shards and their context ids // such that we can talk to them abgain in the next roundtrip. String scrollId = null; if (request.scroll() != null) { scrollId = request.scrollId(); } listener.onResponse(new SearchResponse(internalResponse, scrollId, this.scrollId.getContext().length, successfulOps.get(), 0, buildTookInMillis(), buildShardFailures(), SearchResponse.Clusters.EMPTY)); } catch (Exception e) { listener.onFailure(new ReduceSearchPhaseException("fetch", "inner finish failed", e, buildShardFailures())); } }
public SearchScrollRequestBuilder(ElasticsearchClient client, SearchScrollAction action, String scrollId) { super(client, action, new SearchScrollRequest(scrollId)); }
public <T> Page<T> continueScroll(@Nullable String scrollId, long scrollTimeInMillis, Class<T> clazz, SearchResultMapper mapper) { SearchScrollRequest request = new SearchScrollRequest(scrollId); request.scroll(TimeValue.timeValueMillis(scrollTimeInMillis)); SearchResponse response; try { response = client.searchScroll(request); } catch (IOException e) { throw new ElasticsearchException("Error for search request with scroll: " + request.toString(), e); } return mapper.mapResults(response, clazz, Pageable.unpaged()); }
@Override protected void doExecute(Task task, SearchScrollRequest request, ActionListener<SearchResponse> listener) { try { ParsedScrollId scrollId = parseScrollId(request.scrollId()); Runnable action; switch (scrollId.getType()) { case QUERY_THEN_FETCH_TYPE: action = new SearchScrollQueryThenFetchAsyncAction(logger, clusterService, searchTransportService, searchPhaseController, request, (SearchTask)task, scrollId, listener); break; case QUERY_AND_FETCH_TYPE: // TODO can we get rid of this? action = new SearchScrollQueryAndFetchAsyncAction(logger, clusterService, searchTransportService, searchPhaseController, request, (SearchTask)task, scrollId, listener); break; default: throw new IllegalArgumentException("Scroll id type [" + scrollId.getType() + "] unrecognized"); } action.run(); } catch (Exception e) { listener.onFailure(e); } } }
@Override public Task createTask(long id, String type, String action, TaskId parentTaskId) { return new SearchTask(id, type, action, getDescription(), parentTaskId); }
@Override protected void doStartNextScroll(String scrollId, TimeValue extraKeepAlive, Consumer<? super Response> onResponse) { searchWithRetry(listener -> { SearchScrollRequest request = new SearchScrollRequest(); // Add the wait time into the scroll timeout so it won't timeout while we wait for throttling request.scrollId(scrollId).scroll(timeValueNanos(firstSearchRequest.scroll().keepAlive().nanos() + extraKeepAlive.nanos())); client.searchScroll(request, listener); }, r -> consume(r, onResponse)); }
/** * If set, will enable scrolling of the search request. */ public SearchScrollRequestBuilder setScroll(Scroll scroll) { request.scroll(scroll); return this; }
@Override protected SearchResponse scrollSearchResponse(final String scrollId) throws IOException { return _client.searchScroll(new SearchScrollRequest(scrollId).scroll( AbstractElasticSearchDataContext.TIMEOUT_SCROLL)); } }
@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) { // NOTE: if rest request with xcontent body has request parameters, values parsed from request body have the precedence try { searchScrollRequest.fromXContent(xContentParser); } catch (IOException e) { throw new IllegalArgumentException("Failed to parse request body", e); } }}); return channel -> client.searchScroll(searchScrollRequest, new RestStatusToXContentListener<>(channel)); } }
private void innerFinishHim() { InternalSearchResponse internalResponse = searchPhaseController.merge(sortedShardList, queryResults, fetchResults, request); String scrollId = null; if (request.scroll() != null) { scrollId = request.scrollId(); } listener.onResponse(new SearchResponse(internalResponse, scrollId, this.scrollId.getContext().length, successfulOps.get(), buildTookInMillis(), buildShardFailures())); } }