Backend Development 7 min read

Spring Boot Integration with Elasticsearch 7.6 Using Spring Data Elasticsearch

This tutorial demonstrates how to set up Elasticsearch 7.6 with the IK analyzer, import Spring Data Elasticsearch dependencies, configure connection beans, define entity and repository classes, and implement CRUD, pagination, and highlighted search operations in a Spring Boot application.

IT Architects Alliance
IT Architects Alliance
IT Architects Alliance
Spring Boot Integration with Elasticsearch 7.6 Using Spring Data Elasticsearch

The article begins by requiring an Elasticsearch 7.6 environment with the IK analyzer and provides a reference link for setup.

1. Dependency Import : Add the <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> <version>${version}.RELEASE</version> </dependency> to the Maven pom, ensuring the version matches Elasticsearch and Spring Boot 2.3.

2. Elasticsearch Configuration : Create a configuration class that reads ${elasticSearch.url} from properties and defines a RestHighLevelClient bean using ClientConfiguration.builder().connectedTo(edUrl).build() .

3. Entity Definition : Annotate the entity with @Data and @Document(indexName = "user") , and declare fields with appropriate @Id and @Field annotations, using FieldType.Auto , Keyword , and Text with ik_smart and ik_max_word analyzers.

4. Repository Interface : Extend ElasticsearchRepository<User, Integer> and declare custom query methods such as List<User> findByName(String name) and List<User> findByNameAndInfo(String name, String info) .

5. Controller Implementation : Use @RestController with injected ElasticsearchRestTemplate and the repository. Provide endpoints to save dummy users, retrieve by ID, paginate results, query by name, and query by name & info. The save method manually puts the mapping to enable the IK analyzer.

6. Highlighted Search : Implement a method that builds a BoolQueryBuilder with matchQuery on name and info , creates a NativeSearchQuery with highlight fields, executes the search, extracts highlighted fragments, and returns the enriched user list.

Test results are shown with screenshots of successful data insertion, pagination, and highlighted query output.

backendJavaElasticsearchSpring BootsearchSpring Data
IT Architects Alliance
Written by

IT Architects Alliance

Discussion and exchange on system, internet, large‑scale distributed, high‑availability, and high‑performance architectures, as well as big data, machine learning, AI, and architecture adjustments with internet technologies. Includes real‑world large‑scale architecture case studies. Open to architects who have ideas and enjoy sharing.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.