SpringBoot整合Elasticsearch详细步骤以及代码示例(附源码)

准备工作 环境准备

JAVA版本

java version "1.8.0_121" Java(TM) SE Runtime Environment (build 1.8.0_121-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

ES版本

{ "name": "pYaFJhZ", "cluster_name": "my-cluster", "cluster_uuid": "oC28y-cNQduGItC7qq5W8w", "version": { "number": "6.8.2", "build_flavor": "oss", "build_type": "tar", "build_hash": "b506955", "build_date": "2019-07-24T15:24:41.545295Z", "build_snapshot": false, "lucene_version": "7.7.0", "minimum_wire_compatibility_version": "5.6.0", "minimum_index_compatibility_version": "5.0.0" }, "tagline": "You Know, for Search" }

SpringBoot版本

2.1.7.RELEASE

开发工具使用的是IDEA

安装ES

Elasticsearch介绍以及安装:ElasticSearch入门-基本概念介绍以及安装

开始 创建SpringBoot项目

打开IDEA,在菜单中点击
File > New > Project...
在弹框中选择Spring Initializr

图1


然后Next

填写项目名等,然后Next,

图2

选择依赖的jar包(一般我只选Lombok,其他的自己手动加),然后Next。

图3

最后选择项目所在路径,点击Finish。

图3

搞定收工。至此,一个新的SpringBoot项目就新鲜出炉了。

POM文件

当然,具体依赖的jar包肯定不止第2步选择的那些,其中SpringBoot提供的操作ES的jar包spring-boot-starter-data-elasticsearch当然也是必不可少的。

这里贴出最终的pom文件:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.lifengdi</groupId> <artifactId>search</artifactId> <version>0.0.1-SNAPSHOT</version> <name>search</name> <description>elasticsearch</description> <properties> <java.version>1.8</java.version> <testng.version>6.14.2</testng.version> <spring-cloud-dependencies.version>Greenwich.RELEASE</spring-cloud-dependencies.version> <kibana-logging-spring-boot-starter.version>1.2.4</kibana-logging-spring-boot-starter.version> <fastjson.version>1.2.47</fastjson.version> <alarm-spring-boot-starter.version>1.0.15-SNAPSHOT</alarm-spring-boot-starter.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud-dependencies.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--elasticsearch--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!--测试--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>${testng.version}</version> <scope>test</scope> </dependency> <!-- 日期处理 --> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> </dependency> <!--FastJson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> <!--feign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> application.yml文件

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpgjdp.html