Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to add and remove alias with UpdateAliasesAsync. #8454

Open
advision-digital opened this issue Jan 31, 2025 · 1 comment
Open

Failed to add and remove alias with UpdateAliasesAsync. #8454

advision-digital opened this issue Jan 31, 2025 · 1 comment
Labels

Comments

@advision-digital
Copy link

advision-digital commented Jan 31, 2025

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.

@advision-digital advision-digital added 8.x Relates to 8.x client version Category: Bug labels Jan 31, 2025
@flobernd
Copy link
Member

Hi @advision-digital, sorry for late reply.

The correct syntax is as follows:

var b = client.Indices.UpdateAliasesAsync(x => x.Actions(
	y => y.Add(new AddAction() { Index = indexName, Alias = "xxxxxxxx" }),
	y => y.Remove(new RemoveAction() { Index = indexName, Alias = indexAlias })
)).Result;

Your code is using this overload of Actions:

    public UpdateAliasesRequestDescriptor Actions(Action<Elastic.Clients.Elasticsearch.IndexManagement.IndexUpdateAliasesActionDescriptor> configure)
    {

but what you want is this overload:

    public UpdateAliasesRequestDescriptor Actions(params Action<Elastic.Clients.Elasticsearch.IndexManagement.IndexUpdateAliasesActionDescriptor>[] configure)
    {

Please let me know if that answers your question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants