Elasticsearch Java REST高级客户端 删除索引

比眉伴天荒 2023-06-15 11:17 91阅读 0赞

Elasticsearch Java REST高级客户端 删除索引

版本:Elasticsearch 7.2.0

1、索引是否存在

2、删除索引

3、创建客户端

4、maven配置—-pom

1、索引是否存在

  1. /**
  2. * 索引是否存在
  3. * @param indexName
  4. * @return
  5. */
  6. public static boolean exists(String indexName) {
  7. RestHighLevelClient client = getClient();
  8. GetIndexRequest request = new GetIndexRequest(indexName);
  9. request.local(false);
  10. request.humanReadable(true);
  11. request.includeDefaults(false);
  12. try {
  13. return client.indices().exists(request, RequestOptions.DEFAULT);
  14. } catch (IOException e) {
  15. e.printStackTrace();
  16. return false;
  17. }
  18. }

2、删除索引

  1. /**
  2. * 删除索引
  3. */
  4. public static void deleteIndex() {
  5. String indexName = "alert_pinghost-min-2019-11-20";
  6. boolean exists = exists(indexName);
  7. if(!exists) {
  8. //不存在就结束
  9. return ;
  10. }
  11. //索引存在,就执行删除
  12. long s = System.currentTimeMillis();
  13. RestHighLevelClient client = getClient();
  14. DeleteIndexRequest request = new DeleteIndexRequest(indexName);
  15. request.timeout(TimeValue.timeValueMinutes(2));
  16. request.timeout("2m");
  17. try {
  18. AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. }finally {
  22. try {
  23. client.close();
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. long t = System.currentTimeMillis();
  29. //计算删除耗时
  30. System.out.println(t-s);
  31. }

3、创建客户端

REST高级客户端多种方式连接:https://blog.csdn.net/u014646662/article/details/98966833

4、maven配置—-pom

Elasticsearch Java REST高级客户端 maven配置

https://blog.csdn.net/u014646662/article/details/97895028

5、对人工智能感兴趣点下面链接

现在人工智能非常火爆,很多朋友都想学,但是一般的教程都是为博硕生准备的,太难看懂了。最近发现了一个非常适合小白入门的教程,不仅通俗易懂而且还很风趣幽默。所以忍不住分享一下给大家。点这里可以跳转到教程。

https://www.cbedai.net/u014646662

发表评论

表情:
评论列表 (有 0 条评论,91人围观)

还没有评论,来说两句吧...

相关阅读