日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > ElasticSearch Java Api -刪除索引

ElasticSearch Java Api -刪除索引

來源:程序員人生   發布時間:2016-07-14 14:43:39 閱讀次數:5156次

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

1、刪除全部索引庫

下面的例子會刪除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();

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

2、通過ID刪除

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

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

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

3、通過Query刪除

elasticsearch⑵.3 中和舊版本api不太1樣,安裝插件:

sudo bin/plugin install delete-by-query

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

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

java api暫時沒有查到怎樣實現。

4、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; } }
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 久久婷婷成人综合色 | 激情中文字幕 | 在线视频精品一区 | 日日夜夜亚洲 | 国产91精品一区二区 | 亚洲不卡在线观看 | 亚洲一区久久 | 一区二区三区亚洲 | 国产三级一区二区三区 | 一区二区久久久 | 国产在线观看一区二区 | 夜夜骑夜夜骑 | 国产v日产∨综合v精品视频 | 91麻豆国产福利精品 | 久久久久久久久久久久91 | 国产在线视频一区二区 | 国产精品日韩欧美一区二区 | 日本精品在线视频 | 久久不卡 | 一区二区三区四区国产精品 | 午夜亚洲一区 | 中国一级特黄毛片大片 | 黄色1级毛片 | 一级毛片观看 | 欧美日韩国产精品 | 免费av网站在线观看 | 黄色小视频免费观看 | 福利在线观看 | 国产精品国产三级国产 | 国产伦精品一区二区三区精品视频 | 黄色一级片视频 | 国产精品免费一区二区三区 | 欧美日韩亚洲成人 | 午夜精品在线观看 | 国产精品一区在线观看 | 91久久夜色精品国产九色 | 国产成人精品综合 | 草久久免费视频 | 狠狠久久 | 欧美一及黄色片 | 国产成人精品久久二区二区91 |