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 8, 2024
1 parent b8d2739 commit 94eece3
Show file tree
Hide file tree
Showing 105 changed files with 11,991 additions and 16 deletions.
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.13.0")
library("java-client", "org.opensearch.client:opensearch-java:2.10.1")
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-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
}
};
}
}
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 java.util.List;
import org.opensearch.data.example.model.Product;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

/**
* See please https://github.com/spring-projects/spring-data-elasticsearch/blob/main/src/main/asciidoc/reference/elasticsearch-repository-queries.adoc
*/
@Repository
public interface MarketplaceRepository extends ElasticsearchRepository<Product, String> {
List<Product> findByNameLikeAndPriceGreaterThan(String name, BigDecimal price);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright OpenSearch Contributors.
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.data.example.rest;

import java.math.BigDecimal;
import java.util.List;
import org.opensearch.data.example.model.Product;
import org.opensearch.data.example.repository.MarketplaceRepository;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/marketplace")
public class MarketplaceRestController {
private final MarketplaceRepository repository;

public MarketplaceRestController(MarketplaceRepository repository) {
this.repository = repository;
}

@GetMapping(value = "/search", produces = MediaType.APPLICATION_JSON_VALUE)
public List<Product> search(
@RequestParam(value = "name", required = false, defaultValue = "") String name,
@RequestParam(value = "price", required = false, defaultValue = "0.0") BigDecimal price) {
return repository.findByNameLikeAndPriceGreaterThan(name, price);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright OpenSearch Contributors.
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.data.example.service;

import java.math.BigDecimal;
import org.opensearch.data.example.model.Product;
import org.opensearch.data.example.repository.MarketplaceRepository;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

@Service
public class MarketplaceInitializer implements InitializingBean {
private final MarketplaceRepository repository;

public MarketplaceInitializer(MarketplaceRepository repository) {
this.repository = repository;
}

@Override
public void afterPropertiesSet() throws Exception {
repository.save(new Product(
"1",
"Utopia Bedding Bed Pillows",
new BigDecimal(39.99),
2,
"These professionally finished pillows, with high thread counts, provide great comfort against your skin along with added durability "
+ "that easily resists wear and tear to ensure a finished look for your bedroom.",
"Utopia Bedding"));

repository.save(new Product(
"2",
"Echo Dot Smart speaker",
new BigDecimal(34.99),
10,
"Our most popular smart speaker with a fabric design. It is our most compact smart speaker that fits perfectly into small spaces.",
"Amazon"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#
# Copyright OpenSearch Contributors.
# SPDX-License-Identifier: Apache-2.0
#

opensearch:
uris: https://localhost:9200
username: admin
password: admin

spring:
jackson:
serialization:
INDENT_OUTPUT: true
Loading

0 comments on commit 94eece3

Please sign in to comment.