use of io.openk9.index.writer.entity.model.DocumentEntityRequest in project openk9 by smclab.
the class IndexWriterEndpoins method _insertEntity.
private Publisher<Void> _insertEntity(HttpServerRequest httpRequest, HttpServerResponse httpResponse) {
RestHighLevelClient restHighLevelClient = _restHighLevelClientProvider.get();
Mono<List<DocumentEntityRequest>> request = Mono.from(ReactorNettyUtils.aggregateBodyAsByteArray(httpRequest)).map(json -> _jsonFactory.fromJsonList(json, DocumentEntityRequest.class));
Mono<BulkResponse> elasticResponse = request.flatMapIterable(Function.identity()).map(entity -> {
IndexRequest indexRequest = new IndexRequest(entity.getTenantId() + "-entity");
return indexRequest.source(_jsonFactory.toJson(entity), XContentType.JSON);
}).reduce(new BulkRequest(), BulkRequest::add).flatMap(bulkRequest -> Mono.create(sink -> {
bulkRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
Cancellable cancellable = restHighLevelClient.bulkAsync(bulkRequest, RequestOptions.DEFAULT, new ReactorActionListener<>(sink));
sink.onCancel(cancellable::cancel);
}));
return _httpResponseWriter.write(httpResponse, elasticResponse.thenReturn("{}"));
}
Aggregations