|
46 | 46 | import org.apache.http.util.EntityUtils; |
47 | 47 | import org.elasticsearch.ElasticsearchException; |
48 | 48 | import org.elasticsearch.ElasticsearchStatusException; |
49 | | -import org.elasticsearch.action.ActionRequest; |
50 | 49 | import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; |
51 | 50 | import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest; |
52 | 51 | import org.elasticsearch.action.admin.indices.close.CloseIndexRequest; |
@@ -467,59 +466,30 @@ public Flux<SearchHit> scroll(HttpHeaders headers, SearchRequest searchRequest) |
467 | 466 | searchRequest.scroll(scrollTimeout); |
468 | 467 | } |
469 | 468 |
|
470 | | - Sinks.Many<ActionRequest> requests = Sinks.many().unicast().onBackpressureBuffer(); |
471 | | - Sinks.Many<SearchResponse> inbound = Sinks.many().unicast().onBackpressureBuffer(); |
472 | | - |
473 | | - Flux<SearchResponse> exchange = requests.asFlux().flatMap(it -> { |
474 | | - |
475 | | - if (it instanceof SearchRequest) { |
476 | | - return sendRequest((SearchRequest) it, requestCreator.search(), SearchResponse.class, headers); |
477 | | - } else if (it instanceof SearchScrollRequest) { |
478 | | - return sendRequest((SearchScrollRequest) it, requestCreator.scroll(), SearchResponse.class, headers); |
479 | | - } else if (it instanceof ClearScrollRequest) { |
480 | | - return sendRequest((ClearScrollRequest) it, requestCreator.clearScroll(), ClearScrollResponse.class, headers) |
481 | | - .flatMap(discard -> Flux.empty()); |
482 | | - } |
483 | | - |
484 | | - return Flux.error(new IllegalArgumentException(String |
485 | | - .format("Cannot handle '%s'. Please make sure to use a 'SearchRequest' or 'SearchScrollRequest'.", it))); |
486 | | - }); |
487 | | - |
488 | 469 | return Flux.usingWhen(Mono.fromSupplier(ScrollState::new), |
489 | 470 |
|
490 | | - scrollState -> { |
491 | | - |
492 | | - Flux<SearchHit> searchHits = inbound.asFlux().<SearchResponse> handle((searchResponse, sink) -> { |
| 471 | + state -> { |
493 | 472 |
|
494 | | - scrollState.updateScrollId(searchResponse.getScrollId()); |
495 | | - if (isEmpty(searchResponse.getHits())) { |
| 473 | + return sendRequest(searchRequest, requestCreator.search(), SearchResponse.class, headers) |
| 474 | + .expand(searchResponse -> { |
496 | 475 |
|
497 | | - inbound.tryEmitComplete(); |
498 | | - requests.tryEmitComplete(); |
| 476 | + state.updateScrollId(searchResponse.getScrollId()); |
| 477 | + if (isEmpty(searchResponse.getHits())) { |
| 478 | + return Mono.empty(); |
| 479 | + } |
499 | 480 |
|
500 | | - } else { |
501 | | - |
502 | | - sink.next(searchResponse); |
503 | | - |
504 | | - SearchScrollRequest searchScrollRequest = new SearchScrollRequest(scrollState.getScrollId()) |
505 | | - .scroll(scrollTimeout); |
506 | | - requests.emitNext(searchScrollRequest, Sinks.EmitFailureHandler.FAIL_FAST); |
507 | | - } |
508 | | - |
509 | | - }).map(SearchResponse::getHits) // |
510 | | - .flatMap(Flux::fromIterable); |
511 | | - |
512 | | - return searchHits.doOnSubscribe(ignore -> { |
513 | | - exchange.subscribe(new SinkSubscriber(inbound)); |
514 | | - requests.emitNext(searchRequest, Sinks.EmitFailureHandler.FAIL_FAST); |
515 | | - }); |
| 481 | + return sendRequest(new SearchScrollRequest(searchResponse.getScrollId()).scroll(scrollTimeout), |
| 482 | + requestCreator.scroll(), SearchResponse.class, headers); |
516 | 483 |
|
| 484 | + }); |
517 | 485 | }, state -> cleanupScroll(headers, state), // |
518 | 486 | (state, ex) -> cleanupScroll(headers, state), // |
519 | | - state -> cleanupScroll(headers, state)); // |
| 487 | + state -> cleanupScroll(headers, state)) // |
| 488 | + .filter(it -> !isEmpty(it.getHits())) // |
| 489 | + .map(SearchResponse::getHits) // |
| 490 | + .flatMapIterable(Function.identity()); // |
520 | 491 | } |
521 | 492 |
|
522 | | - |
523 | 493 | private static boolean isEmpty(@Nullable SearchHits hits) { |
524 | 494 | return hits != null && hits.getHits() != null && hits.getHits().length == 0; |
525 | 495 | } |
|
0 commit comments