Missing features while migrating from NEST to Elastic.Clients.Elasticsearch

104 Views Asked by At

I am in the process of migrating away from NEST 7.17.5 to Elastic.Clients.Elasticsearch 8.10.0. While most of the code has been migrated, I fail to find a way to get back the functionality of the following functions available in the NEST library:

  • GetIndicesPointingToAlias

It seems they have not been ported at all.

2

There are 2 best solutions below

0
Yan Sklyarenko On BEST ANSWER

Correct. This is the Github issue collecting the list of supported/unsupported APIs for current 8.x client: https://github.com/elastic/elasticsearch-net/issues/7890.

Basically, there's a generic workaround to use low-level client Elastic.Transport: https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/migration-guide.html#_workarounds_for_missing_features. It should work for the cases you listed in your question.

0
AlexandruC On

Since the CAT API is not available in the new Elastic.Clients.Elasticsearch library, I've created an extension method for the ElasticsearchClient class. The method uses the low-level API Elastic.Transport to query the Elasticsearch CAT API.

Get Indices for a Specific Alias

GetIndicesPointingToAlias, returns all the indices that are associated with a specific alias.

    public static IEnumerable<string> GetIndicesPointingToAlias(this ElasticsearchClient client, string alias)
{
    var requestParams = new DefaultRequestParameters();
    requestParams.QueryString.Add("h", "alias,index");

    var response = client.Transport.Request<StringResponse>(HttpMethod.GET, "_cat/aliases", null, requestParams);

    if (!response.ApiCallDetails.HasSuccessfulStatusCode)
    {
        throw new SearchException(response.ApiCallDetails.DebugInformation);
    }

    if (string.IsNullOrEmpty(response.Body))
    {
        return Enumerable.Empty<string>();
    }

    try
    {
        var objects = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(response.Body) ?? new List<Dictionary<string, string>>(); ;

        var indicesForAlias = objects.Where(dict => dict["alias"] == alias).Select(dict => dict["index"]).ToList();

        return indicesForAlias;
    }
    catch (JsonException ex)
    {
        throw new SearchException($"Failed to deserialize response. JSON Exception Message: {ex.Message}, Uri: {response.ApiCallDetails.Uri}, Response Body: {response.Body}");
    }
}