ElasticSearch Java Api(四) -删除索引
生活随笔
收集整理的這篇文章主要介紹了
ElasticSearch Java Api(四) -删除索引
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
刪除可以是刪除整個索引庫,也可以根據文檔id刪除索引庫下的文檔,還可以通過query查詢條件刪除所有符合條件的數據。
一、刪除整個索引庫
下面的例子會刪除indexName索引:
DeleteIndexResponse dResponse = client.admin().indices().prepareDelete(indexName).execute().actionGet();- 1
可以根據DeleteIndexResponse對象的isAcknowledged()方法判斷刪除是否成功,返回值為boolean類型.
如果傳人的indexName不存在會出現異常.可以先判斷索引是否存在:
- 1
- 2
- 3
根據IndicesExistsResponse對象的isExists()方法的boolean返回值可以判斷索引庫是否存在.
二、通過ID刪除
下面的例子是刪除索引名為blog,類型為article,id為1的文檔:
DeleteResponse dResponse = client.prepareDelete("blog", "article", "1").execute().actionGet();- 1
通過DeleteResponse對象的isFound()方法,可以得到刪除是否成功,返回值為boolean類型.
三、通過Query刪除
elasticsearch-2.3 中和舊版本api不太一樣,安裝插件:
sudo bin/plugin install delete-by-query集群有多個節點的情況下,每個節點都需要安裝并重啟.
如果想要移除插件,可以執行以下命令:
刪除索引名為twitter,類型為tweet,user字段中含有kimchy的所有文檔:
DELETE /twitter/tweet/_query?q=user:kimchyJava 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 ipprivate static int ServerPort = 9300;// portprivate 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;}}總結
以上是生活随笔為你收集整理的ElasticSearch Java Api(四) -删除索引的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ElasticSearch Java A
- 下一篇: Java 多线程 yield方法