如何在C#中使用Nest获取所有索引并过滤索引

cao*_*ish 6 .net c# elasticsearch nest

我需要列出 Elasticsearch 中的所有索引和类型。

基本上我_client.stats().Indices用来获取索引,并使用foreach排除的索引列表进行过滤,如下所示:

public Dictionary<string, Stats> AllIndexes()
{
    _client = new ElasticClient(setting);
    var result = _client.Stats();
    var allIndex = result.Indices;
    var excludedIndexList = ExcludedIndexList();
    foreach (var index in excludedIndexList)
    {
        if (allIndex.ContainsKey(index)) allIndex.Remove(index);
    }

    return allIndex;
}
Run Code Online (Sandbox Code Playgroud)

这是从 Elasticsearch 列出所有索引的正确方法还是有更好的方法?

Moh*_*deh 6

GetIndexAsyncAssembly Nest, Version=7.0.0.0Version=7.0.0.0你可以使用这个删除:

 var result = await _client.Indices.GetAsync(new GetIndexRequest(Indices.All));
Run Code Online (Sandbox Code Playgroud)


Mar*_*man 3

这是可行的,这是一种稍微性感的编写方式,可以.Except()在 result.Indices 上使用。

另一条路线是使用.CatIndices()

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/cat-indices.html