添加依赖
<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>x-pack-sql-jdbc</artifactId>
<version>8.7.1</version>
</dependency>
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>8.7.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>2.0.1</version>
</dependency>
application.yaml
spring:
elasticsearch:
rest:
# 是否启用es
enable: true
uris: localhost:9200
host: localhost
port: 9200
username: elastic
password: 123456
index: indexName
crtName: http_ca.crt
注:http_ca.crt文件需要放在resources资源文件下面。(下载的es程序里面有这个文件证书文件)
import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
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.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import javax.annotation.PostConstruct;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
/**
* es8的Java客户端配置
* author:Geng
*/
@Configuration
//@Slf4j
public class ElasticSearchConfig {
@Value("${spring.elasticsearch.rest.host}")
private String host;
@Value("${spring.elasticsearch.rest.enable}")
private boolean enable;
@Value("${spring.elasticsearch.rest.port}")
private int port;
@Value("${spring.elasticsearch.rest.username}")
private String userName;
@Value("${spring.elasticsearch.rest.password}")
private String passWord;
@Value("${spring.elasticsearch.rest.crtName}")
private String tempCrtName;
private static String crtName;
@PostConstruct
private void init() {
crtName = tempCrtName;
}
/**
* 解析配置的字符串,转为HttpHost对象数组
*
* @return
*/
private HttpHost toHttpHost() {
HttpHost httpHost = new HttpHost(host, port, "http");
return httpHost;
}
/**
* 同步客户端
* @return
* @throws Exception
*/
@Bean
public ElasticsearchClient clientBySync() throws Exception {
ElasticsearchTransport transport = getElasticsearchTransport(userName, passWord, toHttpHost());
return new ElasticsearchClient(transport);
}
/**
* 异步客户端
* @return
* @throws Exception
*/
@Bean
public ElasticsearchAsyncClient clientByAsync() throws Exception {
ElasticsearchTransport transport = getElasticsearchTransport(userName, passWord, toHttpHost());
return new ElasticsearchAsyncClient(transport);
}
/**
* 传输对象
* @return
* @throws Exception
*/
@Bean
public ElasticsearchTransport getTransport() throws Exception {
return getElasticsearchTransport(userName, passWord, toHttpHost());
}
private static SSLContext buildSSLContext() {
ClassPathResource resource = new ClassPathResource(crtName);
SSLContext sslContext = null;
try {
CertificateFactory factory = CertificateFactory.getInstance("X.509");
Certificate trustedCa;
try (InputStream is = resource.getInputStream()) {
trustedCa = factory.generateCertificate(is);
}
KeyStore trustStore = KeyStore.getInstance("pkcs12");
trustStore.load(null, null);
trustStore.setCertificateEntry("ca", trustedCa);
SSLContextBuilder sslContextBuilder = SSLContexts.custom()
.loadTrustMaterial(trustStore, null);
sslContext = sslContextBuilder.build();
} catch (CertificateException | IOException | KeyStoreException | NoSuchAlgorithmException |
KeyManagementException e) {
// log.error("ES连接认证失败", e);
}
return sslContext;
}
private static ElasticsearchTransport getElasticsearchTransport(String username, String passwd, HttpHost... hosts) {
// 账号密码的配置
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, passwd));
// 自签证书的设置,并且还包含了账号密码
RestClientBuilder.HttpClientConfigCallback callback = httpAsyncClientBuilder -> httpAsyncClientBuilder
.setSSLContext(buildSSLContext())
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.setDefaultCredentialsProvider(credentialsProvider);
// 用builder创建RestClient对象
RestClient client = RestClient
.builder(hosts)
.setHttpClientConfigCallback(callback)
.build();
return new RestClientTransport(client, new JacksonJsonpMapper());
}
}
索引相关操作
import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.BulkRequest;
import co.elastic.clients.elasticsearch.core.CreateRequest;
import co.elastic.clients.elasticsearch.core.CreateResponse;
import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import co.elastic.clients.elasticsearch.indices.DeleteIndexResponse;
import co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient;
import co.elastic.clients.elasticsearch.indices.GetIndexResponse;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientOptions;
import co.elastic.clients.util.ObjectBuilder;
import com.es.demo.domain.User;
import jdk.internal.instrumentation.TypeMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class ESController {
@Autowired
private ElasticsearchClient syncClient;
@Autowired
private ElasticsearchTransport transport;
@GetMapping("/init")
public void initElastic() throws Exception{
//获取索引客户端对象
ElasticsearchIndicesClient indices = syncClient.indices();
//创建索引 采用构建器的方式构建(在创建之前需要先判断该索引是否存在)
boolean exists = indices.exists(u -> u.index("user")).value();
if (exists) {
System.out.println("该索引已存在!!");
} else {
CreateIndexResponse createIndexResponse = indices.create(c -> c.index("user"));
}
//查询索引
GetIndexResponse getResponse = indices.get(g -> g.index("user"));
System.out.println("查询索引:"+getResponse);
//删除索引
DeleteIndexResponse deleteResponse = indices.delete(d -> d.index("user"));
System.out.println("删除索引:"+deleteResponse.acknowledged());
}
}
文档相关操作
import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.Result;
import co.elastic.clients.elasticsearch.core.BulkRequest;
import co.elastic.clients.elasticsearch.core.CreateRequest;
import co.elastic.clients.elasticsearch.core.CreateResponse;
import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
import co.elastic.clients.elasticsearch.core.search.HitsMetadata;
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import co.elastic.clients.elasticsearch.indices.DeleteIndexResponse;
import co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient;
import co.elastic.clients.elasticsearch.indices.GetIndexResponse;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientOptions;
import co.elastic.clients.util.ObjectBuilder;
import com.es.demo.domain.User;
import jdk.internal.instrumentation.TypeMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class ESController {
@Autowired
private ElasticsearchClient syncClient;
@Autowired
private ElasticsearchTransport transport;
@GetMapping("/init")
public void initElastic() throws Exception{
//获取索引客户端对象
ElasticsearchIndicesClient indices = syncClient.indices();
//创建文档
User user = new User();
user.setId(1001);
user.setUsername("阿桃");
user.setSex("男");
user.setAge(26);
Result result = syncClient.create(c -> c.index("user").id("1001").document(user)).result();
System.out.println("创建文档:"+result);
//批量创建文档
List<User> users = new ArrayList<>(); //假设有数据
syncClient.bulk(b -> { //批量创建操作
users.forEach(u -> { //遍历需要创建的数据
b.operations(
o ->o.create(c -> c.index("user").id(u.getId().toString()).document(u))
);
});
return b;
});
//删除文档
syncClient.delete(d -> d.index("user").id("1001"));
//查询文档
HitsMetadata<Object> hits = syncClient.search(s -> {
s.query(q -> q.match(
m -> m.field("username").query("阿桃")
));
return s;
}, Object.class).hits();
transport.close(); //同步操作时需要关闭,异步时不需要关闭
}
}
异步客户端操作
import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.Result;
import co.elastic.clients.elasticsearch.core.BulkRequest;
import co.elastic.clients.elasticsearch.core.CreateRequest;
import co.elastic.clients.elasticsearch.core.CreateResponse;
import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
import co.elastic.clients.elasticsearch.core.search.HitsMetadata;
import co.elastic.clients.elasticsearch.indices.*;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientOptions;
import co.elastic.clients.util.ObjectBuilder;
import com.es.demo.domain.User;
import jdk.internal.instrumentation.TypeMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class ESController {
@Autowired
private ElasticsearchAsyncClient asyncClient;
@Autowired
private ElasticsearchTransport transport;
@GetMapping("/initAsync")
public void initAsyncElastic() throws Exception{
//获取索引异步客户端对象
ElasticsearchIndicesAsyncClient indices = asyncClient.indices();
//异步调用无法直接获取操作反馈,只能通过回调进行判断
//情况一
indices.create(c -> c.index("newUser")).whenComplete(
(response,error)->{
if(null != response){
System.out.println(response.acknowledged());
}else {
System.out.println(error);
}
}
);
//情况二 thenApply中获取过acknowledged以后后续不用再获取了
//thenApply是在创建完成后执行的,在whenComplete之前
indices.create(c -> c.index("newUser")).thenApply(response -> response.acknowledged())
.whenComplete(
(response,error)->{
if(response.equals(true)){
System.out.println(response);
}else {
System.out.println(error);
}
}
);
}
}