Skip to content

[FEATURE] Reactive version of the clients #447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,17 @@ public class MyService {
}
```

Or, using the reactive Spring Data Repositories:

```java
public interface PersonRepository extends ReactiveCrudRepository<Person, Long> {

Flux<Person> findByLastname(String lastname);

Flux<Person> findByFirstnameLike(String firstname);
}
```

### Using the OpenSearch RestClient

Spring Data OpenSearch operates upon an OpenSearch client that is connected to a single OpenSearch node or a cluster. Although the OpenSearch Client can be used directly to work with the cluster, applications using Spring Data Elasticsearch normally use the higher level abstractions of `ElasticsearchOperations` and repositories (please consult [official Spring Data Elasticsearch documentation](https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/)). Use the builder to provide cluster addresses, set default `HttpHeaders` or enable SSL.
Expand Down Expand Up @@ -294,6 +305,16 @@ public class MarketplaceRepositoryIntegrationTests {
}
```

Or, using the reactive Spring Data Repositories:

```java
@DataOpenSearchTest
@EnableReactiveElasticsearchRepositories
public class MarketplaceRepositoryIntegrationTests {
...
}
```

### Spring Boot Service Connection

#### Testcontainers Service Connections
Expand Down
4 changes: 4 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ dependencyResolutionManagement {
library("data-commons", "org.springframework.data:spring-data-commons:3.5.0")
library("data-elasticsearch", "org.springframework.data:spring-data-elasticsearch:5.5.0")
library("web", "org.springframework", "spring-web").versionRef("spring")
library("webflux", "org.springframework", "spring-webflux").versionRef("spring")
library("context", "org.springframework", "spring-context").versionRef("spring")
library("tx", "org.springframework", "spring-tx").versionRef("spring")
library("test", "org.springframework", "spring-test").versionRef("spring")
library("boot-web", "org.springframework.boot", "spring-boot-starter-web").versionRef("spring-boot")
library("boot-webflux", "org.springframework.boot", "spring-boot-starter-webflux").versionRef("spring-boot")
library("boot-autoconfigure", "org.springframework.boot", "spring-boot-autoconfigure").versionRef("spring-boot")
library("boot-docker-compose", "org.springframework.boot", "spring-boot-docker-compose").versionRef("spring-boot")
library("boot-test", "org.springframework.boot", "spring-boot-test").versionRef("spring-boot")
library("boot-test-autoconfigure", "org.springframework.boot", "spring-boot-test-autoconfigure").versionRef("spring-boot")
library("boot-testcontainers", "org.springframework.boot", "spring-boot-testcontainers").versionRef("spring-boot")
library("projectreactor", "io.projectreactor:reactor-test:3.7.5")
plugin("spring-boot", "org.springframework.boot").versionRef("spring-boot")
}

Expand Down Expand Up @@ -90,3 +93,4 @@ include("spring-data-opensearch-test-autoconfigure")
include("spring-data-opensearch-testcontainers")
include("spring-data-opensearch-examples:spring-boot-gradle")
include("spring-data-opensearch-examples:spring-boot-java-client-gradle")
include("spring-data-opensearch-examples:spring-boot-reactive-client-gradle")
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ docker run -p 9200:9200 -e "discovery.type=single-node" -e OPENSEARCH_INITIAL_AD
2. Build and run the project using [Gradle](https://gradle.org/):

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

3. Exercise the REST endpoint available at: `http://localhost:8080/marketplace`
Expand All @@ -34,7 +34,7 @@ docker run -p 9200:9200 -e "discovery.type=single-node" -e OPENSEARCH_INITIAL_AD
1. Build and run the project using [Gradle](https://gradle.org/):

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

2. Exercise the REST endpoint available at: `http://localhost:8080/marketplace`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ dependencies {
}
}

description = "Spring Data OpenSearch Spring Boot Example Project"
description = "Spring Data OpenSearch Java Client Spring Boot Example Project"

spotless {
java {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Spring Data OpenSearch Reactive 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 reactive Spring Boot web application. The application assumes that there is an [OpenSearch](https://opensearch.org) service up and running on the local machine.
This example uses the [`opensearch-java` client](https://opensearch.org/docs/latest/clients/java/).

## Pre-requisites

* [Docker](https://www.docker.com/)
* Java 17

## Using Docker CLI

1. Start [OpenSearch](https://opensearch.org) using

```shell
docker run -p 9200:9200 -e "discovery.type=single-node" -e OPENSEARCH_INITIAL_ADMIN_PASSWORD=<strong-password> opensearchproject/opensearch:2.15.0
```

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

```shell
./gradlew :spring-data-opensearch-examples:spring-boot-reactive-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'`

## Using Spring Boot Testcontainers integration

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

```shell
./gradlew :spring-data-opensearch-examples:spring-boot-reactive-client-gradle:bootTestRun
```

2. 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,68 @@
/*
* 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.webflux)
implementation(jacksonLibs.core)
implementation(jacksonLibs.databind)
implementation(opensearchLibs.client)
implementation(opensearchLibs.java.client)
testImplementation(springLibs.test)
testImplementation(springLibs.projectreactor)
testImplementation(springLibs.boot.test)
testImplementation(springLibs.boot.test.autoconfigure)
testImplementation(springLibs.boot.testcontainers)
testImplementation(opensearchLibs.testcontainers)
testImplementation(project(":spring-data-opensearch-testcontainers")) {
exclude("org.opensearch.client", "opensearch-rest-high-level-client")
}
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 Reactive Client 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.ReactiveMarketplaceRepository;
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.EnableReactiveElasticsearchRepositories;

@Configuration
@EnableReactiveElasticsearchRepositories(basePackageClasses = ReactiveMarketplaceRepository.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
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright OpenSearch Contributors.
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.data.example.model;

import java.math.BigDecimal;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

@Document(indexName = "marketplace")
public class Product {
@Id
private String id;

@Field(type = FieldType.Text, name = "name")
private String name;

@Field(type = FieldType.Double, name = "price")
private BigDecimal price;

@Field(type = FieldType.Integer, name = "quantity")
private Integer quantity;

@Field(type = FieldType.Text, name = "description")
private String description;

@Field(type = FieldType.Keyword, name = "vendor")
private String vendor;

public Product() {}

public Product(String id, String name, BigDecimal price, Integer quantity, String description, String vendor) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
this.description = description;
this.vendor = vendor;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getQuantity() {
return quantity;
}

public void setQuantity(Integer quantity) {
this.quantity = quantity;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getVendor() {
return vendor;
}

public void setVendor(String vendor) {
this.vendor = vendor;
}

public BigDecimal getPrice() {
return price;
}

public void setPrice(BigDecimal price) {
this.price = price;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright OpenSearch Contributors.
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.data.example.repository;

import java.math.BigDecimal;
import org.opensearch.data.example.model.Product;
import org.springframework.data.elasticsearch.repository.ReactiveElasticsearchRepository;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;

/**
* See please https://github.com/spring-projects/spring-data-elasticsearch/blob/main/src/main/asciidoc/reference/elasticsearch-repository-queries.adoc
*/
@Repository
public interface ReactiveMarketplaceRepository extends ReactiveElasticsearchRepository<Product, String> {
Flux<Product> findByNameLikeAndPriceGreaterThan(String name, BigDecimal price);
}
Loading
Loading