ElasticSearch Java Api(四) -刪除索引
阿新 • • 發佈:2018-12-26
刪除可以是刪除整個索引庫,也可以根據文件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();
根據IndicesExistsResponse物件的isExists()方法的boolean返回值可以判斷索引庫是否存在.
二、通過ID刪除
下面的例子是刪除索引名為blog,型別為article,id為1的文件:
DeleteResponse dResponse = client.prepareDelete("blog", "article", "1").execute().actionGet();
通過DeleteResponse物件的isFound()方法,可以得到刪除是否成功,返回值為boolean型別.
三、通過Query刪除
elasticsearch-2.3 中和舊版本api不太一樣,安裝外掛:
sudo bin/plugin install delete-by-query
叢集有多個節點的情況下,每個節點都需要安裝並重啟.
如果想要移除外掛,可以執行以下命令:
sudo bin/plugin remove delete-by-query
刪除索引名為twitter,型別為tweet,user欄位中含有kimchy的所有文件:
DELETE /twitter/tweet/_query?q=user:kimchy
四、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;
}
}
掃碼向博主提問
中科院碩士_姚攀
部落格專家
熟悉Lucene、ES、ELK