删除可以是删除整个索引库,也可以根据文档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();
  • 1
  • 2
  • 3
  • 4

根据IndicesExistsResponse对象的isExists()方法的boolean返回值可以判断索引库是否存在.

二、通过ID删除

下面的例子是删除索引名为blog,类型为article,id为1的文档:

DeleteResponse dResponse = client.prepareDelete("blog", "article", "1").execute().actionGet();
  • 1
  • 2

通过DeleteResponse对象的isFound()方法,可以得到删除是否成功,返回值为boolean类型.

三、通过Query删除

elasticsearch-2.3 中和旧版本api不太一样,安装插件:

sudo bin/plugin install delete-by-query
  • 1

集群有多个节点的情况下,每个节点都需要安装并重启.
如果想要移除插件,可以执行以下命令:

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 Java Api-删除索引的更多相关文章

  1. [搜索]ElasticSearch Java Api(一) -添加数据创建索引

    转载:http://blog.csdn.net/napoay/article/details/51707023 ElasticSearch JAVA API官网文档:https://www.elast ...

  2. 第08章 ElasticSearch Java API

    本章内容 使用客户端对象(client object)连接到本地或远程ElasticSearch集群. 逐条或批量索引文档. 更新文档内容. 使用各种ElasticSearch支持的查询方式. 处理E ...

  3. Elasticsearch Java API 很全的整理

    Elasticsearch 的API 分为 REST Client API(http请求形式)以及 transportClient API两种.相比来说transportClient API效率更高, ...

  4. Elasticsearch java api 基本搜索部分详解

    文档是结合几个博客整理出来的,内容大部分为转载内容.在使用过程中,对一些疑问点进行了整理与解析. Elasticsearch java api 基本搜索部分详解 ElasticSearch 常用的查询 ...

  5. Elasticsearch java api 常用查询方法QueryBuilder构造举例

    转载:http://m.blog.csdn.net/u012546526/article/details/74184769 Elasticsearch java api 常用查询方法QueryBuil ...

  6. Elasticsearch Java API深入详解

    0.题记 之前Elasticsearch的应用比较多,但大多集中在关系型.非关系型数据库与Elasticsearch之间的同步.以上内容完成了Elasticsearch所需要的基础数据量的供给.但想要 ...

  7. ElasticSearch Java api 详解_V1.0

    /×××××××××××××××××××××××××××××××××××××××××/ Author:xxx0624 HomePage:http://www.cnblogs.com/xxx0624/ ...

  8. Elasticsearch Java API的基本使用

    说明 在明确了ES的基本概念和使用方法后,我们来学习如何使用ES的Java API. 本文假设你已经对ES的基本概念已经有了一个比较全面的认识. 客户端 你可以用Java客户端做很多事情: 执行标准的 ...

  9. (转)ElasticSearch Java Api-检索索引库

    上篇博客记录了如何用java调用api把数据写入索引,这次记录下如何搜索. 一.准备数据 String data1 = JsonUtil.model2Json(new Blog(1, "gi ...

随机推荐

  1. Mysql大数据备份及恢复

    <p>[引自攀岩人生的博客]MySQL备份一般采取全库备份.日志备份;MySQL出现故障后可以使用全备份和日志备份将数据恢复到最后一个二进制日志备份前的任意位置或时间;mysql的二进制日 ...

  2. HDU1541 经典树状数组

    HDU1541 题意: 如图,等级为0的点有1,等级为1得点有4,2  等级为2的点有3,等级为3的点有5-------即即左下角的点的个数 现给你一些点(x,y),输入顺序按y升序,y相等时按x升序 ...

  3. 高级数据查询SQL语法

    接上一篇关系数据库SQL之基本数据查询:子查询.分组查询.模糊查询,主要是关系型数据库基本数据查询.包括子查询.分组查询.聚合函数查询.模糊查询,本文是介绍一下关系型数据库几种高级数据查询SQL语法, ...

  4. ClientScript.RegisterClientScriptBlock 不执行

    ClientScript.RegisterClientScriptBlock 不执行 页面中 form标签必须加入 runat=server

  5. zepto.js 自定义打包集成其他模块构建流程

    1.首先在自己的电脑上要安装Node.js和npm包管理工具: 2.从github上下载zepto.js的源文件包到本地磁盘(例如:E:\Learning\JS): 地址:https://github ...

  6. C语言中的“>>”和“<<”

    http://baike.1688.com/doc/view-d1750791.html C语言中的“>>”和“<<” [标签:程序设计] 浏览次数:68937提问时间:200 ...

  7. Altium 原理图出现元件 “Extra Pin…in Normal of part ”警告

    原理是因为元器件库中的元器件的所有模式MODE不能和pcb中的引进匹配造成的,那什么又是MODE呢, 看下买的图便很清楚了, MODE可以理解为不同的视图模式吧. 然后赶紧打开原理图库中的有问题的元器 ...

  8. Bash 文件夹操作

    mkdir, rm,find都是对tree结构的文件夹进行的操作,可以安装tree用tree命令直接打印出树的结构 文件夹的操作分为只操作当前文件夹的集合数据和迭代操作的tree数据 Bash迭代当前 ...

  9. HDU 6395 2018 Multi-University Training Contest 7 (快速幂+分块)

    原题地址 Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)T ...

  10. Linq 连接运算符:Concat,Union

    //Concat()方法附加两个相同类型的序列,并返回一个新序列(集合)IList<string> strList = new List<string>() { "O ...