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 ...
随机推荐
- 3.3 Lucene检索原理
Lucene是一个高效的,基于Java的全文检索库[1].所以在介绍Lucene的检索功能之前,我们要先了解一下全文检索以及Lucene的索引结构. 一.全文检索的基本原理 1. 数据的分类 什么是全 ...
- spring in action 学习笔记六:bean在不同情况下的默认id号或者将名字
bean如果不知名id是什么它一般都有一个id或者讲名字. 第一种情况:组件扫描的情况:默认的id号或者bean的name是类名的首字母小写. 代码如下: package com.qls.beanli ...
- swiper伸缩侧边菜单栏
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- mac 安装 visual studio 配置
前言:今天主要分享的是 Mac 下一款编程软件--Visual Studio,的确,这款软件之前一直是只有 Windows 系统独占的,2017年终于开发了 Mac 版本. 微软这次为 Mac 开发者 ...
- java equals 与 hashCode
转:http://m.blog.csdn.net/blog/pengchua/2297547# 如果你为某个类写了equals方法,那么应该同时编写hashCode方法.如果没有提供hashcode方 ...
- 翻煎饼_简单模拟_C++
一.题目描述(懒人可直接跳过看题目概述) 题目来源: SWUST OJ 题目0254 http://acm.swust.edu.cn/problem/0254/ 二.问题概述 给出一列数,每次可将包 ...
- 【原创】Linux环境下的图形系统和AMD R600显卡编程(7)——AMD显卡的软件中断
CPU上处理的中断可以分成“硬件中断”和“软件中断”两类,比如网卡产生的中断称为硬件中断,而如果是软件使用诸如"int 0x10"(X86平台上)这样的指令产生中断称为软件中断,硬 ...
- python delete数据
#!/usr/bin/env python # -*- coding:utf-8 -*- # @Time : 2017/11/24 0:27 # @Author : lijunjiang # @Fil ...
- log4j配置文件动态指定日志文件名称
我们在项目当中经常会使用log4j进行日志记录,偶尔会遇到一些要求,比如日志文件名称按照启动参数动态配置,而不去修改log4j.xml,比较简单的一种做法是,通过设置系统属性的方式实现,代码: if( ...
- hdu 5109(构造数+对取模的理解程度)
Alexandra and A*B Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...