【问题标题】:Delete entire index in ElasticSearch删除 ElasticSearch 中的整个索引
【发布时间】:2021-11-20 23:35:53
【问题描述】:

我正在使用 Java 客户端访问 ElastichSearch。我正在尝试删除整个索引。我可以使用以下代码删除单个文档:

DeleteResponse response = client.prepareDelete("twitter", "tweet", "1").get();

我想在一条指令中删除给定索引的所有文档。请注意,我使用的是 2.2 版。

谢谢

编辑:我发现了一个类似的问题,但它指的是旧的 API 版本。我目前正在使用 2.2 版。

【问题讨论】:

标签: java elasticsearch


【解决方案1】:

最简单的方法是删除您的索引,然后重新创建它。

DeleteIndexResponse deleteResponse = client.admin().indices().delete(new DeleteIndexRequest("your-index")).actionGet()

然后

client.admin().indices().prepareCreate("your-index").get();

这将适用于 2.2 api

【讨论】:

  • 您知道如何使用 Java High Level Rest Client 实现相同的功能吗?这适用于传输客户端,但现在弹性搜索已弃用它。
【解决方案2】:

与 maximede 的答案相同的解决方案,但使用较新版本的 RestHighLevelClient(未折旧):

// delete current index
var deleteRequest = new DeleteIndexRequest("index-name");
client.indices().delete(deleteRequest, RequestOptions.DEFAULT);

// create new one
CreateIndexRequest request = new CreateIndexRequest("index-name");
client.indices().create(request, RequestOptions.DEFAULT);

请确保从正确的包中导入:

org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest
org.elasticsearch.client.indices.CreateIndexRequest;

与 micronaut 框架 io.micronaut.elasticsearch:micronaut-elasticsearch:4.0.0 配合良好

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    • 2021-11-21
    相关资源
    最近更新 更多