ElasticSearch Java Api -删除索引
发布时间:2020-12-13 21:11:42 所属栏目:PHP教程 来源:网络整理
导读:删除可以是删除全部索引库,也能够根据文档id删除索引库下的文档,还可以通过query查询条件删除所有符合条件的数据。 1、删除全部索引库 下面的例子会删除indexName索引: DeleteIndexResponse dResponse = client .admin () .indices () .prepareDelete (in
删除可以是删除全部索引库,也能够根据文档id删除索引库下的文档,还可以通过query查询条件删除所有符合条件的数据。 1、删除全部索引库下面的例子会删除indexName索引: DeleteIndexResponse dResponse = client.admin().indices().prepareDelete(indexName)
.execute().actionGet(); 可以根据DeleteIndexResponse对象的 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 demopackage 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","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;
}
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |