Repository: searchbox-io/Jest
Pull request #503 (2017-04-21 11:49:20)

Add support for indices flush parameters
This change set adds support for the "force" and "wait_if_ongoing" parameters to the indices flush operation.

https://www.elastic.co/guide/en/elasticsearch/reference/2.4/indices-flush.html


Patch:
diff --git a/jest-common/src/main/java/io/searchbox/indices/Flush.java b/jest-common/src/main/java/io/searchbox/indices/Flush.java
index a4561fce..52114981 100644
--- a/jest-common/src/main/java/io/searchbox/indices/Flush.java
+++ b/jest-common/src/main/java/io/searchbox/indices/Flush.java
@@ -25,6 +25,21 @@ public class Flush extends GenericResultAbstractAction {
     }
 
     public static class Builder extends AbstractMultiIndexActionBuilder<Flush, Builder> {
+        public Builder force(boolean force) {
+            return setParameter("force", force);
+        }
+
+        public Builder force() {
+            return force(true);
+        }
+
+        public Builder waitIfOngoing(boolean waitIfOngoing) {
+            return setParameter("wait_if_ongoing", waitIfOngoing);
+        }
+
+        public Builder waitIfOngoing() {
+            return waitIfOngoing(true);
+        }
 
         @Override
         public Flush build() {


Test patch:
diff --git a/jest-common/src/test/java/io/searchbox/indices/FlushTest.java b/jest-common/src/test/java/io/searchbox/indices/FlushTest.java
index 8ab1a696..c19f8d7c 100644
--- a/jest-common/src/test/java/io/searchbox/indices/FlushTest.java
+++ b/jest-common/src/test/java/io/searchbox/indices/FlushTest.java
@@ -15,6 +15,22 @@ public class FlushTest {
         assertEquals("twitter%2Cmyspace/_flush", flush.getURI());
     }
 
+    @Test
+    public void testBasicUriGenerationWithForce() {
+        Flush flush = new Flush.Builder().addIndex("twitter").force().build();
+
+        assertEquals("POST", flush.getRestMethodName());
+        assertEquals("twitter/_flush?force=true", flush.getURI());
+    }
+
+    @Test
+    public void testBasicUriGenerationWithWaitIfOngoing() {
+        Flush flush = new Flush.Builder().addIndex("twitter").waitIfOngoing().build();
+
+        assertEquals("POST", flush.getRestMethodName());
+        assertEquals("twitter/_flush?wait_if_ongoing=true", flush.getURI());
+    }
+
     @Test
     public void equalsReturnsTrueForSameIndices() {
         Flush flush1 = new Flush.Builder().addIndex("twitter").addIndex("myspace").build();
@@ -30,5 +46,4 @@ public class FlushTest {
 
         assertNotEquals(flush1, flush2);
     }
-
 }
\ No newline at end of file
diff --git a/jest/src/test/java/io/searchbox/indices/FlushIntegrationTest.java b/jest/src/test/java/io/searchbox/indices/FlushIntegrationTest.java
index f8737907..9e27f8bd 100644
--- a/jest/src/test/java/io/searchbox/indices/FlushIntegrationTest.java
+++ b/jest/src/test/java/io/searchbox/indices/FlushIntegrationTest.java
@@ -44,6 +44,36 @@ public class FlushIntegrationTest extends AbstractIntegrationTest {
                 stats1.getShards().length, stats1.getTotal().getFlush().getTotal());
     }
 
+    @Test
+    public void testFlushWithForce() throws InterruptedException, ExecutionException, TimeoutException, IOException {
+        Flush flush = new Flush.Builder().force().build();
+        JestResult result = client.execute(flush);
+        assertTrue(result.getErrorMessage(), result.isSucceeded());
+
+        IndicesStatsResponse statsResponse = client().admin().indices().stats(
+                new IndicesStatsRequest().clear().flush(true).refresh(true)).actionGet();
+        assertNotNull(statsResponse);
+        IndexStats stats1 = statsResponse.getIndex(INDEX_NAME);
+
+        assertEquals("There should be exactly one flush operation per shard performed on this index",
+                stats1.getShards().length, stats1.getTotal().getFlush().getTotal());
+    }
+
+    @Test
+    public void testFlushWithWaitifOngoing() throws InterruptedException, ExecutionException, TimeoutException, IOException {
+        Flush flush = new Flush.Builder().waitIfOngoing().build();
+        JestResult result = client.execute(flush);
+        assertTrue(result.getErrorMessage(), result.isSucceeded());
+
+        IndicesStatsResponse statsResponse = client().admin().indices().stats(
+                new IndicesStatsRequest().clear().flush(true).refresh(true)).actionGet();
+        assertNotNull(statsResponse);
+        IndexStats stats1 = statsResponse.getIndex(INDEX_NAME);
+
+        assertEquals("There should be exactly one flush operation per shard performed on this index",
+                stats1.getShards().length, stats1.getTotal().getFlush().getTotal());
+    }
+
     @Test
     public void testFlushSpecificIndices() throws InterruptedException, ExecutionException, TimeoutException, IOException {
         Flush flush = new Flush.Builder().addIndex(INDEX_NAME_2).addIndex(INDEX_NAME_3).build();