Integrating Elasticsearch with Spring Boot for Full-Text Search
This tutorial explains how to integrate Elasticsearch into a Spring Boot application, covering project setup, dependency addition, configuration, entity and repository creation, service and controller implementation, and testing to enable efficient full‑text search for large data sets.
In modern applications, efficient management and fast retrieval of large amounts of data are crucial; Elasticsearch provides a powerful open‑source full‑text search engine.
This article demonstrates how to integrate Elasticsearch with a Spring Boot project to achieve full‑text search capabilities.
Creating a Spring Boot Project
Start a new Spring Boot project using Spring Initializr, selecting the required dependencies.
Adding Elasticsearch Dependency
Include the Spring Data Elasticsearch starter in pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>This brings in Spring Data Elasticsearch for easier usage.
Configuring Elasticsearch Connection
Add connection settings to application.properties:
spring:
data:
elasticsearch:
cluster-nodes: localhost:9200Ensure an Elasticsearch instance is running locally on port 9200.
Creating the Entity Class
Define a simple entity mapped to an Elasticsearch index:
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "documents", type = "document")
public class DocumentEntity {
@Id
private String id;
private String title;
private String content;
// constructors, getters, setters omitted
}Creating the Elasticsearch Repository
Extend ElasticsearchRepository to interact with the index:
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface DocumentRepository extends ElasticsearchRepository<DocumentEntity, String> {
// custom query methods can be added here
}Implementing the Service Layer
Write a service that uses the repository for search and save operations:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DocumentService {
@Autowired
private DocumentRepository documentRepository;
public List<DocumentEntity> searchDocuments(String keyword) {
// example custom method; adjust as needed
return documentRepository.findByTitleOrContent(keyword, keyword);
}
public void saveDocument(DocumentEntity document) {
documentRepository.save(document);
}
}Creating the Controller Layer
Expose HTTP endpoints for searching and adding documents:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/documents")
public class DocumentController {
@Autowired
private DocumentService documentService;
@GetMapping("/search")
public List<DocumentEntity> searchDocuments(@RequestParam String keyword) {
return documentService.searchDocuments(keyword);
}
@PostMapping("/add")
public void addDocument(@RequestBody DocumentEntity document) {
documentService.saveDocument(document);
}
}Testing
Run the Spring Boot application and use tools such as Postman to test the search and add‑document endpoints.
Conclusion
The example shows how to integrate Elasticsearch into a Spring Boot application, enabling efficient full‑text search and improving data management for large datasets. Further enhancements can leverage advanced Elasticsearch features according to specific business needs.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
