Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all the indexes and filter the indexes by using Nest in C#

I need to list all the indexes and types in Elasticsearch.

Basically I use _client.stats().Indices to acquire the indexes, and filter using foreach excluded index list like this:

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;
}

Is this right way to do to list all the indexes from Elasticsearch or is there a better way?

like image 845
caoglish Avatar asked Oct 01 '14 01:10

caoglish


3 Answers

GetIndexAsync is removed from Assembly Nest, Version=7.0.0.0 from Version=7.0.0.0 you can use this :

 var result = await _client.Indices.GetAsync(new GetIndexRequest(Indices.All));
like image 149
Mohsen Hosseinalizadeh Avatar answered Nov 10 '22 10:11

Mohsen Hosseinalizadeh


This works, a slightly sexier way of writing it would be using .Except() on result.Indices.

Another route would be by using .CatIndices()

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

like image 3
Martijn Laarman Avatar answered Nov 10 '22 10:11

Martijn Laarman


Code Assembly Nest, Version=6.0.0.0 as below

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

you will get result in result.Indices.Keys string list

like image 2
Harshal Avatar answered Nov 10 '22 10:11

Harshal