jar版本7.6.2
1、索引是否存在
boolean exists = reportESClient.indices().exists(new GetIndexRequest(indexName), RequestOptions.DEFAULT); if (!exists) { CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName); createIndexRequest.source(index_mappings, XContentType.JSON);//创建索引 reportESClient.indices().create(createIndexRequest, RequestOptions.DEFAULT); return null; }
2、创建索引
private static final String index_mappings = "{\n" + " \"mappings\" : {\n" + " \"properties\" : {\n" + " \"updateTime\" : {\n" + " \"type\" : \"date\",\n" + " \"format\" : \"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis\"\n" + " },\n" + " \"id\" : {\n" + " \"type\" : \"keyword\"\n" + " },\n" + " \"managePrice\" : {\n" + " \"type\" : \"integer\"\n" + " },\n" + " \"orderNo\" : {\n" + " \"type\" : \"keyword\"\n" + " }\n" + " }\n" + " }\n" + "}";
CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName); createIndexRequest.source(index_mappings, XContentType.JSON);//创建索引 reportESClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
3、保存
UpdateRequest updateRequest = new UpdateRequest(); updateRequest.id(orderNo); updateRequest.index(indexName); updateRequest.doc(JSON.toJSONString(esManagePriceEntity), XContentType.JSON); updateRequest.docAsUpsert(true); UpdateResponse updateResponse = reportESClient.update(updateRequest, RequestOptions.DEFAULT);
4、删除
DeleteRequest deleteRequest = new DeleteRequest(); deleteRequest.id(dto.getOrderNo()); deleteRequest.index(indexName); DeleteResponse deleteResponse = reportESClient.delete(deleteRequest, RequestOptions.DEFAULT); System.out.println("deleteResponse:" + JSON.toJSONString(deleteResponse));
参考
Java High Level REST Client 之 创建索引