Repository: searchbox-io/Jest
Pull request #573 (2018-01-02 21:16:04)

Added option to fetch indices by alias name
Added an option to the getAliases command to fetch indices by alias name


Patch:
diff --git a/jest-common/src/main/java/io/searchbox/indices/aliases/GetAliases.java b/jest-common/src/main/java/io/searchbox/indices/aliases/GetAliases.java
index 29b2cf8f..965ef960 100644
--- a/jest-common/src/main/java/io/searchbox/indices/aliases/GetAliases.java
+++ b/jest-common/src/main/java/io/searchbox/indices/aliases/GetAliases.java
@@ -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());
     }
 
@@ -20,11 +26,36 @@ public class GetAliases extends GenericResultAbstractAction {
 
     @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);


Test patch:
diff --git a/jest-common/src/test/java/io/searchbox/indices/aliases/GetAliasesTest.java b/jest-common/src/test/java/io/searchbox/indices/aliases/GetAliasesTest.java
index 733a5b85..39fece2a 100644
--- a/jest-common/src/test/java/io/searchbox/indices/aliases/GetAliasesTest.java
+++ b/jest-common/src/test/java/io/searchbox/indices/aliases/GetAliasesTest.java
@@ -1,5 +1,6 @@
 package io.searchbox.indices.aliases;
 
+import java.util.Arrays;
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
@@ -12,7 +13,23 @@ public class GetAliasesTest {
         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
diff --git a/jest/src/test/java/io/searchbox/indices/aliases/GetAliasesIntegrationTest.java b/jest/src/test/java/io/searchbox/indices/aliases/GetAliasesIntegrationTest.java
index 9c6cc3d4..88cf2d40 100644
--- a/jest/src/test/java/io/searchbox/indices/aliases/GetAliasesIntegrationTest.java
+++ b/jest/src/test/java/io/searchbox/indices/aliases/GetAliasesIntegrationTest.java
@@ -2,15 +2,14 @@ package io.searchbox.indices.aliases;
 
 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
  */