在Elasticsearch7.15版本之后,Elasticsearch官方将它的高级客户端RestHighLevelClient标记为弃用状态。同时推出了全新的Java API客户端Elasticsearch Java API Client,该客户端也将在Elasticsearch8.0及以后版本中成为官方推荐使用的客户端。
Elasticsearch Java API Client 支持除 Vector tile search API 和 Find structure API 之外的所有 Elasticsearch API。且支持所有API数据类型,并且不再有原始JsonValue属性。它是针对Elasticsearch8.0及之后版本的客户端,目前Elasticsearch已经更新至8.0.1,所以我们需要学习新的Elasticsearch Java API Client的使用方法。
// Create the low-level client RestClientrestClient= RestClient.builder( newHttpHost("localhost", 9200)).build(); // Create the transport with a Jackson mapper ElasticsearchTransporttransport=newRestClientTransport( restClient, newJacksonJsonpMapper()); // And create the API client ElasticsearchClientclient=newElasticsearchClient(transport); GetIndexResponsecreateIndexResponse= client.indices().get(e->e.index("newapi")); System.out.println(String.join(",", createIndexResponse.result().keySet())); transport.close(); restClient.close();
删除索引
1 2 3 4 5 6 7 8 9 10 11 12
// Create the low-level client RestClientrestClient= RestClient.builder( newHttpHost("localhost", 9200)).build(); // Create the transport with a Jackson mapper ElasticsearchTransporttransport=newRestClientTransport( restClient, newJacksonJsonpMapper()); // And create the API client ElasticsearchClientclient=newElasticsearchClient(transport); DeleteIndexResponsedeleteIndexResponse= client.indices().delete(e->e.index("newapi")); System.out.println(deleteIndexResponse.acknowledged()); transport.close(); restClient.close();