本章基于java的sdk演示下es的操作。例子稍简单一点,读者可自己扩展。本发环境如下:mac os 11、es7.8.0、idea2022
maven引入
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.8.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.8.0</version>
</dependency>
client工具类
public class EsUtils {
public static RestHighLevelClient client(){
return new RestHighLevelClient(RestClient.builder(
new HttpHost("127.0.0.1",9200,"http")
));
}
//协议
private static String schema="http";
// 集群地址,如果有多个用“,”隔开
private static String address = "127.0.0.1:9200";
// 连接超时时间
private static int connectTimeout = 5000;
// Socket 连接超时时间
private static int socketTimeout = 5000;
// 获取连接的超时时间
private static int connectionRequestTimeout = 5000;
// 最大连接数
private static int maxConnectNum = 100;
// 最大路由连接数
private static int maxConnectPerRoute =100;
public static RestHighLevelClient client2(){
List<HttpHost> hostLists = new ArrayList<>();
String[] hostList = address.split(",");
for (String addr : hostList) {
String host = addr.split(":")[0];
String port = addr.split(":")[1];
hostLists.add(new HttpHost(host, Integer.parseInt(port)));
}
HttpHost[] httpHost = hostLists.toArray(new HttpHost[]{});
// 构建连接对象
RestClientBuilder builder = RestClient.builder(httpHost);
// 异步连接延时配置
builder.setRequestConfigCallback(requestConfigBuilder -> {
requestConfigBuilder.setConnectTimeout(connectTimeout);
requestConfigBuilder.setSocketTimeout(socketTimeout);
requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeout);
return requestConfigBuilder;
});
// 异步连接数配置
builder.setHttpClientConfigCallback(httpClientBuilder -> {
httpClientBuilder.setMaxConnTotal(maxConnectNum);
httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
httpClientBuilder.setKeepAliveStrategy((response, context) -> Duration.ofMinutes(5).toMillis());
return httpClientBuilder;
});
return new RestHighLevelClient(builder);
}
}
映射实体类
public class Person {
private String id;
private String name;
private int age;
private String address;
}
测试类
public class IndexTest {
//添加索引
public void addIndex() throws IOException {
//1.使用client获取操作索引对象
IndicesClient indices = EsUtils.client().indices();
//2.具体操作获取返回值
//2.1 设置索引名称
CreateIndexRequest createIndexRequest=new CreateIndexRequest("king");
CreateIndexResponse createIndexResponse = indices.create(createIndexRequest, RequestOptions.DEFAULT);
//3.根据返回值判断结果
System.out.println(createIndexResponse.isAcknowledged());
}
public void addIndexAndMapping() throws IOException {
//1.使用client获取操作索引对象
IndicesClient indices = EsUtils.client().indices();
//2.具体操作获取返回值
//2.具体操作,获取返回值
CreateIndexRequest createIndexRequest = new CreateIndexRequest("king");
//2.1 设置mappings
String mapping = "{\n" +
" \"properties\" : {\n" +
" \"address\" : {\n" +
" \"type\" : \"text\"\n" +
// " \"analyzer\" : \"ik_max_word\"\n" +
" },\n" +
" \"age\" : {\n" +
" \"type\" : \"long\"\n" +
" },\n" +
" \"name\" : {\n" +
" \"type\" : \"keyword\"\n" +
" }\n" +
" }\n" +
" }";
createIndexRequest.mapping(mapping, XContentType.JSON);
CreateIndexResponse createIndexResponse = indices.create(createIndexRequest, RequestOptions.DEFAULT);
//3.根据返回值判断结果
System.out.println(createIndexResponse.isAcknowledged());
}
/**
* 查询索引
*/
public void queryIndex() throws IOException {
IndicesClient indices = EsUtils.client().indices();
GetIndexRequest getRequest=new GetIndexRequest("king");
GetIndexResponse response = indices.get(getRequest, RequestOptions.DEFAULT);
// Map<String, MappingMetaData> mappings = response.getMappings();
// //iter 提示foreach
// for (String key : mappings.keySet()) {
// System.out.println(key+"==="+mappings.get(key).getSourceAsMap());
// }
}
/**
* 删除索引
*/
public void deleteIndex() throws IOException {
IndicesClient indices = EsUtils.client().indices();
DeleteIndexRequest deleteRequest=new DeleteIndexRequest("king");
AcknowledgedResponse delete = indices.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
}
/**
* 索引是否存在
*/
public void existIndex() throws IOException {
IndicesClient indices = EsUtils.client().indices();
GetIndexRequest getIndexRequest=new GetIndexRequest("king");
boolean exists = indices.exists(getIndexRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
}