Demo实现的功能
springBoot集成elasticsearch(下面简称es)有2种实现方案, 一种是基于Jest, 另一种是基于springDataElasticsearch, 我们此次整合采用后者实现.
简单实现对es搜索引擎, 添加索引数据, 查询数据等操作.
github源码地址
https://github.com/mikewuhao/springBoot-es-demo
搭建详细步骤
1. 准备工作
1.1 提前把es环境搭建和启动好, 注意好es版本匹配(本人用的是6.4.3版本)
(本人开始打算用windows环境部署es, 发现从官网下载好慢, 改从虚拟机上使用docker部署es, 参考本人博客: https://blog.csdn.net/mikewuhao/article/details/106690887)
1.2 提前把es的head可视化插件配置好(Kibana插件也可以)
参考本人的博客:https://blog.csdn.net/mikewuhao/article/details/106682282
2. 项目结构

在idea开发工具里面新建maven类型的project, 命名为springBoot-es-demo
按上图目录结构建包
2.1 pom文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wuhao.demo</groupId> <artifactId>springBootEs</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <!--elasticsearch--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--alibaba fastjson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.38</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.3</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
2.2 配置文件application.properties
1 2 3 4 5 6 7 8 | logging.level.org.springframework=DEBUG #端口配置 server.port=8085 #es地址 spring.data.elasticsearch.cluster-nodes=192.168.175.145:9300 spring.data.elasticsearch.cluster-name=docker-cluster |
2.3 启动类, EsApplication
1 2 3 4 5 6 7 8 9 10 11 12 | package com.wuhao; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class EsApplication { public static void main(String[] args) { SpringApplication.run(EsApplication.class,args); } } |
2.4 控制层EsController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | package com.wuhao.controller; import com.wuhao.domain.User; import com.wuhao.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; import java.util.Optional; @RestController public class EsController { @Autowired private UserRepository userRepository; /** * 数据添加进es */ @RequestMapping("/addUser2Es") public void addUser2Es(){ User user= new User(); user.setId(1L); user.setAddress("北京"); user.setSex("男"); user.setUserName("mike"); userRepository.index(user); } /** * 从es查询数据 */ @RequestMapping("/query") public String query(){ List<User> userList = userRepository.findByUserNameLike("mike"); return userList.toString(); } } |
2.5实体类, User
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | package com.wuhao.domain; import org.springframework.data.elasticsearch.annotations.Document; @Document(indexName = "user_index", type = "user") public class User { private Long id; private String userName; private String birthday; private String sex; private String address; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User{" + "id=" + id + ", userName='" + userName + '\'' + ", birthday='" + birthday + '\'' + ", sex='" + sex + '\'' + ", address='" + address + '\'' + '}'; } } |
2.6 实体类, UserRepository
1 2 3 4 5 6 7 8 9 10 11 | package com.wuhao.repository; import com.wuhao.domain.User; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import java.util.List; public interface UserRepository extends ElasticsearchRepository<User, Long> { public List<User> findByUserNameLike(String userName); } |
2.7 操作es的接口类, UserRepository
1 2 3 4 5 6 7 8 9 10 11 | package com.wuhao.repository; import com.wuhao.domain.User; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import java.util.List; public interface UserRepository extends ElasticsearchRepository<User, Long> { public List<User> findByUserNameLike(String userName); } |
演示效果
1 先在浏览器上输入 http://localhost:8085/addUser2Es , 把数据添加进es,
到head插件里查看索引情况, 证明索引创建成功.

2 再在浏览器上输入 http://localhost:8085/query, 执行es的查询功能, 从es查询数据返回到前端

遇到的问题
1 安装的es版本一定和jdk的、spring-boot-starter-data-elasticsearch的版本要对应.
es与jdk的版本对应关系

es与spring data elasticsearch的版本对应关系

2 配置文件里的cluster-name一定对应es和cluster-name一致, 否则启动报错.


参考博客
https://cloud.tencent.com/developer/article/1573013