1、RestClient 介绍
JavaREST客户端有两种模式:
Java Low Level REST Client:ES官方的低级客户端。低级别的客户端通过http与Elasticearch集群通信。
Java High Level REST Client:ES官方的高级客户端。基于上面的低级客户端,也是通过HTTP与ES集群进行通信。它提供了更多的接口。
此外Spring也对RestClient进行了封装,本文主要介绍Java High Level REST Client的使用
2、使用RestClient操作索引
2.1创建链接
java
//创建ES连接对象
String esAuth = Base64.encodeBase64String(("test_user" + ":" + "test_user").getBytes());
RestClientBuilder builder = RestClient.builder(new HttpHost("test01.es.58dns.org", 9299, "http"))
.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder
.setConnectTimeout(13000)
.setSocketTimeout(15000)
.setConnectionRequestTimeout(15000));
builder.setDefaultHeaders(new BasicHeader[]{new BasicHeader("Authorization", "Basic " + esAuth)});
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
System.out.println(restHighLevelClient);
2.2创建索引库
下方代码仅创建了索引库,没有指定映射mapping
可通过request.source("映射字符串",XContentType.JSON)指定映射字符串
java
// 创建索引
CreateIndexRequest createIndexRequest = new CreateIndexRequest("user2");
CreateIndexResponse response = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
boolean acknowledged = response.isAcknowledged();
//判断是否创建索引成功
System.out.println(acknowledged);
2.3 查询索引信息、判断索引库是否存在
java
//查询索引
GetIndexRequest getIndexRequest = new GetIndexRequest("user2");
GetIndexResponse getIndexResponse = restHighLevelClient.indices().get(getIndexRequest, RequestOptions.DEFAULT);
System.out.println("getIndexResponse.getAliases() = " + getIndexResponse.getAliases());
System.out.println("getIndexResponse.getMappings() = " + getIndexResponse.getMappings());
System.out.println("getIndexResponse.getSetting() = " + getIndexResponse.getSettings());
//判断索引是否存在
boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
System.out.println("exists = " + exists);
2.4删除索引,判断是否删除成功
java
//删除索引
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("user2");
AcknowledgedResponse delete = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
//判断是否删除成功
System.out.println("delete.isAcknowledged() = " + delete.isAcknowledged());
3、进行文档操作
java
//新增数据
User user = new User();
user.setName("小李");
user.setAge(12);
user.setPassword("123321");
//转化为json串
ObjectMapper objectMapper = new ObjectMapper();
String userString = objectMapper.writeValueAsString(user);
//创建请求
IndexRequest indexRequest = new IndexRequest();
//指定索引库名称和id
indexRequest.index("user").id("1");
//指定索引类型
indexRequest.type("_doc");
//指定要添加的内容
indexRequest.source(userString, XContentType.JSON);
IndexResponse index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
System.out.println("index.getId() = " + index.getId());
//查询文档
GetRequest getIndexRequest1 = new GetRequest();
getIndexRequest1.index("user").id("1");
GetResponse getIndexResponse1 = restHighLevelClient.get(getIndexRequest1,RequestOptions.DEFAULT);
String sourceAsString = getIndexResponse1.getSourceAsString();
System.out.println("sourceAsString = " + sourceAsString);
//修改文档
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index("user").id("1");
updateRequest.doc("name","129212","age",20);
updateRequest.type("_doc");
UpdateResponse update = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
System.out.println("update.getGetResult() = " + update.getGetResult());
//再次查询文档
GetRequest getIndexRequest2 = new GetRequest();
getIndexRequest2.index("user").id("1");
getIndexRequest2.type("_doc");
GetResponse getIndexResponse2 = restHighLevelClient.get(getIndexRequest2,RequestOptions.DEFAULT);
String sourceAsString2 = getIndexResponse2.getSourceAsString();
System.out.println("sourceAsString2 = " + sourceAsString2);
//删除文档
DeleteRequest deleteRequest1 = new DeleteRequest();
deleteRequest1.index("user").id("1");
restHighLevelClient.delete(deleteRequest1,RequestOptions.DEFAULT);