-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Andriy Redko <andriy.redko@aiven.io>
- Loading branch information
Showing
105 changed files
with
11,991 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
spring-data-opensearch-examples/spring-boot-java-client-gradle/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'` |
63 changes: 63 additions & 0 deletions
63
spring-data-opensearch-examples/spring-boot-java-client-gradle/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...-java-client-gradle/src/main/java/org/opensearch/data/example/MarketplaceApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...ava-client-gradle/src/main/java/org/opensearch/data/example/MarketplaceConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}; | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
...ring-boot-java-client-gradle/src/main/java/org/opensearch/data/example/model/Product.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...nt-gradle/src/main/java/org/opensearch/data/example/repository/MarketplaceRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
33 changes: 33 additions & 0 deletions
33
...ient-gradle/src/main/java/org/opensearch/data/example/rest/MarketplaceRestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...ient-gradle/src/main/java/org/opensearch/data/example/service/MarketplaceInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...ata-opensearch-examples/spring-boot-java-client-gradle/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.