Step-by-Step Guide: Installing ElasticSearch, ElasticSearch‑head, and Integrating with Spring Boot
This tutorial walks through installing ElasticSearch on CentOS, setting up the ElasticSearch‑head visual plugin, and integrating ElasticSearch with a Spring Boot application, including environment preparation, configuration, CRUD API implementation, and testing via Postman, providing a comprehensive guide for developers.
1. Introduction
Elastic, the company behind the popular search engine ElasticSearch, went public on Nasdaq in October 2018. ElasticSearch is a Lucene‑based distributed full‑text search server that offers RESTful APIs, real‑time search, high reliability, and easy installation. This article explains how to install ElasticSearch, set up the ElasticSearch‑head visual plugin, and integrate it with Spring Boot.
2. ElasticSearch Installation
We use CentOS7 as the target OS. First, download the ElasticSearch RPM from the official site: https://www.elastic.co/cn/downloads/elasticsearch Then install JDK (if not already present): yum -y install java-1.8.0-openjdk<br/>java -version Install ElasticSearch: rpm -ivh elasticsearch-6.1.0.rpm Find the installation path (usually /usr/share/elasticsearch/) and create data and log directories:
mkdir -p /data/es-data<br/>chown -R elasticsearch:elasticsearch /data/es-data<br/>mkdir -p /log/es-log<br/>chown -R elasticsearch:elasticsearch /log/es-logEdit /etc/elasticsearch/elasticsearch.yml with the following essential settings:
# Set cluster name<br/>cluster.name: my-es<br/><br/># Data and log paths<br/>path.data: /data/es-data<br/>path.logs: /log/es-log<br/><br/># Network settings<br/>network.host: 0.0.0.0<br/>http.port: 9200<br/><br/># Enable CORS for elasticsearch‑head<br/>http.cors.enabled: true<br/>http.cors.allow-origin: "*"Start and enable the service:
systemctl start elasticsearch<br/>systemctl status elasticsearch<br/>systemctl enable elasticsearchVerify the installation: curl -X GET http://localhost:9200 If the firewall blocks the port, disable it:
# Check firewall status<br/>firewall-cmd --state<br/># Stop firewall<br/>systemctl stop firewalld.service<br/># Disable firewall on boot<br/>systemctl disable firewalld.service3. ElasticSearch‑head Installation
ElasticSearch‑head provides a web UI for browsing indices. It requires Node.js and Git.
Install Node.js:
wget https://nodejs.org/dist/v10.9.0/node-v10.9.0-linux-x64.tar.gz<br/>tar -zxvf node-v10.9.0-linux-x64.tar.gz<br/>mv node-v10.9.0-linux-x64 /usr/local/nodejs<br/>ln -s /usr/local/nodejs/bin/node /usr/bin/node<br/>ln -s /usr/local/nodejs/bin/npm /usr/bin/npm<br/>node -v<br/>npm -vInstall Git (if missing) and clone the head project:
yum install -y git<br/>git clone https://github.com/mobz/elasticsearch-head.gitEnter the project directory and install dependencies using the Taobao mirror for speed:
cd elasticsearch-head<br/>npm install cnpm -g --registry=https://registry.npm.taobao.org<br/>ln -s /usr/local/nodejs/bin/cnpm /usr/local/bin/cnpm<br/>cnpm installModify _site/app.js to point to your ElasticSearch server (e.g., 197.168.24.207 instead of localhost).
Start the UI service in the background: cd node_modules/grunt/bin<br/>nohup ./grunt server & Access the UI at http://<your‑ip>:9100 and verify that the student index appears.
4. Spring Boot Integration
Add the following dependencies to pom.xml:
<!-- JPA support -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- ElasticSearch -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>Configure the connection in application.properties:
spring.data.elasticsearch.cluster-name=my-es
spring.data.elasticsearch.cluster-nodes=197.168.24.207:9300Create the Student entity:
@Data
@Accessors(chain = true)
@Document(indexName = "student", type = "school")
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String id;
private String name;
private String gender;
private Integer age;
}Define a repository extending ElasticsearchRepository with custom query methods:
public interface StudentRepository extends ElasticsearchRepository<Student, String> {
List<Student> findByNameLike(String keyword);
@Query("{\"match_phrase\":{\"name\":\"?0\"}}")
List<Student> findByNameCustom(String keyword);
}Implement a REST controller providing CRUD endpoints:
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentRepository studentRepository;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
// batch add, add, update, delete, get all, get by id, simple search, custom search, advanced search ... (methods omitted for brevity)
}Run the Spring Boot application and test the APIs with Postman (batch add, single add, update, delete, query all, query by ID, fuzzy name search, custom phrase search, and advanced search using NativeSearchQueryBuilder).
5. Summary
ElasticSearch delivers high‑performance search over massive datasets. While the installation steps are a bit involved, the subsequent Spring Boot integration is straightforward. This guide serves as an introductory tutorial for developers new to ElasticSearch; advanced search techniques can be explored in the official API documentation.
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.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
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.
