use of io.openk9.search.client.api.ReactorActionListener 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("{}"));
}
use of io.openk9.search.client.api.ReactorActionListener in project openk9 by smclab.
the class ReindexDatasourceConsumer method activate.
@Activate
void activate() {
RestHighLevelClient restHighLevelClient = _restHighLevelClientProvider.get();
_disposable = _datasourceEventConsumer.datasourceUpdateEvents().flatMap(datasource -> _pluginDriverManagerClient.getPluginDriver(datasource.getDriverServiceName()).onErrorResume(throwable -> {
if (_log.isErrorEnabled()) {
_log.error(throwable.getMessage());
}
return Mono.empty();
}).map(pluginDriverDTO -> Tuples.of(datasource, datasource.getTenantId() + "-" + pluginDriverDTO.getName() + "-data"))).filterWhen(t2 -> Mono.create(sink -> restHighLevelClient.indices().existsAsync(new GetIndexRequest(t2.getT2()), RequestOptions.DEFAULT, new ReactorActionListener<>(sink)))).flatMap(t2 -> Mono.<GetSettingsResponse>create(sink -> restHighLevelClient.indices().getSettingsAsync(new GetSettingsRequest().indices(t2.getT2()).names("index.creation_date"), RequestOptions.DEFAULT, new ReactorActionListener<>(sink))).map(response -> Tuples.of(t2.getT1(), t2.getT2(), Instant.ofEpochMilli(Long.parseLong(response.getSetting(t2.getT2(), "index.creation_date")))))).log().filter(t3 -> t3.getT1().getLastIngestionDate().isBefore(t3.getT3())).flatMap(t3 -> Mono.<AcknowledgedResponse>create(sink -> restHighLevelClient.indices().deleteAsync(new DeleteIndexRequest(t3.getT2()), RequestOptions.DEFAULT, new ReactorActionListener<>(sink)))).onErrorContinue((throwable, ignore) -> {
if (_log.isErrorEnabled()) {
_log.error(throwable.getMessage());
}
}).subscribe();
}
use of io.openk9.search.client.api.ReactorActionListener in project openk9 by smclab.
the class IndexWriterEndpoins method _deleteDataDocuments.
private Publisher<Void> _deleteDataDocuments(HttpServerRequest httpServerRequest, HttpServerResponse httpServerResponse) {
RestHighLevelClient restHighLevelClient = _restHighLevelClientProvider.get();
Mono<byte[]> body = ReactorNettyUtils.aggregateBodyAsByteArray(httpServerRequest);
Mono<String> responseMono = body.map(bytes -> _jsonFactory.fromJson(bytes, DeleteEntitiesRequest.class)).flatMap(deleteEntitiesRequest -> {
if (!deleteEntitiesRequest.getContentIds().isEmpty()) {
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
for (String contentId : deleteEntitiesRequest.getContentIds()) {
boolQuery.mustNot(matchQuery("contentId", contentId));
}
Mono<Datasource> datasourceMono = _datasourceClient.findDatasource(deleteEntitiesRequest.getDatasourceId());
return datasourceMono.flatMap(datasource -> _pluginDriverManagerClient.getPluginDriver(datasource.getDriverServiceName()).map(pluginDriverDTO -> datasource.getTenantId() + "-" + pluginDriverDTO.getName() + "-data").map(DeleteByQueryRequest::new).map(deleteByQueryRequest -> deleteByQueryRequest.setQuery(boolQuery)).flatMap(deleteByQueryRequest -> Mono.<BulkByScrollResponse>create(sink -> restHighLevelClient.deleteByQueryAsync(deleteByQueryRequest, RequestOptions.DEFAULT, new ReactorActionListener<>(sink)))).map(Object::toString).doOnNext(_log::info).flatMap(response -> Mono.fromRunnable(() -> _sendCleanOrphanEntitiesRequest(datasource.getTenantId())).thenReturn(response)));
}
return Mono.empty();
});
return _httpResponseWriter.write(httpServerResponse, responseMono);
}
Aggregations