本文讲述使用spring集成elasticSearch
1、引入spring-boot-starter-data-elasticsearch包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
2、添加es配置
不同的版本的配置方法不太一致。这里我呈现两个版本的配置方式
4.30版本
方法一、yml文件添加
spring:
elasticsearch:
uris: ["127.0.0.1:9200"]
username: "elastic"
password: "yuyuyu"
方法二、便携config文件
package com.example.esdemo;
import lombok.extern.log4j.Log4j2;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch