Nest 删除所有索引-程序旅途

今天发现 Elasticsearch 中多了很多乱七八糟的索引,有20多个,而且上次我用于 reindex 的索引不见了,由于没有开启密码认证,可能被端口扫描了。

首先我要把所有索引删除,然后再开启密码认证。

删除所有索引

删除索引的文档:https://www.elastic.co/guide/en/elasticsearch/reference/7.8/indices-delete-index.html

删除所有索引可以使用如下命令

curl -XDELETE http://localhost:9200/_all
或
curl -XDELETE http://localhost:9200/*

由于安全起见,这样删除会报以下错误

Wildcard expressions or all indices are not allowed

Nest 删除所有索引-程序旅途

可以通过配置 action.destructive_requires_name: false 允许这样操作

curl XPUT http://localhost:9200/_cluster/settings -d '
{
    永久生效 
    "persistent" : {
        "action.destructive_requires_name" : false
    },
    本次生效,重启集群后失效 
    "transient" : {
        "action.destructive_requires_name" : false
    }
}'

但依然不建议这么做。

使用 Nest 删除所有索引

下面使用 ES 的 .NET 客户端 Nest 删除所有索引

首先获取所有的索引

var indicesResponse = elasticClient.Cat.Indices(cid => cid.AllIndices());

遍历删除

foreach (var item in indicesResponse.Records)
{
    elasticClient.Indices.Delete(item.Index);
}