1. 项目总览
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c9538ca843bc2e49cdddb608832fdac1.png#pic_center)
2. 配置pom.xml
<?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.shok</groupId>
<artifactId>esTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>esTest</name>
<description>esTest</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.9.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.9.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<finalName>esTest</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source><!-- 源代码使用的JDK版本 -->
<target>8</target><!-- 需要生成的目标class文件的编译版本 -->
<encoding>UTF-8</encoding><!-- 字符集编码 -->
</configuration>
</plugin>
</plugins>
</build>
</project>
3. 配置elasticsearch.properties
# es用户名
elastic.username=elastic
# es密码
elastic.password=kibana2020
#es集群名
elastic.cluster.name=app-es
#es集群地址列表,多个地址,用“,”分开,地址和端口号相对应
elastic.cluster.discover.hostname=192.168.1.58:9200,192.168.1.58:9202,192.168.1.58:9203
#es集群是否加入如自动嗅探
elastic.cluster.clientTransportSniff=true
4. es的初始化-含权限认证
package com.shok.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ConfigUtils {
private static String esConfigFileName = "elasticsearch.properties";
private static String esClusterName;
private static String esClusterDiscoverHostName;
private static String clientTransportSniff;
private static String esUserName;
private static String esPassword;
public static String getEsUserName() {
return esUserName;
}
public static void setEsUserName(String esUserName) {
ConfigUtils.esUserName = esUserName;
}
public static String getEsPassword() {
return esPassword;
}
public static void setEsPassword(String esPassword) {
ConfigUtils.esPassword = esPassword;
}
private static Properties properties = new Properties();
static {
try {
ClassLoader classLoader = ConfigUtils.class.getClassLoader();
InputStream resourceAsStream = classLoader.getResourceAsStream(esConfigFileName);
properties.load(resourceAsStream);
init();
} catch (IOException e) {
e.printStackTrace();
}
}
static void init() {
esUserName = properties.getProperty("elastic.username");
esPassword = properties.getProperty("elastic.password");
esClusterName = properties.getProperty("elastic.cluster.name");
esClusterDiscoverHostName = properties.getProperty("elastic.cluster.discover.hostname");
clientTransportSniff = properties.getProperty("elastic.cluster.clientTransportSniff");
if ("".