ElasticSearch Java Api-删除索引
删除可以是删除整个索引库,也可以根据文档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-删除索引的更多相关文章
- [搜索]ElasticSearch Java Api(一) -添加数据创建索引
转载:http://blog.csdn.net/napoay/article/details/51707023 ElasticSearch JAVA API官网文档:https://www.elast ...
- 第08章 ElasticSearch Java API
本章内容 使用客户端对象(client object)连接到本地或远程ElasticSearch集群. 逐条或批量索引文档. 更新文档内容. 使用各种ElasticSearch支持的查询方式. 处理E ...
- Elasticsearch Java API 很全的整理
Elasticsearch 的API 分为 REST Client API(http请求形式)以及 transportClient API两种.相比来说transportClient API效率更高, ...
- Elasticsearch java api 基本搜索部分详解
文档是结合几个博客整理出来的,内容大部分为转载内容.在使用过程中,对一些疑问点进行了整理与解析. Elasticsearch java api 基本搜索部分详解 ElasticSearch 常用的查询 ...
- Elasticsearch java api 常用查询方法QueryBuilder构造举例
转载:http://m.blog.csdn.net/u012546526/article/details/74184769 Elasticsearch java api 常用查询方法QueryBuil ...
- Elasticsearch Java API深入详解
0.题记 之前Elasticsearch的应用比较多,但大多集中在关系型.非关系型数据库与Elasticsearch之间的同步.以上内容完成了Elasticsearch所需要的基础数据量的供给.但想要 ...
- ElasticSearch Java api 详解_V1.0
/×××××××××××××××××××××××××××××××××××××××××/ Author:xxx0624 HomePage:http://www.cnblogs.com/xxx0624/ ...
- Elasticsearch Java API的基本使用
说明 在明确了ES的基本概念和使用方法后,我们来学习如何使用ES的Java API. 本文假设你已经对ES的基本概念已经有了一个比较全面的认识. 客户端 你可以用Java客户端做很多事情: 执行标准的 ...
- (转)ElasticSearch Java Api-检索索引库
上篇博客记录了如何用java调用api把数据写入索引,这次记录下如何搜索. 一.准备数据 String data1 = JsonUtil.model2Json(new Blog(1, "gi ...
随机推荐
- 《c程序设计语言》读书笔记-5.9-指针转换天数和日期
#include "stdio.h" #include "stdlib.h" #include "string.h" static char ...
- [原]C++拾遗
int a=3,b=4; bool ok=(a==2,b==4); printf("%d\n",ok); //输出的结果是1,逗号既不是&& 也不是|| 应该是从前 ...
- delete zone and cfgsave on brocade by CMD
brocade:user> cfgshowDefined configuration: cfg: cfg001 AMS_ESX_HBA1; AMS_ESX_HBA2; HUS_ESX_HBA1; ...
- shell脚本——项目1
案例名称:系统初始化 背景:10台已装有linux系统的服务器 需求: 1.设置时区同步 2.禁用selinux 3.清空防火墙策略 4.历史命令显示操作时间 5.禁止root远程登录 6.禁止定时任 ...
- 孙鑫 VC++深入详解第14课——TCP通信/UDP通信(转)
原文转自 http://blog.csdn.net/supersonico/article/details/18900319 一.用VC++来编写TCP 服务器/客户端的简单程序. 注意事项: 1.要 ...
- cat /proc/maps 进程内存映射【转】
转自:http://blog.csdn.net/fisher_jiang/article/details/5063852 proc/<PID>/maps查看进程的虚拟地址空间是如何使用的. ...
- 从jscript脚本混淆说起
转载:http://www.freebuf.com/column/144897.html 脚本病毒是一个一直以来就存在,且长期活跃着的一种与PE病毒完全不同的一类病毒类型,其制作的门槛低.混淆加密方式 ...
- ObjectInputStream&ObjectOutputStream工具类
序列化:将数据保存到文件:ObjectOutputStream; 反序列化:将文件中的数据显示出来:ObjectInputStream; 在反序列化程序中运行后能够正常输出Person的相关信息, ...
- springBoot【01】
/* 使用spring官网的 http://start.spring.io/ 来建立项目包 生成入口文件,入口文件中对类注释@SpringBootApplication,这个注释是唯一的,标明这个类是 ...
- js-监听页面滚动
两种监听页面滚动的方法 一.原生js通过window.onscroll监听 window.onscroll = function() { //为了保证兼容性,这里取两个值,哪个有值取哪一个 //scr ...