1. Setup Maven Dependencies
In your pom.xml
:
<dependencies>
<!-- Spring Boot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Data Elasticsearch -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- Lombok (optional, for getters/setters) -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- Spring Boot Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2. Configure Elasticsearch
In application.yml
(or application.properties
):
spring:
elasticsearch:
uris: http://localhost:9200
username: elastic
password: your_password
Make sure Elasticsearch is running at
localhost:9200
. You can download it from Elastic.
3. Create an Entity
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = "products")
public class Product {
@Id
private String id;
private String name;
private String description;
private double price;
}
@Document(indexName = "products")
tells Spring Data this entity maps to theproducts
index in Elasticsearch.
4. Create a Repository
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface ProductRepository extends ElasticsearchRepository<Product, String> {
List<Product> findByName(String name);
}
- Spring Data Elasticsearch provides CRUD operations automatically.
5. Create a Service
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ProductService {
private final ProductRepository repository;
public ProductService(ProductRepository repository) {
this.repository = repository;
}
public Product save(Product product) {
return repository.save(product);
}
public List<Product> findAll() {
return (List<Product>) repository.findAll();
}
public List<Product> findByName(String name) {
return repository.findByName(name);
}
public void delete(String id) {
repository.deleteById(id);
}
}
6. Create a REST Controller
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/products")
public class ProductController {
private final ProductService service;
public ProductController(ProductService service) {
this.service = service;
}
@PostMapping
public Product create(@RequestBody Product product) {
return service.save(product);
}
@GetMapping
public List<Product> getAll() {
return service.findAll();
}
@GetMapping("/search")
public List<Product> searchByName(@RequestParam String name) {
return service.findByName(name);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable String id) {
service.delete(id);
}
}
7. Run the Application
- Run
SpringBootApplication
as usual:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ElasticsearchDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ElasticsearchDemoApplication.class, args);
}
}
- Test endpoints using Postman or curl:
# Add product
POST http://localhost:8080/products
Body:
{
"name": "Laptop",
"description": "Gaming Laptop",
"price": 1500
}
# Get all products
GET http://localhost:8080/products
# Search by name
GET http://localhost:8080/products/search?name=Laptop
# Delete product
DELETE http://localhost:8080/products/{id}