This project shows how to build a Spring Boot application using Elasticsearch for storage and retrieval of data.
- Java 17
- Spring Boot 3.1.2
- Spring Data Elasticsearch
- Elasticsearch 7.16.1
The application exposes REST endpoints for CRUD operations on a Book
entity that is persisted in Elasticsearch.
Some key components:
- Spring Data Elasticsearch - Provides integration with Elasticsearch
- BookRepository - Interface extending ElasticsearchRepository for CRUD ops
- BookService - Business logic for creating/updating/deleting Books
- BookController - REST controller exposing endpoints
Download and install Elasticsearch locally, then start it:
docker pull docker.elastic.co/elasticsearch/elasticsearch:7.16.1
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 \
-e "discovery.type=single-node" \
docker.elastic.co/elasticsearch/elasticsearch:7.16.1
Access the Elasticsearch REST API at http://localhost:9200
to interact with Elasticsearch and perform various operations.
docker stop elasticsearch
docker rm elasticsearch
Elasticsearch will run on port 9200.
mvn clean install
The app will run on port 8080.
The REST endpoints support CRUD operations for the Book
entity:
curl -X POST "http://localhost:8080/books" \
-H "Content-Type: application/json" \
-d '{"title":"New Book","author":"John Doe","description":"A new book"}'
curl -X GET "http://localhost:8080/books/{id}"
curl -X PUT "http://localhost:8080/books/{id}" \
-H "Content-Type: application/json" \
-d '{"title":"Updated Book","author":"Jane Smith","description":"An updated book"}'
curl -X DELETE "http://localhost:8080/books/{id}"