ElasticSearch Java Api-刪除索引


刪除可以是刪除整個索引庫,也可以根據文檔id刪除索引庫下的文檔,還可以通過query查詢條件刪除所有符合條件的數據。

一、刪除整個索引庫

下面的例子會刪除indexName索引:

DeleteIndexResponse dResponse = client.admin().indices().prepareDelete(indexName) .execute().actionGet();

可以根據DeleteIndexResponse對象的isAcknowledged()方法判斷刪除是否成功,返回值為boolean類型.
如果傳人的indexName不存在會出現異常.可以先判斷索引是否存在:

IndicesExistsRequest inExistsRequest = new IndicesExistsRequest(indexName); IndicesExistsResponse inExistsResponse = client.admin().indices() .exists(inExistsRequest).actionGet();
  • 1
  • 2
  • 3
  • 4

根據IndicesExistsResponse對象的isExists()方法的boolean返回值可以判斷索引庫是否存在.

二、通過ID刪除

下面的例子是刪除索引名為blog,類型為article,id為1的文檔:

DeleteResponse dResponse = client.prepareDelete("blog", "article", "1").execute().actionGet(); 
  • 1
  • 2

通過DeleteResponse對象的isFound()方法,可以得到刪除是否成功,返回值為boolean類型.

三、通過Query刪除

elasticsearch-2.3 中和舊版本api不太一樣,安裝插件:

sudo bin/plugin install delete-by-query
  • 1

集群有多個節點的情況下,每個節點都需要安裝並重啟.
如果想要移除插件,可以執行以下命令:

sudo bin/plugin remove delete-by-query

刪除索引名為twitter,類型為tweet,user字段中含有kimchy的所有文檔:

DELETE /twitter/tweet/_query?q=user:kimchy

Java api參考Elasticsearch Java Api(六)–DeleteByQuery。

四、java demo

package cn.com.bropen.es; import static org.elasticsearch.index.query.QueryBuilders.termQuery; import java.net.InetAddress; import java.net.UnknownHostException; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest; import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.client.Client; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.index.query.QueryBuilder; public class ElasticSearchCreate { private static String ServerIP = "127.0.0.1";// ElasticSearch server ip private static int ServerPort = 9300;// port private Client client; public static void main(String[] args) { try { Client client = TransportClient.builder().build().addTransportAddress( new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); DeleteResponse dResponse = client.prepareDelete("blog", "article", "11").execute() .actionGet(); if (dResponse.isFound()) { System.out.println("刪除成功"); } else { System.out.println("刪除失敗"); } QueryBuilder qb1 = termQuery("title", "hibernate"); } catch (UnknownHostException e) { e.printStackTrace(); } deleteIndex("test");//刪除名為test的索引庫 } // 刪除索引庫 public static void deleteIndex(String indexName) { try { if (!isIndexExists(indexName)) { System.out.println(indexName + " not exists"); } else { Client client = TransportClient.builder().build().addTransportAddress( new InetSocketTransportAddress(InetAddress.getByName(ServerIP), ServerPort)); DeleteIndexResponse dResponse = client.admin().indices().prepareDelete(indexName) .execute().actionGet(); if (dResponse.isAcknowledged()) { System.out.println("delete index "+indexName+" successfully!"); }else{ System.out.println("Fail to delete index "+indexName); } } } catch (UnknownHostException e) { e.printStackTrace(); } } // 創建索引庫 public static void createIndex(String indexName) { try { Client client = TransportClient.builder().build().addTransportAddress( new InetSocketTransportAddress(InetAddress.getByName(ServerIP), ServerPort)); // 創建索引庫 if (isIndexExists("indexName")) { System.out.println("Index " + indexName + " already exits!"); } else { CreateIndexRequest cIndexRequest = new CreateIndexRequest("indexName"); CreateIndexResponse cIndexResponse = client.admin().indices().create(cIndexRequest) .actionGet(); if (cIndexResponse.isAcknowledged()) { System.out.println("create index successfully!"); } else { System.out.println("Fail to create index!"); } } } catch (UnknownHostException e) { e.printStackTrace(); } } // 判斷索引是否存在 傳入參數為索引庫名稱 public static boolean isIndexExists(String indexName) { boolean flag = false; try { Client client = TransportClient.builder().build().addTransportAddress( new InetSocketTransportAddress(InetAddress.getByName(ServerIP), ServerPort)); IndicesExistsRequest inExistsRequest = new IndicesExistsRequest(indexName); IndicesExistsResponse inExistsResponse = client.admin().indices() .exists(inExistsRequest).actionGet(); if (inExistsResponse.isExists()) { flag = true; } else { flag = false; } } catch (UnknownHostException e) { e.printStackTrace(); } return flag; } }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM