Remove index from alias using elasticsearch-java client SDK

How do I remove indices from an alias using the elasticsearch-java client?
The API does not seem to have a method to specify the alias.
I cannot find an examples nor documentation.

I am busy converting code from the older highlevel to the newer client.
It will be a while before I am able to compile all the code and or have working tests.
I would rather not find out by trial and error.

What I have now is this:

private void removeIndexFromAlias(final List<String> indicesToDelete, final String alias) throws IOException {
        client.indices()
                .updateAliases((UpdateAliasesRequest.Builder updateAliasesRequestBuilder) ->
                        updateAliasesRequestBuilder
                        .actions( actionBuilder ->
                                        actionBuilder.removeIndex((RemoveIndexAction.Builder removeIndexAction) -> removeIndexAction
                                                //REMARK: I am  not sure if .index(alias) is right here.
                                                // There is no .alias(...) method
                                                // I cannot find examples
                                                .index(alias)
                                                .indices(indicesToDelete))));
    }

The only option to provide an alias is in the .index method as suggested by IntelliJ ;).

Hello!
Don't perform this call! It doesn't delete an index from an alias, it deletes both alias and index. I'll try to understand why this is so sparsely documented an will get back to you, in the meantime the correct way to remove an index from an alias is:

client.indices().deleteAlias(d -> d.index("name-of-the-index").name("name-of-the-alias"))
1 Like

Thank you. It is a lot shorter too. Of course I didn't believe IntelliJ here;) I will probably come back with similar questions when I get stuck again and the IDE or documentation leads me in the wrong direction.

Some thoughts:

Once written the code is easy to read with the new API; but only if you had prior exposure to code written for NodeJS, jQuery or similar. People who wrote less functional style Java will have a harder time understanding it.

Writing the code is hard.
The code I wrote is what I get when I follow the types and use the intellisense of the IDE. The combination of builders and lambdas in combination with generics with Java's type erasure makes it hard for the IDE too to provide sensible options. Having better Javadoc would not get us far enough. Good examples with best practices for common use cases are essential to use this SDK.

Yeah. That's why I wrote Switching from the Java High Level Rest Client to the new Java API Client | Elastic Blog

And I'm maintaining this repo:

I know you already know about it :wink:
I'm sharing this here for future readers.

1 Like

Good examples for non-search actions such as I received from the feedback here would be a welcome addition to that project.