删除索引库

简介: 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; } }
 
相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。  
目录
相关文章
|
JSON 数据挖掘 API
深入研究:京东店铺所有商品API详解
本文介绍了一款强大的工具——京东店铺所有商品API,它可以帮助用户批量获取指定京东店铺的商品详细信息。通过传入店铺ID,API可返回包括商品名称、价格、库存、销量等在内的多维度数据,响应格式为JSON。文章还提供了Python调用示例,利用`requests`库完成签名生成与数据请求,助力商家管理店铺、开发者构建应用及数据分析人员挖掘商业价值,极大提升电商运营效率。
374 16
element-plus 下拉框实现全选功能
element-plus 下拉框实现全选功能
1927 0
|
人工智能 程序员 API
作为阿里云生态圈从业者,从第三方视角来说说通义零码
本文作者作为一名零码用户,分享了自己使用零码进行API接口和桌面小工具开发的体验。即使非专业程序员,零码也能提供代码和思路,大大提升编码效率。在阿里云生态圈中,零码帮助团队新人快速成长,实现高效开发。文章还展示了零码在C++程序报错排查中的应用,证明其强大的辅助能力。用零码,大有可为!
628 0
|
机器学习/深度学习 开发框架 量子技术
未来编程:量子计算与量子编程语言的兴起
【5月更文挑战第27天】 在探索计算前沿的旅程中,量子计算以其对传统计算概念的颠覆性挑战而脱颖而出。本文旨在探讨量子计算的基础原理以及正在发展的量子编程语言,这些语言预计将成为未来软件开发的重要工具。我们将简要回顾量子位(qubit)和量子门的逻辑,并分析几个量子编程语言的案例,如Q#、Qiskit和Quipper,以展示它们如何使程序员能够编写和测试量子算法。通过比较传统编程与量子编程的不同之处,本文揭示了量子计算对于未来技术革新的潜力以及它所面临的挑战。
|
机器学习/深度学习 数据采集 数据可视化
使用Python实现深度学习模型:智能医疗影像识别与诊断
【8月更文挑战第19天】 使用Python实现深度学习模型:智能医疗影像识别与诊断
403 0
如何把多个文件(夹)向上移动1层(或多层)(在批量复制前或后进行)
该文介绍了如何使用一个工具将四个文件夹内的所有文件合并到另一个文件夹中。工具可以从百度网盘或蓝奏云下载,提取码分别为qwu2和2r1z。操作步骤包括:打开工具,选择文件批量复制,设置源路径上移,确定设置后将文件夹拖入,导入文件,移除不需要的文件夹路径,最后执行移动操作。移动过程中会删除空文件夹,并在回收站中可见。
1066 1
|
编解码 移动开发 前端开发
【专栏:HTML与CSS移动端开发篇】使用Viewport Meta标签优化移动端显示
【4月更文挑战第30天】本文介绍了HTML的Viewport Meta标签在移动端网页优化中的作用。Viewport Meta标签定义了视口属性,如宽度、高度、初始缩放等,解决移动设备因屏幕尺寸差异导致的显示问题。主要属性包括width(常用device-width)、initial-scale、maximum-scale、minimum-scale和user-scalable。
701 4
|
人工智能 安全 数据挖掘
阿里云高级技术专家李鹏:AI基础设施的演进与挑战 | GenAICon 2024
阿里云高级技术专家、阿里云异构计算AI推理团队负责人李鹏将在主会场第二日上午的AI Infra专场带来演讲,主题为《AI基础设施的演进与挑战》。
数据库系统工程师考点笔记
数据库系统工程师考点笔记
1856 0