ElasticSearch Java Api(四) -删除索引
来源:互联网 发布:mac用的office 编辑:程序博客网 时间:2024/06/07 15:43
删除可以是删除整个索引库,也可以根据文档id删除索引库下的文档,还可以通过query查询条件删除所有符合条件的数据。
一、删除整个索引库
下面的例子会删除indexName索引:
DeleteIndexResponse dResponse = client.admin().indices().prepareDelete(indexName) .execute().actionGet();
- 1
可以根据DeleteIndexResponse对象的isAcknowledged()
方法判断删除是否成功,返回值为boolean类型.
如果传人的indexName不存在会出现异常.可以先判断索引是否存在:
IndicesExistsRequest inExistsRequest = new IndicesExistsRequest(indexName);IndicesExistsResponse inExistsResponse = client.admin().indices() .exists(inExistsRequest).actionGet();
- 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
集群有多个节点的情况下,每个节点都需要安装并重启.
如果想要移除插件,可以执行以下命令:
sudo bin/plugin remove delete-by-query
删除索引名为twitter,类型为tweet,user字段中含有kimchy的所有文档:
DELETE /twitter/tweet/_query?q=user:kimchy
Javaapi参考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; }}
阅读全文
0 0
- ElasticSearch Java Api(四) -删除索引
- ElasticSearch Java Api(四) -删除索引
- ElasticSearch Java Api(四) -删除索引
- ElasticSearch Java Api(四) -删除索引
- ElasticSearch Java Api -删除索引
- 分布式搜索elasticsearch java API 之(三)(四)------索引数据、删除索引数据
- 分布式搜索elasticsearch java API 之(四)------删除索引数据
- 分布式搜索elasticsearch java API 之(四)------删除索引数据
- 分布式搜索elasticsearch java API 之(四)------删除索引数据
- ELK(四)ElasticSearch Java API
- Elasticsearch java API (8)删除API
- 分布式搜索elasticsearch java API 之(六)------批量添加删除索引
- 分布式搜索elasticsearch java API 之(六)------批量添加删除索引
- 分布式搜索elasticsearch java API 之(六)------批量添加删除索引
- Elasticsearch Java API之清空索引
- elasticsearch清空索引缓存 java api
- ElasticSearch Java Api(二) -检索索引库
- ElasticSearch Java Api(三) -更新索引库
- Oracle并行操作——从串行到并行
- 令人振奋的Class(下):继承和实战代码示例
- 关于Mongodb的操作
- HDFS分布式文件系统
- 解决 Bash On Windows 无法下载(慢)
- ElasticSearch Java Api(四) -删除索引
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
- Android四大组件及Intent
- POI 自定义16进制颜色导出
- LeetCode
- 经典问题a+=b和a=a+b
- Torch
- MFC模态对话框和非模态对话框及副窗口调用主窗口控件 自定义消息发送
- 关于弱网测试的一些要点,供测试的朋友们进行参考