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

时间:2014-10-01 01:10:44

标签: c# .net 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;

        }

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

4 个答案:

答案 0 :(得分:3)

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

另一种方法是使用.CatIndices()

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

答案 1 :(得分:1)

代码Assembly Nest, Version=6.0.0.0如下

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

您将在result.Indices.Keys字符串列表中获得结果

答案 2 :(得分:0)

GetIndexAsync已从Assembly Nest, Version=7.0.0.0中删除 来自Version=7.0.0.0的您可以使用此:

 var result = await _client.Indices.GetAsync(new GetIndexRequest(Indices.All));

答案 3 :(得分:0)

在 Version=7.0.0.0 中,GetIndexAsync 方法不再接受 null。 解决办法:

def local(time, est_weekday = nil)
  days = [:sunday, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday]
  local_time = Time.parse(time)
  utc_time = TZInfo::Timezone.get('America/New_York').local_to_utc(local_time)
  utc_time_formatted = utc_time.strftime("%I:%M %p")
  if est_weekday && days.include?(est_weekday.downcase.to_sym)
    #extract intended wday for ruby datetime and assign
    weekday_index = days.index(est_weekday.downcase.to_sym)
    #get placeholder wday from desired EST day/time
    temp_est_weekday_index = local_time.wday
    #get placeholder wday from adjusted UTC day/time
    temp_utc_weekday_index = utc_time.wday
    #has the conversion to UTC advanced the wday?
    weekday_advances = temp_utc_weekday_index != temp_est_weekday_index
    #adjust wday index if timezone conversion has advanced weekday
    weekday_index += 1 if weekday_advances
    weekday = days[weekday_index]
    return {time: utc_time_formatted, day: weekday || nil }
  else
    return utc_time_formatted
  end
end