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 ...
随机推荐
- Java代码实现真分页
在JavaWeb项目中,分页是一个非常常见且重要的一个小方面.本次作为记载和学习,记录项目中出现的分页并做好学习记录.在这里,用的是SSH框架.框架可以理解如下图: 在JSP页面,描写的代码如下: & ...
- UVALIVE 3571 Visible Lattice Points
就欧拉函数然后地推一下. #include <map> #include <set> #include <list> #include <cmath> ...
- Linux内核实践之工作队列【转】
转自:http://blog.csdn.net/bullbat/article/details/7410563 版权声明:本文为博主原创文章,未经博主允许不得转载. 工作队列(work queue)是 ...
- scandir函数详解
scandir函数详解2009-10-30 10:51scandir函数:读取特定的目录数据表头文件:#include <dirent.h>定义函数:int scandir(const c ...
- 通过GitHub部署项目到Nginx服务器
1.更新源: 2.安装nginx 3.安装成功 4.DNS域名解析 5.访问域名就会找到相应IP地址的主机,一个IP可对应多个域名 6.提交到gitHub 复制这两行 填上邮箱和密码 7.提交成功 8 ...
- JAVA SERVLET 属性范围样例
package com.jeelearning.servlet; import java.io.IOException; import javax.servlet.RequestDispatcher; ...
- Codeforces Gym100952 C. Palindrome Again !!-回文字符串 (2015 HIAST Collegiate Programming Contest)
C. Palindrome Again !! time limit per test 1 second memory limit per test 64 megabytes input sta ...
- HDU 2639 Bone Collector II【01背包 + 第K大价值】
The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup&quo ...
- HDU 2553 N皇后问题(回溯 + 剪枝)
本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398797 题意: 在N*N(N <= 10)的方格棋盘放置了N个皇后,使得它们不相互攻击(即 ...
- 动态规划 001 - 编辑距离(Levenshtein Distance)问题
问题 字符串的编辑距离也被称为距Levenshtein距离(Levenshtein Distance),属于经典算法,常用方法使用递归,更好的方法是使用动态规划算法,以避免出现重叠子问题的反复计算,减 ...