|
| 1 | +/* |
| 2 | + * Copyright 2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.elasticsearch.client.elc; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.function.Function; |
| 20 | + |
| 21 | +import org.jetbrains.annotations.Nullable; |
| 22 | + |
| 23 | +import co.elastic.clients.ApiClient; |
| 24 | +import co.elastic.clients.elasticsearch._types.ElasticsearchException; |
| 25 | +import co.elastic.clients.elasticsearch.sql.QueryRequest; |
| 26 | +import co.elastic.clients.elasticsearch.sql.QueryResponse; |
| 27 | +import co.elastic.clients.transport.ElasticsearchTransport; |
| 28 | +import co.elastic.clients.transport.TransportOptions; |
| 29 | +import co.elastic.clients.util.ObjectBuilder; |
| 30 | +import reactor.core.publisher.Mono; |
| 31 | + |
| 32 | +/** |
| 33 | + * Reactive version of {@link co.elastic.clients.elasticsearch.sql.ElasticsearchSqlClient}. |
| 34 | + * |
| 35 | + * @author Aouichaoui Youssef |
| 36 | + * @since 5.4 |
| 37 | + */ |
| 38 | +public class ReactiveElasticsearchSqlClient extends ApiClient<ElasticsearchTransport, ReactiveElasticsearchSqlClient> { |
| 39 | + public ReactiveElasticsearchSqlClient(ElasticsearchTransport transport, @Nullable TransportOptions transportOptions) { |
| 40 | + super(transport, transportOptions); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public ReactiveElasticsearchSqlClient withTransportOptions(@Nullable TransportOptions transportOptions) { |
| 45 | + return new ReactiveElasticsearchSqlClient(transport, transportOptions); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Executes a SQL request |
| 50 | + * |
| 51 | + * @param fn a function that initializes a builder to create the {@link QueryRequest}. |
| 52 | + */ |
| 53 | + public final Mono<QueryResponse> query(Function<QueryRequest.Builder, ObjectBuilder<QueryRequest>> fn) |
| 54 | + throws IOException, ElasticsearchException { |
| 55 | + return query(fn.apply(new QueryRequest.Builder()).build()); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Executes a SQL request. |
| 60 | + */ |
| 61 | + public Mono<QueryResponse> query(QueryRequest query) { |
| 62 | + return Mono.fromFuture(transport.performRequestAsync(query, QueryRequest._ENDPOINT, transportOptions)); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Executes a SQL request. |
| 67 | + */ |
| 68 | + public Mono<QueryResponse> query() { |
| 69 | + return Mono.fromFuture( |
| 70 | + transport.performRequestAsync(new QueryRequest.Builder().build(), QueryRequest._ENDPOINT, transportOptions)); |
| 71 | + } |
| 72 | +} |
0 commit comments