Skip to content

Commit aa81e91

Browse files
committed
issues/2334 | sample spring elastic app
* added readme file Signed-off-by: Snoopy009 <[email protected]>
1 parent 6d35d65 commit aa81e91

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed

spring-elastic/README.md

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# Spring Boot Elasticsearch Example
2+
3+
This project is a simple Spring Boot application that demonstrates how to integrate with Elasticsearch using the Java High-Level REST Client. It provides REST endpoints to create and search books in an Elasticsearch index.
4+
5+
## Prerequisites
6+
7+
- **Java Development Kit (JDK) 8 or higher**
8+
- **Maven** for building the project
9+
- **Docker** for running Elasticsearch
10+
- An API client like **Postman** or **cURL** for testing endpoints
11+
12+
## Setting Up Elasticsearch with Docker
13+
14+
To run Elasticsearch, we'll use Docker to pull and run the official Elasticsearch image.
15+
16+
Execute the following command in your terminal:
17+
18+
```bash
19+
docker run -d \
20+
-p 9200:9200 \
21+
-p 9300:9300 \
22+
--name elasticsearch \
23+
-e "discovery.type=single-node" \
24+
-e "xpack.security.enabled=false" \
25+
docker.elastic.co/elasticsearch/elasticsearch:8.15.2
26+
```
27+
28+
## Explanation of the Docker Command:
29+
30+
- `-d`: Run the container in detached mode (in the background).
31+
- `-p 9200:9200`: Map port 9200 of the host to port 9200 of the container (HTTP API).
32+
- `-p 9300:9300`: Map port 9300 of the host to port 9300 of the container (Transport API).
33+
- `--name elasticsearch`: Assign the name "elasticsearch" to the container.
34+
- `-e "discovery.type=single-node"`: Run Elasticsearch in single-node mode.
35+
- `-e "xpack.security.enabled=false"`: Disable X-Pack security features for simplicity. !!! Not recommended for production
36+
- `docker.elastic.co/elasticsearch/elasticsearch:8.15.2`: The Docker image to use.
37+
38+
### Verify Elasticsearch is Running:
39+
40+
After running the Docker command, verify that Elasticsearch is running by accessing [http://localhost:9200](http://localhost:9200) in your browser or using `curl`:
41+
42+
```bash
43+
curl http://localhost:9200
44+
```
45+
46+
You should receive a JSON response with cluster information.
47+
48+
Running the Spring Boot Application
49+
1. Clone the Repository
50+
51+
```bash
52+
git clone https://github.com/yourusername/your-repo-name.git
53+
cd your-repo-name
54+
```
55+
56+
2. Configure Application Properties
57+
Ensure that your application.properties file is correctly set up. It should be located in src/main/resources/ and contain the following properties:
58+
59+
```bash
60+
spring.application.name=spring-elastic
61+
server.port=8081
62+
63+
spring.elasticsearch.url=localhost
64+
spring.elasticsearch.port=9200
65+
66+
# Credentials are optional if security is disabled
67+
spring.elasticsearch.username=
68+
spring.elasticsearch.password=
69+
```
70+
3. Build the Project
71+
Use Maven to build the project:
72+
73+
```bash
74+
mvn clean install
75+
```
76+
77+
4. Run the Application
78+
You can run the application using Maven:
79+
80+
81+
```bash
82+
mvn spring-boot:run
83+
```
84+
85+
Or run the generated JAR file:
86+
87+
```bash
88+
java -jar target/your-app-name.jar
89+
```
90+
The application will start on port 8081.
91+
92+
Project Structure
93+
- Controller
94+
BookController: Handles HTTP requests for creating and searching books.
95+
- Model
96+
Book: Represents the book entity stored in Elasticsearch.
97+
- Client
98+
BookElasticsearchClient: Manages the connection to Elasticsearch.
99+
- Service
100+
BookElasticsearchService: Contains business logic for interacting with Elasticsearch.
101+
Endpoints
102+
1. Create a Book
103+
- URL: /book/create
104+
- Method: POST
105+
- Description: Creates a new book in the Elasticsearch index.
106+
- Request Body:
107+
108+
```bash
109+
{
110+
"id": "1",
111+
"name": "Elasticsearch Basics",
112+
"description": "An introductory guide to Elasticsearch.",
113+
"price": 29.99
114+
}
115+
```
116+
Sample cURL Request:
117+
118+
```bash
119+
curl -X POST \
120+
http://localhost:8081/book/create \
121+
-H 'Content-Type: application/json' \
122+
-d '{
123+
"id": "1",
124+
"name": "Elasticsearch Basics",
125+
"description": "An introductory guide to Elasticsearch.",
126+
"price": 29.99
127+
}'
128+
```
129+
130+
2. Find Books by Name
131+
- URL: /book/find_by_name
132+
- Method: GET
133+
- Description: Searches for books by name in the Elasticsearch index.
134+
- Query Parameter:
135+
- name: The name or partial name of the book to search for.
136+
```bash
137+
curl -X GET "http://localhost:8081/book/find_by_name?name=Elasticsearch"
138+
```
139+
140+
### Conclusion
141+
You have successfully set up an Elasticsearch instance using Docker and run a Spring Boot application that interacts with it. This application demonstrates basic CRUD operations with Elasticsearch, providing a foundation for more complex integrations.
142+
143+
Feel free to extend this application by adding more features like update and delete operations, or by integrating authentication and security features.
144+
145+
Note: In a production environment, you should enable security features in Elasticsearch and handle credentials appropriately.

0 commit comments

Comments
 (0)