Skip to content
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
2 changes: 2 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github: kasramp
custom: https://paypal.me/Madadipouya
25 changes: 25 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Java CI

on: [push]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Cache
uses: actions/cache@v3
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
distribution: zulu
java-version: 17
- name: Run tests
run: mvn clean verify
- name: Build with Maven
run: mvn -B package --file pom.xml
33 changes: 13 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,58 +1,51 @@
# Spring Data Elasticsearch Example
# Spring Data Elasticsearch Example with Spring Boot 3 and ElasticSearch 8

## Introduction

This example demonstrates how to use Spring Data Elasticsearch to do simple CRUD operation.
This example demonstrates how to use Spring Data Elasticsearch to do simple CRUD operations.

You can find the tutorial about this example at the below link:
You can find the tutorial about this example at this link: [Getting started with Spring Data Elasticsearch](https://www.geekyhacker.com/getting-started-with-spring-data-elasticsearch/)

[https://geekyhacker.com/2019/05/01/getting-started-with-spring-data-elasticsearch/](https://geekyhacker.com/2019/05/01/getting-started-with-spring-data-elasticsearch/)
For this example, we created a Book controller that allows doing the following operations with Elasticsearch:

For this example, a Book controller created that allows to do the following operations with Elasticsearch:

- Get list of all books
- Get the list of all books
- Create a book
- Update a book by Id
- Delete a book by Id
- Search for a book by ISBN
- Fuzzy search for books by author and title


## How to run

The first thing to do is to start Elasticsearch. For that you can use the `docker-compose` file in this project
and run it like this:
The first thing to do is to start Elasticsearch. For that, you can use the `docker-compose` file in this project and run it like this:

```bash
$ docker-compose -f docker-compose up -d
```

It brings Elasticsearch up on a single node cluster with the cluser name `elasticsearch`.
It brings Elasticsearch up on a single node cluster with the cluster name `elasticsearch`.

Then you can run the application like below:

```bash
$ ./mvnw spring-boot:run
```

If your Elasticsearch URI is not `localhost` and/or the cluster name is different simply override one or both of the following environment variable:
If your Elasticsearch URI is not `localhost` is different simply override the following environment variable:

- `ES_URI`
- `ES_CLUSTER_NAME`

Once everything is up and running open the browser and go to [http://localhost:8080](http://localhost:8080). You should see Swagger to interact with.

## Run testcontainers tests

The integration tests written relying on [testcontainers](https://www.testcontainers.org/) to spin up Elasticsearch on the spot to run tests against.
## Run Testcontainers tests

To know more about container read this tutorial:
[https://geekyhacker.com/2019/05/08/integration-test-with-testcontainers-in-java/](https://geekyhacker.com/2019/05/08/integration-test-with-testcontainers-in-java/)
The integration tests are written relying on [Testcontainers](https://www.testcontainers.org/) to spin up Elasticsearch on the spot and run tests against it.
To know more about container testing read this tutorial: [Integration test with Testcontainers in Java](https://www.geekyhacker.com/integration-test-with-testcontainers-in-java/)

To run them just execute below command:
To run the integration test (using Testcontainers) just run the below command:

```bash
$ mvn clean verify
```

Make sure you have your docker running.
Make sure you have your docker running.
7 changes: 4 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
elasticsearch:
image: elasticsearch:6.4.3
image: elasticsearch:8.7.0
container_name: elasticsearch
ports:
- "9300:9300"
- "9200:9200"
environment:
- discovery.type=single-node
- cluster.name=elasticsearch
- cluster.name=elasticsearch
# Since ES 8, SSL is on by default, disabling on local
- xpack.security.enabled=false
48 changes: 18 additions & 30 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
<version>3.1.0</version>
</parent>
<groupId>com.madadipouya.elasticsearch.springdata.example</groupId>
<artifactId>elasticsearch-springdata</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.0.2</version>
<name>Elasticsearch SpringData</name>
<description>Elasticsearch SpringData</description>

<properties>
<java.version>11</java.version>
<java.version>17</java.version>
<mockito.version>5.3.1</mockito.version>
<testcontainers.version>1.18.3</testcontainers.version>
</properties>

<dependencies>
Expand All @@ -32,64 +33,52 @@
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.2</version>
<version>5.9.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.4</version>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>2.23.4</version>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.17.3</version>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.17.2</version>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>elasticsearch</artifactId>
<version>1.17.3</version>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand All @@ -99,12 +88,12 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<version>3.1.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M3</version>
<version>3.1.2</version>
<configuration>
<skipTests>false</skipTests>
<rerunFailingTestsCount>3</rerunFailingTestsCount>
Expand All @@ -123,5 +112,4 @@
</plugin>
</plugins>
</build>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,32 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import static springfox.documentation.builders.PathSelectors.regex;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(regex("/v1/.*"))
.build();
public OpenAPI apiInfo() {
return new OpenAPI().info(new Info().title("Spring Data Elasticsearch example")
.description("Spring Data Elasticsearch example with Testcontainers")
.version("v0.0.2")
.contact(getContactDetails())
.license(getLicenseDetails()));
}

private Contact getContactDetails() {
return new Contact().name("Kasra Madadipouya")
.email("kasra@madadipouya.com")
.url("https://geekyhacker.com");
}

private License getLicenseDetails() {
return new License().name("GNU General Public License v3.0")
.url("https://github.com/kasramp/Spring-Data-ElasticSearch-Example/blob/master/LICENSE");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Positive;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Positive;
import java.util.List;

@RestController
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import org.springframework.web.servlet.ModelAndView;

@RestController
public class redirect {
public class Redirect {

@GetMapping(value = "/")
public ModelAndView redirectToDocPage() {
return new ModelAndView("redirect:/swagger-ui.html");
return new ModelAndView("redirect:/swagger-ui/index.html");
}

@GetMapping(value = "/apidocs")
public ModelAndView redirectToApiPage() {
return new ModelAndView("redirect:/swagger-ui.html");
return new ModelAndView("redirect:/swagger-ui/index.html");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.madadipouya.elasticsearch.springdata.example.validator.PublicationYearValidator;

import javax.validation.Constraint;
import javax.validation.Payload;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

@Document(indexName = "books", type = "book")
@Document(indexName = "books")
public class Book {

@Id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.madadipouya.elasticsearch.springdata.example.repository;

import com.madadipouya.elasticsearch.springdata.example.model.Book;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

Expand Down
Loading