【问题标题】:How to get all the indexes and filter the indexes by using Nest in C#如何在 C# 中使用 Nest 获取所有索引并过滤索引
【发布时间】:2014-10-01 01:10:43
【问题描述】:

我需要列出 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


    【解决方案1】:

    GetIndexAsync 已从 Assembly Nest, Version=7.0.0.0 中删除 从Version=7.0.0.0 你可以使用这个:

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • 但是没有办法通过 NEST 1.9 指定通配符过滤器。
      【解决方案3】:

      代码Assembly Nest, Version=6.0.0.0如下

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

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

      【讨论】:

        【解决方案4】:

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

        var response = await _searchClient.Cat.IndicesAsync(c => c.AllIndices());
        var logIndexes = response.Records.Select(a => a.Index).ToArray();
        

        【讨论】:

          猜你喜欢
          • 2023-04-07
          • 1970-01-01
          • 2018-01-12
          • 2011-02-22
          • 2021-07-29
          • 1970-01-01
          • 2018-09-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多