Skip to content

Commit 3eea4a9

Browse files
authored
Add opensearch-java client support (#227)
Signed-off-by: Andriy Redko <[email protected]>
1 parent 3692a1d commit 3eea4a9

File tree

106 files changed

+12071
-22
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+12071
-22
lines changed

README.md

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,18 @@ The Spring Data OpenSearch follows the release model of the Spring Data Elastics
3333

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

36-
At the moment, Spring Data OpenSearch provides the possibility to use the `RestHighLevelCLient` to connect to OpenSearch clusters.
36+
37+
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.
38+
39+
#### Using `RestHighLevelCLient` (default)
40+
41+
By default, the `RestHighLevelCLient` is configured as the means to communicate with OpenSearch clusters.
3742

3843
```xml
3944
<dependency>
4045
<groupId>org.opensearch.client</groupId>
4146
<artifactId>spring-data-opensearch</artifactId>
42-
<version>1.3.0</version>
47+
<version>1.4.0</version>
4348
</dependency>
4449
```
4550

@@ -49,7 +54,7 @@ To use Spring Boot 3.x auto configuration support:
4954
<dependency>
5055
<groupId>org.opensearch.client</groupId>
5156
<artifactId>spring-data-opensearch-starter</artifactId>
52-
<version>1.3.0</version>
57+
<version>1.4.0</version>
5358
</dependency>
5459
```
5560

@@ -59,11 +64,80 @@ To use Spring Boot 3.x auto configuration support for testing:
5964
<dependency>
6065
<groupId>org.opensearch.client</groupId>
6166
<artifactId>spring-data-opensearch-test-autoconfigure</artifactId>
62-
<version>1.3.0</version>
67+
<version>1.4.0</version>
6368
<scope>test</scope>
6469
</dependency>
6570
```
6671

72+
#### Using `OpenSearchClient` (preferred)
73+
74+
To switch over to `OpenSearchClient`, the `opensearch-rest-high-level-client` dependency has to be replaced in favor of `opensearch-java`.
75+
76+
```xml
77+
<dependency>
78+
<groupId>org.opensearch.client</groupId>
79+
<artifactId>spring-data-opensearch</artifactId>
80+
<version>1.4.0</version>
81+
<exclusions>
82+
<exclusion>
83+
<groupId>org.opensearch.client</groupId>
84+
<artifactId>opensearch-rest-high-level-client</artifactId>
85+
</exclusion>
86+
</exclusions>
87+
</dependency>
88+
89+
<dependency>
90+
<groupId>org.opensearch.client</groupId>
91+
<artifactId>opensearch-java</artifactId>
92+
<version>2.10.1</version>
93+
</dependency>
94+
```
95+
96+
To use Spring Boot 3.x auto configuration support:
97+
98+
```xml
99+
<dependency>
100+
<groupId>org.opensearch.client</groupId>
101+
<artifactId>spring-data-opensearch-starter</artifactId>
102+
<version>1.4.0</version>
103+
<exclusions>
104+
<exclusion>
105+
<groupId>org.opensearch.client</groupId>
106+
<artifactId>opensearch-rest-high-level-client</artifactId>
107+
</exclusion>
108+
</exclusions>
109+
</dependency>
110+
111+
<dependency>
112+
<groupId>org.opensearch.client</groupId>
113+
<artifactId>opensearch-java</artifactId>
114+
<version>2.10.1</version>
115+
</dependency>
116+
```
117+
118+
To use Spring Boot 3.x auto configuration support for testing:
119+
120+
```xml
121+
<dependency>
122+
<groupId>org.opensearch.client</groupId>
123+
<artifactId>spring-data-opensearch-test-autoconfigure</artifactId>
124+
<version>1.4.0</version>
125+
<scope>test</scope>
126+
<exclusions>
127+
<exclusion>
128+
<groupId>org.opensearch.client</groupId>
129+
<artifactId>opensearch-rest-high-level-client</artifactId>
130+
</exclusion>
131+
</exclusions>
132+
</dependency>
133+
134+
<dependency>
135+
<groupId>org.opensearch.client</groupId>
136+
<artifactId>opensearch-java</artifactId>
137+
<version>2.10.1</version>
138+
</dependency>
139+
```
140+
67141
## Getting Started
68142

69143
Here is a quick teaser of an application using Spring Data Repositories in Java:
@@ -224,7 +298,7 @@ Add the Apache Maven dependency:
224298
<dependency>
225299
<groupId>org.opensearch.client</groupId>
226300
<artifactId>spring-data-opensearch</artifactId>
227-
<version>1.2.1</version>
301+
<version>1.4.0</version>
228302
</dependency>
229303
```
230304

@@ -251,7 +325,7 @@ Add the Gradle dependency:
251325
```groovy
252326
dependencies {
253327
...
254-
implementation "org.opensearch.client:spring-data-opensearch:1.2.1"
328+
implementation "org.opensearch.client:spring-data-opensearch:1.4.0"
255329
...
256330
}
257331
```

settings.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ dependencyResolutionManagement {
3131

3232
create("opensearchLibs") {
3333
version("opensearch", "2.14.0")
34+
library("java-client", "org.opensearch.client:opensearch-java:2.10.3")
3435
library("client", "org.opensearch.client", "opensearch-rest-client").versionRef("opensearch")
3536
library("high-level-client", "org.opensearch.client", "opensearch-rest-high-level-client").versionRef("opensearch")
3637
library("sniffer", "org.opensearch.client", "opensearch-rest-client-sniffer").versionRef("opensearch")
@@ -43,6 +44,10 @@ dependencyResolutionManagement {
4344
library("databind", "com.fasterxml.jackson.core", "jackson-databind").versionRef("jackson")
4445
}
4546

47+
create("jakarta") {
48+
library("json-bind", "jakarta.json.bind:jakarta.json.bind-api:2.0.0")
49+
}
50+
4651
create("pluginLibs") {
4752
version("spotless", "6.23.1")
4853
version("editorconfig", "0.0.3")
@@ -78,3 +83,4 @@ include("spring-data-opensearch")
7883
include("spring-data-opensearch-starter")
7984
include("spring-data-opensearch-test-autoconfigure")
8085
include("spring-data-opensearch-examples:spring-boot-gradle")
86+
include("spring-data-opensearch-examples:spring-boot-java-client-gradle")

spring-data-opensearch-examples/spring-boot-gradle/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This sample project demonstrates the usage of the [Spring Data OpenSearch](https
66
1. The easiest way to get [OpenSearch](https://opensearch.org) service up and running is by using [Docker](https://www.docker.com/):
77

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

1212
2. Build and run the project using [Gradle](https://gradle.org/):
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Spring Data OpenSearch Java Client Spring Boot Example Project
2+
===
3+
4+
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).
5+
6+
1. The easiest way to get [OpenSearch](https://opensearch.org) service up and running is by using [Docker](https://www.docker.com/):
7+
8+
```shell
9+
docker run -p 9200:9200 -e "discovery.type=single-node" opensearchproject/opensearch:2.11.1
10+
```
11+
12+
2. Build and run the project using [Gradle](https://gradle.org/):
13+
14+
```shell
15+
./gradlew :spring-data-opensearch-examples:spring-boot-java-client-gradle:bootRun
16+
```
17+
18+
3. Exercise the REST endpoint available at: `http://localhost:8080/marketplace`
19+
20+
- Fetch all products: `curl 'http://localhost:8080/marketplace/search'`
21+
- Search products by name: `curl 'http://localhost:8080/marketplace/search?name=pillow'`
22+
- Search products by name and price greater than: `curl 'http://localhost:8080/marketplace/search?name=pillow&price=35.0'`
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright OpenSearch Contributors.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
plugins {
7+
alias(springLibs.plugins.spring.boot)
8+
alias(pluginLibs.plugins.spotless)
9+
alias(pluginLibs.plugins.editorconfig)
10+
id("java-conventions")
11+
}
12+
13+
buildscript {
14+
dependencies {
15+
classpath(pluginLibs.editorconfig)
16+
classpath(pluginLibs.spotless)
17+
}
18+
}
19+
20+
dependencies {
21+
api(project(":spring-data-opensearch")) {
22+
exclude("org.opensearch.client", "opensearch-rest-high-level-client")
23+
}
24+
api(project(":spring-data-opensearch-starter")) {
25+
exclude("org.opensearch.client", "opensearch-rest-high-level-client")
26+
}
27+
implementation(springLibs.boot.web)
28+
implementation(jacksonLibs.core)
29+
implementation(jacksonLibs.databind)
30+
implementation(opensearchLibs.client)
31+
implementation(opensearchLibs.java.client)
32+
testImplementation(springLibs.test)
33+
testImplementation(springLibs.boot.test)
34+
testImplementation(springLibs.boot.test.autoconfigure)
35+
testImplementation(opensearchLibs.testcontainers)
36+
testImplementation(project(":spring-data-opensearch-test-autoconfigure")) {
37+
exclude("org.opensearch.client", "opensearch-rest-high-level-client")
38+
}
39+
40+
constraints {
41+
implementation("ch.qos.logback:logback-classic") {
42+
version {
43+
require("1.4.12")
44+
}
45+
because("Fixes CVE-2023-6378")
46+
}
47+
}
48+
}
49+
50+
description = "Spring Data OpenSearch Spring Boot Example Project"
51+
52+
spotless {
53+
java {
54+
target("src/main/java/**/*.java", "src/test/java/org/opensearch/**/*.java")
55+
56+
trimTrailingWhitespace()
57+
indentWithSpaces()
58+
endWithNewline()
59+
60+
removeUnusedImports()
61+
importOrder()
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright OpenSearch Contributors.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.data.example;
7+
8+
import org.springframework.boot.SpringApplication;
9+
import org.springframework.boot.autoconfigure.SpringBootApplication;
10+
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration;
11+
12+
@SpringBootApplication(exclude = ElasticsearchDataAutoConfiguration.class)
13+
public class MarketplaceApplication {
14+
public static void main(String[] args) {
15+
SpringApplication.run(MarketplaceConfiguration.class, args);
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright OpenSearch Contributors.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.data.example;
7+
8+
import java.security.KeyManagementException;
9+
import java.security.KeyStoreException;
10+
import java.security.NoSuchAlgorithmException;
11+
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
12+
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
13+
import org.apache.http.ssl.SSLContextBuilder;
14+
import org.opensearch.client.RestClientBuilder;
15+
import org.opensearch.data.example.repository.MarketplaceRepository;
16+
import org.opensearch.spring.boot.autoconfigure.RestClientBuilderCustomizer;
17+
import org.springframework.context.annotation.Bean;
18+
import org.springframework.context.annotation.ComponentScan;
19+
import org.springframework.context.annotation.Configuration;
20+
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
21+
22+
@Configuration
23+
@EnableElasticsearchRepositories(basePackageClasses = MarketplaceRepository.class)
24+
@ComponentScan(basePackageClasses = MarketplaceConfiguration.class)
25+
public class MarketplaceConfiguration {
26+
/**
27+
* Allow to connect to the OpenSearch instance which uses self-signed certificates
28+
*/
29+
@Bean
30+
RestClientBuilderCustomizer customizer() {
31+
return new RestClientBuilderCustomizer() {
32+
@Override
33+
public void customize(HttpAsyncClientBuilder builder) {
34+
try {
35+
builder.setSSLContext(new SSLContextBuilder()
36+
.loadTrustMaterial(null, new TrustSelfSignedStrategy())
37+
.build());
38+
} catch (final KeyManagementException | NoSuchAlgorithmException | KeyStoreException ex) {
39+
throw new RuntimeException("Failed to initialize SSL Context instance", ex);
40+
}
41+
}
42+
43+
@Override
44+
public void customize(RestClientBuilder builder) {
45+
// No additional customizations needed
46+
}
47+
};
48+
}
49+
}

0 commit comments

Comments
 (0)