forked from searchbox-io/Jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request searchbox-io#523 from logzio/add-rollover-action
Add rollover action
- Loading branch information
Showing
3 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
jest-common/src/main/java/io/searchbox/indices/Rollover.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package io.searchbox.indices; | ||
|
||
import io.searchbox.action.AbstractAction; | ||
import io.searchbox.action.GenericResultAbstractAction; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Rollover extends GenericResultAbstractAction { | ||
|
||
protected Rollover(Rollover.Builder builder) { | ||
super(builder); | ||
|
||
this.indexName = builder.index; | ||
Map<String, Object> rolloverConditions = new HashMap<>(); | ||
if (builder.conditions != null) { | ||
rolloverConditions.put("conditions", builder.conditions); | ||
} | ||
if (builder.settings != null) { | ||
rolloverConditions.put("settings", builder.settings); | ||
} | ||
this.payload = rolloverConditions; | ||
|
||
setURI(buildURI() + (builder.isDryRun ? "?dry_run" : "")); | ||
} | ||
|
||
@Override | ||
public String getRestMethodName() { | ||
return "POST"; | ||
} | ||
|
||
@Override | ||
protected String buildURI() { | ||
return super.buildURI() + "/_rollover"; | ||
} | ||
|
||
|
||
public static class Builder extends AbstractAction.Builder<Rollover, Rollover.Builder> { | ||
|
||
private String index; | ||
private Object conditions; | ||
private Object settings; | ||
private boolean isDryRun; | ||
|
||
public Builder(String index) { | ||
this.index = index; | ||
} | ||
|
||
public Rollover.Builder conditions(Object conditions) { | ||
this.conditions = conditions; | ||
return this; | ||
} | ||
|
||
public Rollover.Builder setDryRun(boolean dryRun) { | ||
this.isDryRun = dryRun; | ||
return this; | ||
} | ||
|
||
public Rollover.Builder settings(Object settings) { | ||
this.settings = settings; | ||
return this; | ||
} | ||
|
||
@Override | ||
public Rollover build() { | ||
return new Rollover(this); | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
jest-common/src/test/java/io/searchbox/indices/RolloverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.searchbox.indices; | ||
|
||
import com.google.gson.Gson; | ||
import org.elasticsearch.common.collect.MapBuilder; | ||
import org.junit.Test; | ||
|
||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotEquals; | ||
|
||
public class RolloverTest { | ||
|
||
Map<String, Object> rolloverConditions = new MapBuilder<String, Object>() | ||
.put("max_docs", "10000") | ||
.put("max_age", "1d") | ||
.immutableMap(); | ||
Map<String, Object> rolloverSettings = new MapBuilder<String, Object>() | ||
.put("index.number_of_shards", "2") | ||
.immutableMap(); | ||
|
||
|
||
@Test | ||
public void testBasicUriGeneration() { | ||
Rollover rollover = new Rollover.Builder("twitter").conditions(rolloverConditions).build(); | ||
assertEquals("POST", rollover.getRestMethodName()); | ||
assertEquals("twitter/_rollover", rollover.getURI()); | ||
assertEquals("{\"conditions\":{\"max_age\":\"1d\",\"max_docs\":\"10000\"}}", rollover.getData(new Gson())); | ||
} | ||
|
||
@Test | ||
public void testBasicUriWithSettingsGeneration() { | ||
Rollover rollover = new Rollover.Builder("twitter").conditions(rolloverConditions).settings(rolloverSettings).build(); | ||
assertEquals("POST", rollover.getRestMethodName()); | ||
assertEquals("twitter/_rollover", rollover.getURI()); | ||
assertEquals("{\"settings\":{\"index.number_of_shards\":\"2\"},\"conditions\":{\"max_age\":\"1d\",\"max_docs\":\"10000\"}}", rollover.getData(new Gson())); | ||
} | ||
|
||
@Test | ||
public void testDryRunUriGeneration() { | ||
Rollover rollover = new Rollover.Builder("twitter").conditions(rolloverConditions).setDryRun(true).build(); | ||
assertEquals("POST", rollover.getRestMethodName()); | ||
assertEquals("twitter/_rollover?dry_run", rollover.getURI()); | ||
} | ||
|
||
@Test | ||
public void equalsReturnsTrueForSameDestination() { | ||
Rollover indexRollover1 = new Rollover.Builder("twitter").conditions(rolloverConditions).build(); | ||
Rollover indexRollover2 = new Rollover.Builder("twitter").conditions(rolloverConditions).build(); | ||
|
||
assertEquals(indexRollover1, indexRollover2); | ||
} | ||
|
||
@Test | ||
public void equalsReturnsFalseForDifferentIndex() { | ||
Rollover indexRollover1 = new Rollover.Builder("twitter").conditions(rolloverConditions).build(); | ||
Rollover indexRollover2 = new Rollover.Builder("myspace").conditions(rolloverConditions).build(); | ||
|
||
assertNotEquals(indexRollover1, indexRollover2); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
jest/src/test/java/io/searchbox/indices/RolloverIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.searchbox.indices; | ||
|
||
import io.searchbox.client.JestResult; | ||
import io.searchbox.common.AbstractIntegrationTest; | ||
import org.elasticsearch.common.collect.MapBuilder; | ||
import org.elasticsearch.test.ESIntegTestCase; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.SUITE, numDataNodes = 1) | ||
public class RolloverIntegrationTest extends AbstractIntegrationTest { | ||
|
||
private final Map<String, Object> rolloverConditions = new MapBuilder<String, Object>() | ||
.put("max_docs", "1") | ||
.put("max_age", "1d") | ||
.immutableMap(); | ||
|
||
@Test | ||
public void testRollover() throws IOException { | ||
String aliasSetting = "{ \"aliases\": { \"rollover-test-index\": {} } }"; | ||
CreateIndex createIndex = new CreateIndex.Builder("rollover-test-index-000001").settings(aliasSetting).build(); | ||
|
||
JestResult result = client.execute(createIndex); | ||
assertTrue(result.getErrorMessage(), result.isSucceeded()); | ||
|
||
Rollover rollover = new Rollover.Builder("rollover-test-index").conditions(rolloverConditions).build(); | ||
|
||
result = client.execute(rollover); | ||
assertTrue(result.getErrorMessage(), result.isSucceeded()); | ||
} | ||
} |