Using Elasticsearch with Spring Boot: Setup and Configuration

1. Installation

Elasticsearch's compatibility with Spring Boot versions is critical; ensure you download the correct version based on your project's Spring Boot and Spring Framework versions.

To find the appropriate version, check the versions used in your project and then proceed to download from the official Elastic website:

Download link: https://www.elastic.co/cn/downloads/past-releases/elasticsearch-7-17-3

After downloading and extracting the package, follow the setup instructions available at: https://www.ngui.cc/el/3053538.html?action=onClick

Once installed, verify the service is running correctly by accessing:

http://loclahost:9200/

2. Adding Chinese Analysis Plugin

To enable Chinese text processing, install the IK Analyzer plugin.

Download link: https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v7.17.3

After installation, restart the Elasticsearch service. If new terms need to be added, update the configuration accordingly.

Configuration file example (ik/config/analysis-ik/ik.properties):

<?xml version="1.0" encoding="UTF-8"?>
<properties>
  <comment>IK Analyzer extended configuration</comment>
  <!-- Define custom dictionary path -->
  <entry key="ext_dict">custom.dic</entry>
  <!-- Define custom stop words dictionary -->
  <entry key="ext_stopwords"></entry>
  <!-- Remote dictionary URL -->
  <entry key="remote_ext_dict">words_location</entry>
  <!-- Remote stop words dictionary URL -->
  <entry key="remote_ext_stopwords">words_location</entry>
</properties>

3. Integration in Application

  1. Add required dependencies
<!-- Elasticsearch client dependency -->
<dependency>
  <groupId>org.elasticsearch.client</groupId>
  <artifactId>elasticsearch-rest-high-level-client</artifactId>
  <version>7.17.3</version>
</dependency>

  1. Data retrieval using BoolQueryBuilder's should/must clauses, equivalent to SQL OR/AND conditions

Index Creation (similar to database table definition)

{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 2
  },
  "mappings": {
    "properties": {
      "id": { "type": "long" },
      "projectId": { "type": "text" },
      "title": { "type": "text", "analyzer": "ik_smart" },
      "excerpt": { "type": "text", "analyzer": "ik_max_word" },
      "createTime": {
        "type": "date",
        "store": true,
        "format": "yyyy-MM-dd HH:mm:ss"
      },
      "icon": { "type": "keyword", "index": false },
      "tags": { "type": "keyword", "index": false },
      "module": { "type": "keyword", "index": false }
    }
  }
}

Documant Insertion Example

{
  "id": 6430,
  "projectId": "flash_qg005ewi",
  "title": "Twitter (X) gets approval for cryptocurrency payments",
  "excerpt": "PANews report on August 29th, according to Cryptotimes.",
  "icon": "https://uscloudmedia.s3.us-west-2.amazonaws.com/dhunter/icon/pannews.png",
  "createTime": "2023-08-30 23:30:00",
  "module": 1,
  "tags": ""
}

Full-text Search Queries

Search all documents:

POST /schools/_search

{
  "query": {
    "match_all": {}
  }
}

Fuzzy search across fields:

{
  "query": {
    "multi_match": {
      "query": "SEC",
      "fields": ["title", "excerpt"]
    }
  }
}

Elasticsearch is comonly used in applications requiring fast search performance over large datasets, such as document repositories, news platforms, or lyrics databases.

Tags: elasticsearch SpringBoot IKAnalyzer Full-Text Search rest client

Posted on Sat, 05 Sep 2026 16:45:43 +0000 by pankirk