Elasticsearch5.0 Java Api(三) -- 刪除索引

來源:互聯網
上載者:User

標籤:ret   opened   class   admin   rtc   sleep   static   rip   rtti   

  測試刪除索引的功能

  1 package com.juyun.test;  2   3 import java.net.InetAddress;  4 import java.net.UnknownHostException;  5   6 import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;  7 import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;  8 import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;  9 import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest; 10 import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse; 11 import org.elasticsearch.action.delete.DeleteResponse; 12 import org.elasticsearch.action.search.SearchResponse; 13 import org.elasticsearch.client.Client; 14 import org.elasticsearch.common.settings.Settings; 15 import org.elasticsearch.common.transport.InetSocketTransportAddress; 16 import org.elasticsearch.index.query.QueryBuilder; 17 import org.elasticsearch.index.query.QueryBuilders; 18 import org.elasticsearch.search.SearchHit; 19 import org.elasticsearch.search.SearchHits; 20 import org.elasticsearch.transport.client.PreBuiltTransportClient; 21  22 public class ElasticsearchDelete { 23  24     private static String ServerIP = "172.16.0.112"; // ES伺服器IP 25     private static int ServerPort = 9300; // 連接埠號碼 26     private static Client client; 27  28     /** 29      * 測試刪除索引功能 30      * @param args 31      * @throws UnknownHostException  32      */ 33     public static void main(String[] args) throws Exception { 34  35         try { 36             // 設定叢集名稱  37             Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build(); 38             // 建立client 39             client = new PreBuiltTransportClient(settings) 40                     .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ServerIP), ServerPort)); 41  42         } catch (UnknownHostException e) { 43             e.printStackTrace(); 44         } 45  46         // deleteDocById("flow", "data", "AVvm2KHFAQ3YDg7NziSy"); // 刪除指定文檔 47          deleteIndex("flow"); // 刪除名為test的索引庫 48         // deleteByQuery("flow", "data"); 49         // createIndex("flow"); // 建立索引 50          51          52         client.close(); 53     } 54  55     // 根據索引名,類型和id刪除文檔 56     public static void deleteDocById(String indexName,String typeName,String id){ 57          58          DeleteResponse dResponse = client.prepareDelete(indexName, typeName, id).execute() 59                  .actionGet(); 60  61     } 62      63     // 刪除整個索引庫 64     public static void deleteIndex(String indexName) throws UnknownHostException { 65  66         if (!isIndexExists(indexName)) { 67             System.out.println("輸入的索引:"+indexName + "不存在!"); 68         } else { 69             DeleteIndexResponse dResponse = client.admin().indices().prepareDelete(indexName) 70                     .execute().actionGet(); 71             if (dResponse.isAcknowledged()) { 72                 System.out.println("delete index "+indexName+"  successfully!"); 73             }else{ 74                 System.out.println("Fail to delete index "+indexName); 75             } 76         } 77     } 78  79     // 刪除一個類別下的所有doc 80     public static void deleteByQuery(String indexName,String typeName) throws Exception{ 81          82         long startTime=System.currentTimeMillis(); // 擷取開始時間     83         QueryBuilder queryBuilder =QueryBuilders.matchAllQuery(); 84         SearchResponse searchResponse = client.prepareSearch(indexName).setTypes(typeName) 85                 .setQuery(queryBuilder) 86                 .setSize(10000) 87                 .execute() 88                 .actionGet(); 89         SearchHits hits = searchResponse.getHits(); // 擷取搜尋結果 90         Long resultNum = hits.getTotalHits(); // 查詢到的總記錄數 91         System.out.println("查詢到記錄數:" + resultNum); 92         SearchHit[] searchHists = hits.getHits(); 93         if(searchHists.length>0){ 94             for(SearchHit hit:searchHists){ 95                 String id=hit.id(); 96                 deleteDocById(indexName, typeName, id); 97                 System.out.println("已刪除id為:{"+id+"}的文檔"); 98             } 99         }100         if(resultNum>10000){101             Thread.sleep(2000);102             deleteByQuery(indexName,typeName); // 遞迴調用103         }else{104             long endTime=System.currentTimeMillis(); //擷取結束時間105             long duration = endTime-startTime;106             System.out.println(indexName+"索引下"+typeName+"類別下的文檔已全部刪除,刪除過程共耗時"+duration+"ms");107         }108         109     }110     111     112     113     // 建立索引庫114     public static void createIndex(String indexName) {115 116         if (isIndexExists("indexName")) {117             System.out.println("Index  " + indexName + " already exits!");118         } else {119             CreateIndexRequest cIndexRequest = new CreateIndexRequest("indexName");120             CreateIndexResponse cIndexResponse = client.admin().indices().create(cIndexRequest)121                     .actionGet();122             if (cIndexResponse.isAcknowledged()) {123                 System.out.println("create index successfully!");124             } else {125                 System.out.println("Fail to create index!");126             }127 128         }129 130     }131 132     // 判斷索引是否存在,傳入參數為索引庫名稱133     public static boolean isIndexExists(String indexName) {134         boolean flag = false;135         IndicesExistsRequest inExistsRequest = new IndicesExistsRequest(indexName);136 137         IndicesExistsResponse inExistsResponse = client.admin().indices()138                 .exists(inExistsRequest).actionGet();139 140         if (inExistsResponse.isExists()) {141             flag = true;142         } else {143             flag = false;144         }145         return flag;146         }147 }
ElasticsearchDelete

 

Elasticsearch5.0 Java Api(三) -- 刪除索引

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.