Skip to content

Failed to add and remove alias with UpdateAliasesAsync. #8454

@advision-digital

Description

@advision-digital

Elastic.Clients.Elasticsearch version: 8.17.1

Elasticsearch version: 8.14.3

.NET runtime version: net8.0

Description of the problem including expected versus actual behavior:
I am trying to change the alias of an index using the UpdateAliasesAsync method.

using Elastic.Clients.Elasticsearch;
using Elastic.Clients.Elasticsearch.IndexManagement;
using Elastic.Clients.Elasticsearch.Serialization;
using Elastic.Transport;

namespace debug_console
{
    internal partial class Program
    {
        static void Main(string[] args)
        {
            var options = new ElasticsearchServerOptions();
            var cloudNodePool = new CloudNodePool(options.CloudId, new ApiKey(options.ApiKey));
            var settings = new ElasticsearchClientSettings(cloudNodePool)
                           .ServerCertificateValidationCallback((a, b, c, d) => true) // ordering of fluent api calls matters
                           .ConnectionLimit(options.ConnectionLimit)
                           .DefaultDisableIdInference()
                           .DisableAuditTrail()
                           .DisableMetaHeader()
                           .DisablePing()
                           .EnableHttpCompression(true)
                           .EnableThreadPoolStats(false)
                           .IncludeServerStackTraceOnError(false)
                           .PrettyJson(false);
            var client = new ElasticsearchClient(settings);
            var list = new List<Dictionary<string, DateTimeOffset>>
            {
                new() { { "datetimeoffset", DateTimeOffset.Now } }
            };

            var indexName = "debug-0001";
            var indexAlias = "alias-0001";
            var r1 = client.IndexManyAsync(list, indexName).Result;

            var a = client.Indices.UpdateAliasesAsync(x => x.Actions(y => y.Add(new AddAction() { Index = indexName, Alias = indexAlias }))).Result;
            /* result of previous call
             * GET debug-0001/_alias
             {
                "debug-0001": {
                    "aliases": {
                        "alias-0001": {}
                   }
                }
              }
              */

            var caseSwitch = true;
            if (caseSwitch)
            {

                var b = client.Indices.UpdateAliasesAsync(x => x.Actions(
                                                                   y => y.Add(new AddAction() { Index = indexName, Alias = "xxxxxxxx" })
                                                                         .Remove(new RemoveAction() { Index = indexName, Alias = indexAlias })
                                                         )).Result;
                /* RESULT
                 * GET debug-0001/_alias
                {
                    "debug-0001": {
                        "aliases": {}
                    }
                }
                */


                /* EXPECTED
                 * GET debug-0001/_alias
                {
                    "debug-0001": {
                        "aliases": {
                            "xxxxxxxx": {}
                        }
                    }
                }
                */
            }
            else
            {
                var c = client.Indices.UpdateAliasesAsync(x => x.Actions(
                                                                   y => y.Remove(new RemoveAction() { Index = indexName, Alias = indexAlias })
                                                                         .Add(new AddAction() { Index = indexName, Alias = "xxxxxxxx" })
                                                         )).Result;
                /* RESULT
                 * GET debug-0001/_alias
                {
                    "debug-0001": {
                        "aliases": {
                            "alias-0001": {},
                            "xxxxxxxx": {}
                        }
                    }
                }
                */

                /* EXPECTED
                 * GET debug-0001/_alias
                {
                    "debug-0001": {
                        "aliases": {
                            "xxxxxxxx": {}
                        }
                    }
                }
                */
            }

        }

    }
}

Expected behavior
The alias of debug-0001 should be xxxxxxxx.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions