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 May 17, 2024
1 parent dc425d7 commit b305350
Show file tree
Hide file tree
Showing 106 changed files with 12,071 additions and 22 deletions.
86 changes: 80 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@ The Spring Data OpenSearch follows the release model of the Spring Data Elastics

### OpenSearch 2.x / 1.x client libraries

At the moment, Spring Data OpenSearch provides the possibility to use the `RestHighLevelCLient` to connect to OpenSearch clusters.

At the moment, Spring Data OpenSearch provides the possibility to use either `RestHighLevelCLient` or [OpenSearchClient](https://github.com/opensearch-project/opensearch-java) to connect to OpenSearch clusters.

#### Using `RestHighLevelCLient` (default)

By default, the `RestHighLevelCLient` is configured as the means to communicate with OpenSearch clusters.

```xml
<dependency>
<groupId>org.opensearch.client</groupId>
<artifactId>spring-data-opensearch</artifactId>
<version>1.3.0</version>
<version>1.4.0</version>
</dependency>
```

Expand All @@ -49,7 +54,7 @@ To use Spring Boot 3.x auto configuration support:
<dependency>
<groupId>org.opensearch.client</groupId>
<artifactId>spring-data-opensearch-starter</artifactId>
<version>1.3.0</version>
<version>1.4.0</version>
</dependency>
```

Expand All @@ -59,11 +64,80 @@ To use Spring Boot 3.x auto configuration support for testing:
<dependency>
<groupId>org.opensearch.client</groupId>
<artifactId>spring-data-opensearch-test-autoconfigure</artifactId>
<version>1.3.0</version>
<version>1.4.0</version>
<scope>test</scope>
</dependency>
```

#### Using `OpenSearchClient` (preferred)

To switch over to `OpenSearchClient`, the `opensearch-rest-high-level-client` dependency has to be replaced in favor of `opensearch-java`.

```xml
<dependency>
<groupId>org.opensearch.client</groupId>
<artifactId>spring-data-opensearch</artifactId>
<version>1.4.0</version>
<exclusions>
<exclusion>
<groupId>org.opensearch.client</groupId>
<artifactId>opensearch-rest-high-level-client</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.opensearch.client</groupId>
<artifactId>opensearch-java</artifactId>
<version>2.10.1</version>
</dependency>
```

To use Spring Boot 3.x auto configuration support:

```xml
<dependency>
<groupId>org.opensearch.client</groupId>
<artifactId>spring-data-opensearch-starter</artifactId>
<version>1.4.0</version>
<exclusions>
<exclusion>
<groupId>org.opensearch.client</groupId>
<artifactId>opensearch-rest-high-level-client</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.opensearch.client</groupId>
<artifactId>opensearch-java</artifactId>
<version>2.10.1</version>
</dependency>
```

To use Spring Boot 3.x auto configuration support for testing:

```xml
<dependency>
<groupId>org.opensearch.client</groupId>
<artifactId>spring-data-opensearch-test-autoconfigure</artifactId>
<version>1.4.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.opensearch.client</groupId>
<artifactId>opensearch-rest-high-level-client</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.opensearch.client</groupId>
<artifactId>opensearch-java</artifactId>
<version>2.10.1</version>
</dependency>
```

## Getting Started

Here is a quick teaser of an application using Spring Data Repositories in Java:
Expand Down Expand Up @@ -224,7 +298,7 @@ Add the Apache Maven dependency:
<dependency>
<groupId>org.opensearch.client</groupId>
<artifactId>spring-data-opensearch</artifactId>
<version>1.2.1</version>
<version>1.4.0</version>
</dependency>
```

Expand All @@ -251,7 +325,7 @@ Add the Gradle dependency:
```groovy
dependencies {
...
implementation "org.opensearch.client:spring-data-opensearch:1.2.1"
implementation "org.opensearch.client:spring-data-opensearch:1.4.0"
...
}
```
Expand Down
6 changes: 6 additions & 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.14.0")
library("java-client", "org.opensearch.client:opensearch-java:2.10.3")
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 All @@ -43,6 +44,10 @@ dependencyResolutionManagement {
library("databind", "com.fasterxml.jackson.core", "jackson-databind").versionRef("jackson")
}

create("jakarta") {
library("json-bind", "jakarta.json.bind:jakarta.json.bind-api:2.0.0")
}

create("pluginLibs") {
version("spotless", "6.23.1")
version("editorconfig", "0.0.3")
Expand Down Expand Up @@ -78,3 +83,4 @@ include("spring-data-opensearch")
include("spring-data-opensearch-starter")
include("spring-data-opensearch-test-autoconfigure")
include("spring-data-opensearch-examples:spring-boot-gradle")
include("spring-data-opensearch-examples:spring-boot-java-client-gradle")
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This sample project demonstrates the usage of the [Spring Data OpenSearch](https
1. The easiest way to get [OpenSearch](https://opensearch.org) service up and running is by using [Docker](https://www.docker.com/):

```shell
docker run -p 9200:9200 -e "discovery.type=single-node" opensearchproject/opensearch:2.4.0
docker run -p 9200:9200 -e "discovery.type=single-node" opensearchproject/opensearch:2.11.1
```

2. Build and run the project using [Gradle](https://gradle.org/):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Spring Data OpenSearch Java Client Spring Boot Example Project
===

This sample project demonstrates the usage of the [Spring Data OpenSearch](https://github.com/opensearch-project/spring-data-opensearch/) in the typical Spring Boot web application using [opensearch-java](https://github.com/opensearch-project/opensearch-java) client. The application assumes that there is an [OpenSearch](https://opensearch.org) service up and running on the local machine, available at `https://localhost:9200` (protected by basic authentication with default credentials).

1. The easiest way to get [OpenSearch](https://opensearch.org) service up and running is by using [Docker](https://www.docker.com/):

```shell
docker run -p 9200:9200 -e "discovery.type=single-node" opensearchproject/opensearch:2.11.1
```

2. Build and run the project using [Gradle](https://gradle.org/):

```shell
./gradlew :spring-data-opensearch-examples:spring-boot-java-client-gradle:bootRun
```

3. Exercise the REST endpoint available at: `http://localhost:8080/marketplace`

- Fetch all products: `curl 'http://localhost:8080/marketplace/search'`
- Search products by name: `curl 'http://localhost:8080/marketplace/search?name=pillow'`
- Search products by name and price greater than: `curl 'http://localhost:8080/marketplace/search?name=pillow&price=35.0'`
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright OpenSearch Contributors.
* SPDX-License-Identifier: Apache-2.0
*/

plugins {
alias(springLibs.plugins.spring.boot)
alias(pluginLibs.plugins.spotless)
alias(pluginLibs.plugins.editorconfig)
id("java-conventions")
}

buildscript {
dependencies {
classpath(pluginLibs.editorconfig)
classpath(pluginLibs.spotless)
}
}

dependencies {
api(project(":spring-data-opensearch")) {
exclude("org.opensearch.client", "opensearch-rest-high-level-client")
}
api(project(":spring-data-opensearch-starter")) {
exclude("org.opensearch.client", "opensearch-rest-high-level-client")
}
implementation(springLibs.boot.web)
implementation(jacksonLibs.core)
implementation(jacksonLibs.databind)
implementation(opensearchLibs.client)
implementation(opensearchLibs.java.client)
testImplementation(springLibs.test)
testImplementation(springLibs.boot.test)
testImplementation(springLibs.boot.test.autoconfigure)
testImplementation(opensearchLibs.testcontainers)
testImplementation(project(":spring-data-opensearch-test-autoconfigure")) {
exclude("org.opensearch.client", "opensearch-rest-high-level-client")
}

constraints {
implementation("ch.qos.logback:logback-classic") {
version {
require("1.4.12")
}
because("Fixes CVE-2023-6378")
}
}
}

description = "Spring Data OpenSearch Spring Boot Example Project"

spotless {
java {
target("src/main/java/**/*.java", "src/test/java/org/opensearch/**/*.java")

trimTrailingWhitespace()
indentWithSpaces()
endWithNewline()

removeUnusedImports()
importOrder()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright OpenSearch Contributors.
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.data.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration;

@SpringBootApplication(exclude = ElasticsearchDataAutoConfiguration.class)
public class MarketplaceApplication {
public static void main(String[] args) {
SpringApplication.run(MarketplaceConfiguration.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright OpenSearch Contributors.
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.data.example;

import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.ssl.SSLContextBuilder;
import org.opensearch.client.RestClientBuilder;
import org.opensearch.data.example.repository.MarketplaceRepository;
import org.opensearch.spring.boot.autoconfigure.RestClientBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

@Configuration
@EnableElasticsearchRepositories(basePackageClasses = MarketplaceRepository.class)
@ComponentScan(basePackageClasses = MarketplaceConfiguration.class)
public class MarketplaceConfiguration {
/**
* Allow to connect to the OpenSearch instance which uses self-signed certificates
*/
@Bean
RestClientBuilderCustomizer customizer() {
return new RestClientBuilderCustomizer() {
@Override
public void customize(HttpAsyncClientBuilder builder) {
try {
builder.setSSLContext(new SSLContextBuilder()
.loadTrustMaterial(null, new TrustSelfSignedStrategy())
.build());
} catch (final KeyManagementException | NoSuchAlgorithmException | KeyStoreException ex) {
throw new RuntimeException("Failed to initialize SSL Context instance", ex);
}
}

@Override
public void customize(RestClientBuilder builder) {
// No additional customizations needed
}
};
}
}
Loading

0 comments on commit b305350

Please sign in to comment.