Backend Development 9 min read

How to Integrate Elasticsearch 7.8 with Spring Boot 2.4: A Step-by-Step Guide

This article explains why Elasticsearch is fast, outlines its key features, and provides a complete Spring Boot 2.4 tutorial—including dependencies, configuration, data model, repository usage, and test code—for building a distributed search service.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
How to Integrate Elasticsearch 7.8 with Spring Boot 2.4: A Step-by-Step Guide

Introduction

Elasticsearch is a distributed search engine built on Lucene, offering RESTful APIs, horizontal scalability and is often used for website search, log analysis, or even as a NoSQL store.

Why use Elasticsearch?

Distributed architecture – can scale to hundreds or thousands of nodes and handle petabyte‑scale data.

Full‑text search capabilities inherited from Lucene.

Near‑real‑time indexing – documents become searchable within about a second.

Rich built‑in features such as aggregations and index lifecycle management.

Simplified data ingestion via Beats, Logstash and visualization with Kibana.

What makes Elasticsearch fast?

Its speed comes from the distributed architecture, near‑real‑time indexing, inverted‑index data structure and optimized query algorithms.

Integrating Elasticsearch with Spring Boot

Dependencies

<code>&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  &lt;artifactId&gt;spring-boot-starter-data-elasticsearch&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
  &lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
</code>

Configuration

<code>spring:
  elasticsearch:
    rest:
      uris:
      - http://localhost:9201
logging:
  level:
    com.pack: debug
    org.springframework.data.elasticsearch.core: debug
</code>

Data model

<code>@Document(createIndex = true, indexName = "products", shards = 3, replicas = 1)
public class Product {
  @Id
  private Long id;
  @Field(analyzer = "ik_max_word", type = FieldType.Text)
  private String title;
  @Field(type = FieldType.Keyword)
  private String category;
  @Field(type = FieldType.Double)
  private Double price;
  @Field(type = FieldType.Keyword, index = false)
  private String images;
  @Override
  public String toString() {
    return "Product [id=" + id + ", title=" + title + ", category=" + category + ", price=" + price + ", images=" + images + "]";
  }
}
</code>

Repository

Extend ElasticsearchRepository to get CRUD methods and query derivation.

<code>public interface ProductRepository extends ElasticsearchRepository&lt;Product, Long&gt; {
}
</code>

Supported return types include List&lt;T&gt; , Stream&lt;T&gt; , SearchHits&lt;T&gt; , List&lt;SearchHit&lt;T&gt;&gt; , Stream&lt;SearchHit&lt;T&gt;&gt; , SearchPage&lt;T&gt; . Custom queries can be defined with @Query and highlights with @Highlight .

Testing

<code>@Resource
private ProductRepository productRepository;
@Resource
private ElasticsearchRestTemplate elasticTemplate;

@Test
public void testCreate() {
  Product product = new Product();
  product.setId(3L);
  product.setCategory("配件");
  product.setPrice(299.5d);
  product.setImages("http://www.pack.com/memory.jpg");
  product.setTitle("很牛逼的内存条");
  productRepository.save(product);
}

@Test
public void testQuery() {
  Product product = productRepository.findById(1L).orElse(null);
  System.out.println(product);
}

@Test
public void testFindAll() {
  Pageable pageable = PageRequest.of(1, 2);
  Page&lt;Product&gt; page = productRepository.findAll(pageable);
  System.out.println(page.getTotalPages() + "\n" + page.getContent());
}

@Test
public void testTermSearch() {
  for (Product p : productRepository.findByTitle("Java从入门到精通")) {
    System.out.println(p);
  }
}

@Test
public void testFindByTitle() {
  Pageable pageable = PageRequest.of(0, 2);
  Page&lt;Product&gt; page = productRepository.findByTitle("Java", pageable);
  System.out.println(page.getTotalPages() + "\n" + page.getContent());
}

@Test
public void testFindByCategory() {
  Pageable pageable = PageRequest.of(0, 2);
  Page&lt;Product&gt; page = productRepository.findByCategory("书籍", pageable);
  System.out.println(page.getTotalPages() + "\n" + page.getContent());
}

@Test
public void testCriteriaQuery() {
  Criteria criteria = new Criteria("price").greaterThan(50).lessThan(80);
  Query query = new CriteriaQuery(criteria);
  SearchHits&lt;Product&gt; hits = elasticTemplate.search(query, Product.class, IndexCoordinates.of("products"));
  for (SearchHit&lt;Product&gt; hit : hits) {
    System.out.println(hit);
  }
}

@Test
public void testStringQuery() {
  Query query = new StringQuery("{ \"match\": { \"category\": { \"query\": \"配件\" } } }");
  SearchHits&lt;Product&gt; hits = elasticTemplate.search(query, Product.class);
  for (SearchHit&lt;Product&gt; hit : hits) {
    System.out.println(hit);
  }
}

@Test
public void testStringQueryFuzzy() {
  Query query = new StringQuery("{ \"fuzzy\":{\"title\":{\"value\":\"Java\"}} }");
  HighlightBuilder highBuilder = new HighlightBuilder().preTags("<font color='red'>").postTags("</font>").field("title");
  HighlightQuery highlightQuery = new HighlightQuery(highBuilder);
  query.setHighlightQuery(highlightQuery);
  SearchHits&lt;Product&gt; hits = elasticTemplate.search(query, Product.class);
  for (SearchHit&lt;Product&gt; hit : hits) {
    System.out.println(hit + "\n" + hit.getHighlightField("title"));
  }
}
</code>

When the application starts, the index is created automatically. Tools like the Chrome plugin “ElasticSearch Head” can be used to view cluster status and index information.

Done!

Javaintegrationsearch engineBackend DevelopmentElasticsearchSpring Boot
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

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.