java es 删除数据
时间: 2023-12-24 16:45:33 浏览: 84
es-dedupe:通过命令行对elasticsearch文档进行重复数据删除
Java程序中可以使用Elasticsearch的Java API来删除数据。具体步骤如下:
1. 创建Elasticsearch客户端对象
```java
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));
```
2. 构建DeleteRequest对象
```java
DeleteRequest request = new DeleteRequest("index_name", "type_name", "document_id");
```
其中,"index_name"是要删除的索引名称,"type_name"是要删除的文档类型名称,"document_id"是要删除的文档ID。
3. 执行删除操作
```java
DeleteResponse response = client.delete(request).get();
```
4. 关闭Elasticsearch客户端对象
```java
client.close();
```
完整代码示例:
```java
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import java.net.InetAddress;
public class ElasticsearchDeleteDemo {
public static void main(String[] args) throws Exception {
// 创建Elasticsearch客户端对象
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));
// 构建DeleteRequest对象
DeleteRequest request = new DeleteRequest("index_name", "type_name", "document_id");
// 执行删除操作
DeleteResponse response = client.delete(request).get();
// 输出删除结果
System.out.println("Deleted: " + response);
// 关闭Elasticsearch客户端对象
client.close();
}
}
```
阅读全文