Repository: searchbox-io/Jest
Pull request #420 (2016-11-23 12:51:58)

Add ES5 support to indices stats


Patch:
diff --git a/jest-common/src/main/java/io/searchbox/action/AbstractAction.java b/jest-common/src/main/java/io/searchbox/action/AbstractAction.java
index dce475c3..81b9e6f8 100644
--- a/jest-common/src/main/java/io/searchbox/action/AbstractAction.java
+++ b/jest-common/src/main/java/io/searchbox/action/AbstractAction.java
@@ -258,6 +258,21 @@ public abstract class AbstractAction<T extends JestResult> implements Action<T>
         protected Map<String, Object> headers = new LinkedHashMap<String, Object>();
         protected Set<String> cleanApiParameters = new LinkedHashSet<String>();
 
+        public K toggleApiParameter(String key, boolean enable) {
+            if (enable) {
+                addCleanApiParameter(key);
+            } else {
+                removeCleanApiParameter(key);
+            }
+
+            return (K) this;
+        }
+
+        public K removeCleanApiParameter(String key) {
+            cleanApiParameters.remove(key);
+            return (K) this;
+        }
+
         public K addCleanApiParameter(String key) {
             cleanApiParameters.add(key);
             return (K) this;
diff --git a/jest-common/src/main/java/io/searchbox/indices/Stats.java b/jest-common/src/main/java/io/searchbox/indices/Stats.java
index 2e350c2f..24a20672 100644
--- a/jest-common/src/main/java/io/searchbox/indices/Stats.java
+++ b/jest-common/src/main/java/io/searchbox/indices/Stats.java
@@ -1,10 +1,9 @@
 package io.searchbox.indices;
 
+import com.google.common.base.Joiner;
 import io.searchbox.action.AbstractMultiIndexActionBuilder;
 import io.searchbox.action.GenericResultAbstractAction;
 
-import java.util.Arrays;
-
 /**
  * @author Dogukan Sonmez
  * @author cihat keser
@@ -35,45 +34,71 @@ public class Stats extends GenericResultAbstractAction {
         }
 
         public Builder docs(boolean docs) {
-            return setParameter("docs", docs);
+            return toggleApiParameter("docs", docs);
         }
 
         public Builder store(boolean store) {
-            return setParameter("store", store);
+            return toggleApiParameter("store", store);
         }
 
         public Builder indexing(boolean indexing) {
-            return setParameter("indexing", indexing);
+            return toggleApiParameter("indexing", indexing);
         }
 
         public Builder indexing(boolean indexing, String... types) {
-            setParameter("indexing", indexing);
-            setParameter("types", Arrays.asList(types));
+            toggleApiParameter("indexing", indexing);
+            setParameter("types", Joiner.on(",").join(types));
             return this;
         }
 
         public Builder get(boolean get) {
-            return setParameter("get", get);
+            return toggleApiParameter("get", get);
         }
 
         public Builder warmer(boolean warmer) {
-            return setParameter("warmer", warmer);
+            return toggleApiParameter("warmer", warmer);
         }
 
         public Builder merge(boolean merge) {
-            return setParameter("merge", merge);
+            return toggleApiParameter("merge", merge);
         }
 
         public Builder flush(boolean flush) {
-            return setParameter("flush", flush);
+            return toggleApiParameter("flush", flush);
         }
 
         public Builder refresh(boolean refresh) {
-            return setParameter("refresh", refresh);
+            return toggleApiParameter("refresh", refresh);
         }
 
         public Builder search(boolean search) {
-            return setParameter("search", search);
+            return toggleApiParameter("search", search);
+        }
+
+        public Builder search(boolean search, String... groups) {
+            toggleApiParameter("search", search);
+            setParameter("groups", Joiner.on(",").join(groups));
+            return this;
+        }
+
+        public Builder completion(boolean completion) {
+            return toggleApiParameter("completion", completion);
+        }
+
+        public Builder fielddata(boolean fielddata) {
+            return toggleApiParameter("fielddata", fielddata);
+        }
+
+        public Builder requestCache(boolean requestCache) {
+            return toggleApiParameter("request_cache", requestCache);
+        }
+
+        public Builder suggest(boolean suggest) {
+            return toggleApiParameter("suggest", suggest);
+        }
+
+        public Builder translog(boolean translog) {
+            return toggleApiParameter("translog", translog);
         }
 
         // TODO add "search with groups" parameter


Test patch:
diff --git a/jest-common/src/test/java/io/searchbox/indices/StatsTest.java b/jest-common/src/test/java/io/searchbox/indices/StatsTest.java
index 58a9fc64..27744449 100644
--- a/jest-common/src/test/java/io/searchbox/indices/StatsTest.java
+++ b/jest-common/src/test/java/io/searchbox/indices/StatsTest.java
@@ -1,7 +1,10 @@
 package io.searchbox.indices;
 
+import io.searchbox.action.Action;
 import org.junit.Test;
 
+import java.net.URLDecoder;
+
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotEquals;
 
@@ -31,4 +34,24 @@ public class StatsTest {
         assertNotEquals(stats1, stats2);
     }
 
+    @Test
+    public void testUriGenerationWithStatsFields() throws Exception {
+        Action action = new Stats.Builder()
+                .flush(true)
+                .indexing(true)
+                .search(true, "group1", "group2")
+                .build();
+        assertEquals("_all/_stats/flush,indexing,search?groups=group1,group2", URLDecoder.decode(action.getURI()));
+    }
+
+    @Test
+    public void testUriGenerationWhenRemovingStatsFields() throws Exception {
+        Action action = new Stats.Builder()
+                .flush(true)
+                .indexing(true)
+                .indexing(false)
+                .build();
+        assertEquals("_all/_stats/flush", URLDecoder.decode(action.getURI()));
+    }
+
 }
\ No newline at end of file
diff --git a/jest/src/test/java/io/searchbox/indices/StatsIntegrationTest.java b/jest/src/test/java/io/searchbox/indices/StatsIntegrationTest.java
index d3b27df5..b16aff48 100644
--- a/jest/src/test/java/io/searchbox/indices/StatsIntegrationTest.java
+++ b/jest/src/test/java/io/searchbox/indices/StatsIntegrationTest.java
@@ -17,6 +17,7 @@ import java.util.concurrent.TimeoutException;
 public class StatsIntegrationTest extends AbstractIntegrationTest {
 
     private static final String INDEX_NAME = "flush_test_index";
+    private static final String STATS_WITH_OPTIONS_INDEX_NAME = "stats_with_options_index";
 
     @Test
     public void testDefaultStats() throws InterruptedException, ExecutionException, TimeoutException, IOException {
@@ -37,4 +38,27 @@ public class StatsIntegrationTest extends AbstractIntegrationTest {
         assertNotNull(statsJson.getAsJsonObject("get"));
         assertNotNull(statsJson.getAsJsonObject("search"));
     }
+
+    @Test
+    public void testStatsWithOptions() throws InterruptedException, ExecutionException, TimeoutException, IOException {
+        createIndex(STATS_WITH_OPTIONS_INDEX_NAME);
+        ensureSearchable(STATS_WITH_OPTIONS_INDEX_NAME);
+
+        Stats stats = new Stats.Builder()
+                .flush(true)
+                .indexing(true)
+                .build();
+
+        JestResult result = client.execute(stats);
+        assertTrue(result.getErrorMessage(), result.isSucceeded());
+
+        // Confirm that response has only flush and indexing stats types
+        JsonObject jsonResult = result.getJsonObject();
+        JsonObject statsJson = jsonResult.getAsJsonObject("indices").getAsJsonObject(STATS_WITH_OPTIONS_INDEX_NAME).getAsJsonObject("total");
+        assertNotNull(statsJson);
+        assertNotNull(statsJson.getAsJsonObject("flush"));
+        assertNotNull(statsJson.getAsJsonObject("indexing"));
+        assertEquals("Number of stats received", 2, statsJson.entrySet().size());
+    }
+
 }