Skip to content

Commit

Permalink
Added option to fetch aliases by alias name
Browse files Browse the repository at this point in the history
  • Loading branch information
John Lister committed Jan 2, 2018
1 parent 96335b3 commit 2aea1ad
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package io.searchbox.indices.aliases;

import com.google.common.base.Joiner;
import io.searchbox.action.AbstractMultiIndexActionBuilder;
import io.searchbox.action.GenericResultAbstractAction;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;

/**
* @author cihat keser
*/
public class GetAliases extends GenericResultAbstractAction {
protected String aliasName;

protected GetAliases(Builder builder) {
super(builder);
aliasName = builder.getJoinedAliases();
setURI(buildURI());
}

Expand All @@ -20,11 +26,36 @@ public String getRestMethodName() {

@Override
protected String buildURI() {
return super.buildURI() + "/_aliases";
if (aliasName == null){
return super.buildURI() + "/_alias";
} else {
return super.buildURI() + "/_alias/" + aliasName;
}
}

public static class Builder extends AbstractMultiIndexActionBuilder<GetAliases, Builder> {

protected Set<String> aliasNames = new LinkedHashSet<String>();

public String getJoinedAliases() {
if (aliasNames.size() > 0) {
return Joiner.on(',').join(aliasNames);
} else {
return null;
}
}

public Builder addAlias(String aliasName) {
this.aliasNames.add(aliasName);
return this;
}

public Builder addAliases(Collection<? extends String> aliasNames) {
this.aliasNames.addAll(aliasNames);
return this;
}


@Override
public GetAliases build() {
return new GetAliases(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.searchbox.indices.aliases;

import java.util.Arrays;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
Expand All @@ -12,7 +13,23 @@ public void testBasicUriGeneration() {
GetAliases getAliases = new GetAliases.Builder().addIndex("twitter").build();

assertEquals("GET", getAliases.getRestMethodName());
assertEquals("twitter/_aliases", getAliases.getURI());
assertEquals("twitter/_alias", getAliases.getURI());
}

@Test
public void testBasicUriGenerationWithAliases() {
GetAliases getAliases = new GetAliases.Builder().addIndex("twitter").addAlias("alias").build();

assertEquals("GET", getAliases.getRestMethodName());
assertEquals("twitter/_alias/alias", getAliases.getURI());
}

@Test
public void testBasicUriGenerationWithMultipleAliases() {
GetAliases getAliases = new GetAliases.Builder().addIndex("twitter").addAliases(Arrays.asList(new String[]{"alias1", "alias2"})).build();

assertEquals("GET", getAliases.getRestMethodName());
assertEquals("twitter/_alias/alias1,alias2", getAliases.getURI());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import io.searchbox.client.JestResult;
import io.searchbox.common.AbstractIntegrationTest;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse;
import org.elasticsearch.test.ESIntegTestCase;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
* @author cihat keser
*/
Expand Down

0 comments on commit 2aea1ad

Please sign in to comment.