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

问题描述 投票:3回答:2

我需要列出所有索引并输入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;

        }

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

c# .net elasticsearch nest
2个回答
3
投票

这是有效的,一种略显性感的写作方式将使用.Except()对result.Indices。

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

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


1
投票

代码Assembly Nest, Version=6.0.0.0如下

var result = await _client.GetIndexAsync(null, c => c
                                     .AllIndices()
                             );

你会得到result.Indices.Keys字符串列表中的结果

© www.soinside.com 2019 - 2024. All rights reserved.