Skip to content

Commit

Permalink
Add opensearch-java client support
Browse files Browse the repository at this point in the history
Signed-off-by: Andriy Redko <andriy.redko@aiven.io>
  • Loading branch information
reta committed Apr 16, 2024
1 parent 3a63173 commit ead77f9
Show file tree
Hide file tree
Showing 32 changed files with 7,693 additions and 0 deletions.
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencyResolutionManagement {

create("opensearchLibs") {
version("opensearch", "2.13.0")
library("java-client", "org.opensearch.client:opensearch-java:2.10.0")
library("client", "org.opensearch.client", "opensearch-rest-client").versionRef("opensearch")
library("high-level-client", "org.opensearch.client", "opensearch-rest-high-level-client").versionRef("opensearch")
library("sniffer", "org.opensearch.client", "opensearch-rest-client-sniffer").versionRef("opensearch")
Expand Down
1 change: 1 addition & 0 deletions spring-data-opensearch/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies {
implementation(springLibs.context)
implementation(springLibs.tx)
compileOnly(springLibs.web)
compileOnly(opensearchLibs.java.client)

testImplementation("jakarta.enterprise:jakarta.enterprise.cdi-api:3.0.0")
testImplementation("org.slf4j:log4j-over-slf4j:2.0.12")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2022-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opensearch.data.client.osc;

import org.opensearch.client.opensearch._types.aggregations.Aggregate;

/**
* Class to combine an OpenSearch {@link org.opensearch.client.opensearch._types.aggregations.Aggregate} with its
* name. Necessary as the OpenSearch Aggregate does not know its name.
*
* @author Peter-Josef Meisch
* @since 4.4
*/
public class Aggregation {

private final String name;
private final Aggregate aggregate;

public Aggregation(String name, Aggregate aggregate) {
this.name = name;
this.aggregate = aggregate;
}

public String getName() {
return name;
}

public Aggregate getAggregate() {
return aggregate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2021-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opensearch.data.client.osc;

import org.opensearch.client.RestClient;
import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.client.opensearch.cluster.OpenSearchClusterClient;
import org.opensearch.client.transport.OpenSearchTransport;
import org.springframework.util.Assert;

/**
* Extension of the {@link OpenSearchClient} class that implements {@link AutoCloseable}. As the underlying
* {@link RestClient} must be closed properly this is handled in the {@link #close()} method.
*
* @author Peter-Josef Meisch
* @since 4.4
*/
public class AutoCloseableOpenSearchClient extends OpenSearchClient implements AutoCloseable {

public AutoCloseableOpenSearchClient(OpenSearchTransport transport) {
super(transport);
Assert.notNull(transport, "transport must not be null");
}

@Override
public void close() throws Exception {
transport.close();
}

@Override
public OpenSearchClusterClient cluster() {
return super.cluster();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2021-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opensearch.data.client.osc;

import java.io.IOException;
import org.opensearch.client.ApiClient;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.opensearch.cluster.OpenSearchClusterClient;
import org.opensearch.client.transport.Transport;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import org.springframework.util.Assert;

/**
* base class for a template that uses one of the {@link org.opensearch.client.opensearch.OpenSearchClient}'s child
* clients like {@link OpenSearchClusterClient} or
* {@link org.opensearch.client.opensearch.indices.OpenSearchIndicesClient}.
*
* @author Peter-Josef Meisch
* @since 4.4
*/
public abstract class ChildTemplate<T extends Transport, CLIENT extends ApiClient<T, CLIENT>> {

protected final CLIENT client;
protected final RequestConverter requestConverter;
protected final ResponseConverter responseConverter;
protected final OpenSearchExceptionTranslator exceptionTranslator;

public ChildTemplate(CLIENT client, ElasticsearchConverter elasticsearchConverter) {
this.client = client;
JsonpMapper jsonpMapper = client._transport().jsonpMapper();
requestConverter = new RequestConverter(elasticsearchConverter, jsonpMapper);
responseConverter = new ResponseConverter(jsonpMapper);
exceptionTranslator = new OpenSearchExceptionTranslator(jsonpMapper);
}

/**
* Callback interface to be used with {@link #execute(ClientCallback)} for operating directly on the client.
*/
@FunctionalInterface
public interface ClientCallback<CLIENT, RESULT> {
RESULT doWithClient(CLIENT client) throws IOException;
}

/**
* Execute a callback with the client and provide exception translation.
*
* @param callback the callback to execute, must not be {@literal null}
* @param <RESULT> the type returned from the callback
* @return the callback result
*/
public <RESULT> RESULT execute(ClientCallback<CLIENT, RESULT> callback) {

Assert.notNull(callback, "callback must not be null");

try {
return callback.doWithClient(client);
} catch (IOException | RuntimeException e) {
throw exceptionTranslator.translateException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2021-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opensearch.data.client.osc;

import org.opensearch.client.opensearch.cluster.HealthRequest;
import org.opensearch.client.opensearch.cluster.HealthResponse;
import org.opensearch.client.opensearch.cluster.OpenSearchClusterClient;
import org.opensearch.client.transport.OpenSearchTransport;
import org.springframework.data.elasticsearch.core.cluster.ClusterHealth;
import org.springframework.data.elasticsearch.core.cluster.ClusterOperations;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;

/**
* Implementation of the {@link ClusterOperations} interface using en {@link OpenSearchClusterClient}.
*
* @author Peter-Josef Meisch
* @since 4.4
*/
public class ClusterTemplate extends ChildTemplate<OpenSearchTransport, OpenSearchClusterClient>
implements ClusterOperations {

public ClusterTemplate(OpenSearchClusterClient client, ElasticsearchConverter elasticsearchConverter) {
super(client, elasticsearchConverter);
}

@Override
public ClusterHealth health() {

HealthRequest healthRequest = requestConverter.clusterHealthRequest();
HealthResponse healthResponse = execute(client -> client.health(healthRequest));
return responseConverter.clusterHealth(healthResponse);
}

}
Loading

0 comments on commit ead77f9

Please sign in to comment.