首页 > 代码库 > Elasticsearch5.0 Java Api(三) -- 删除索引
Elasticsearch5.0 Java Api(三) -- 删除索引
测试删除索引的功能
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 }
Elasticsearch5.0 Java Api(三) -- 删除索引
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。