Backend Development 9 min read

Integrating Spring Data Elasticsearch Repositories with Spring Boot and Docker

This tutorial explains how to set up a single‑node Elasticsearch instance with Docker, integrate it into a Spring Boot application using Spring Data Elasticsearch Repositories, configure the necessary dependencies and settings, and perform basic CRUD operations and visualization with Kibana.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Integrating Spring Data Elasticsearch Repositories with Spring Boot and Docker

1. Introduction to Elasticsearch

Elasticsearch is a Java‑based search engine built on Lucene, offering a distributed, multi‑user full‑text search engine with a RESTful API. It stores, searches, and analyzes large volumes of data efficiently, especially useful for semi‑structured natural‑language data.

Four ways to integrate Elasticsearch:

REST Client

Jest

Spring Data

Spring Data Elasticsearch Repositories

This article mainly introduces how to use Spring Data Elasticsearch Repositories, which are tightly integrated with Spring Boot and require only simple configuration to work out of the box.

2. Running Elasticsearch

For quick testing we deploy a single‑node Elasticsearch instance using a Docker image, exposing ports 9200 and 9300.

Pull image:

docker pull elasticsearch:7.4.2

View images:

docker images

Create host mount directories:

mkdir -p /mydata/elasticsearch/config/
mkdir -p /mydata/elasticsearch/data/
echo "http.host: 0.0.0.0" >> /mydata/elasticsearch/config/elasticsearch.yml

Run container:

docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \
-e ES_JAVA_OPS="-Xms256m -Xmx256m" \
-v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \
-v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
-d elasticsearch:7.4.2

Key parameters:

-p 9200:9200 maps the container's 9200 port to the host.
--name elasticsearch gives the container a name.
-v mounts data and configuration directories to the host.
-d runs the container in daemon mode.
-e ES_JAVA_OPS sets JVM memory limits for testing.

View container processes:

docker ps -a

3. Project Integration

Following Spring Boot conventions, no additional beans are required to enable Elasticsearch support. Add the following dependencies to pom.xml :

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
The parent spring-boot-starter-parent version 2.2.1.RELEASE forces the same version for both starters, while the underlying spring-data-elasticsearch is version 3.2.1.RELEASE.

Configure Elasticsearch parameters in application.yml :

spring:
  application:
    name: spring-boot-bulking-elasticsearch
  elasticsearch:
    rest:
      uris: 127.0.0.1:9200
      read-timeout: 5s

Spring Boot provides three ways to operate ES data:

Implement ElasticsearchRepository interface

Use ElasticsearchRestTemplate

Use ElasticsearchOperations

Define an entity and map it to an index:

@Document(indexName = "order", type = "biz1", shards = 2)
public class OrderModel {

    @Id
    private Long orderId;
    private Double amount;
    private Long buyerUid;
    private String shippingAddress;
}

Common annotations:

@Document maps the class to an Elasticsearch document.
@Id marks the document identifier, analogous to a primary key in a relational table.
@Field defines field type, indexing, and storage options.

Create a repository interface extending ElasticsearchRepository :

public interface OrderRepository extends ElasticsearchRepository<OrderModel, Long> {
}

The ElasticsearchRepository interface, provided by Spring Data Elasticsearch, offers common CRUD and query methods.

Unit tests to verify functionality:

@Test
public void test1() {
    OrderModel orderModel = OrderModel.builder()
            .orderId(1L)
            .amount(25.5)
            .buyerUid(13201L)
            .shippingAddress("Shanghai")
            .build();
    orderModel = orderRepository.save(orderModel);
    System.out.println(orderModel);
}

@Test
public void test2() {
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder()
            .withQuery(boolQueryBuilder);
    List
orderDocumentList = orderRepository.search(searchQueryBuilder.build()).getContent();
    System.out.println(JSON.toJSONString(orderDocumentList));
}
Using OrderRepository we can operate on OrderModel data in Elasticsearch; the index is created automatically.

4. Kibana Visualization Console

Install Kibana, start it with ./kibana , and open http://localhost:5601 to view the indices created by the unit tests.

Screenshot:

5. Project Source Code

https://github.com/aalansehaiyang/spring-boot-bulking
Module: spring-boot-bulking-elasticsearch
JavadockerSearch EngineElasticsearchSpring BootSpring Data
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

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.