删除索引库

简介: ElasticSearch Java Api(四) -删除索引标签: elasticsearchjavaapi2016-06-29 14:11 9737人阅读 评论(1) 收藏 举报 分类:Elasticsearch(31) 版权声明:本文为博主原创文章,地址:http://blog.csdn.net/napoay,转载请留言. 目录(?)[+] 删除可以是删除整个索引库,也可以根据文档id删除索引库下的文档,还可以通过query查询条件删除所有符合条件的数据。
 

ElasticSearch Java Api(四) -删除索引

标签: elasticsearchjavaapi
 分类:
Elasticsearch(31) 
 

目录(?)[+]

 

删除可以是删除整个索引库,也可以根据文档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 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 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; } }
 
相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
目录
相关文章
|
Web App开发 存储 算法
WebRTC STUN | Short-term 消息认证
本文是 STUN 协议系列第 1 篇
WebRTC STUN | Short-term 消息认证
|
7月前
|
关系型数据库 数据处理 PostgreSQL
在 Postgres 中使用 Split Part
【8月更文挑战第11天】
388 0
在 Postgres 中使用 Split Part
|
存储 弹性计算 算法
PostgreSQL 垃圾回收参数优化之 - maintenance_work_mem , autovacuum_work_mem
标签 PostgreSQL , 垃圾回收 , 索引扫描 , 内存 背景 夜谈PostgreSQL 垃圾回收参数优化之 - maintenance_work_mem , autovacuum_work_mem。 http://www.postgres.cn/v2/news/viewone/1/398 https://rhaas.blogspot.com/2019/01/how-much
2657 0
|
Web App开发 关系型数据库 测试技术
|
虚拟化
VMware 虚拟机 - 解决 Vmware 启动虚拟机报:该虚拟机似乎正在使用中。 如果该虚拟机未在使用,请按“获取所有权(T)”按钮获取它的所有权。否则,请按“取消(C)”按钮以防损坏的问题
VMware 虚拟机 - 解决 Vmware 启动虚拟机报:该虚拟机似乎正在使用中。 如果该虚拟机未在使用,请按“获取所有权(T)”按钮获取它的所有权。否则,请按“取消(C)”按钮以防损坏的问题
410 0
VMware 虚拟机 - 解决 Vmware 启动虚拟机报:该虚拟机似乎正在使用中。 如果该虚拟机未在使用,请按“获取所有权(T)”按钮获取它的所有权。否则,请按“取消(C)”按钮以防损坏的问题
|
网络协议 测试技术 数据安全/隐私保护
我的mqtt协议和emqttd开源项目个人理解(23) - websocket客户端连接过程分析(Wireshark抓包+源码分析)
我的mqtt协议和emqttd开源项目个人理解(23) - websocket客户端连接过程分析(Wireshark抓包+源码分析)
386 0
|
缓存 关系型数据库 数据库
PostgreSQL技术大讲堂 - 第32讲:数据库参数调整
从零开始学PostgreSQL技术大讲堂 - 第32讲:数据库参数调整
644 2