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

5

7

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?

Bryannabryansk answered 1/10, 2014 at 1:10 Comment(0)
R
13

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));
Remembrancer answered 8/9, 2019 at 9:51 Comment(0)
T
3

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

Tralee answered 16/10, 2014 at 11:5 Comment(1)
But there isn't away to specify a wildcard filter via NEST 1.9.Catercornered
P
2

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

Phthisis answered 30/8, 2018 at 13:50 Comment(0)
K
0

In Version=7.0.0.0 the method GetIndexAsync doesn't accept null any more. The solution:

var response = await _searchClient.Cat.IndicesAsync(c => c.AllIndices());
var logIndexes = response.Records.Select(a => a.Index).ToArray();
Kapellmeister answered 29/1, 2021 at 20:29 Comment(0)
M
0

Just in case anyone is still looking...With NEST 7.* I ended up with:

public async Task<IReadonlyList<string>> GetIndicesByPattern(string indexNamePrefix)
{
    CatResponse<CatIndicesRecord>? result = await esClient.Cat.IndicesAsync(i => i.index(indexNamePrefix));
    return result.Records.Select(i => i.Index).ToList();
}
Mcqueen answered 23/5, 2023 at 14:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.