From f5f19d8645bee0d941401a754492e1ac62d22a22 Mon Sep 17 00:00:00 2001
From: Jason Tedor <jason@tedor.me>
Date: Wed, 6 Apr 2016 10:14:58 -0400
Subject: [PATCH] [elasticsearch] Settings for shards and replicas

---
 .../com/yahoo/ycsb/db/ElasticsearchClient.java  | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/elasticsearch/src/main/java/com/yahoo/ycsb/db/ElasticsearchClient.java b/elasticsearch/src/main/java/com/yahoo/ycsb/db/ElasticsearchClient.java
index a92cd163..ca5e5e09 100644
--- a/elasticsearch/src/main/java/com/yahoo/ycsb/db/ElasticsearchClient.java
+++ b/elasticsearch/src/main/java/com/yahoo/ycsb/db/ElasticsearchClient.java
@@ -60,6 +60,8 @@ import java.util.Vector;
  * <ul>
  * <li>cluster.name = es.ycsb.cluster
  * <li>es.index.key = es.ycsb
+ * <li>elasticsearch.number_of_shards = 1
+ * <li>elasticsearch.number_of_replicas = 0
  * </ul>
  */
 public class ElasticsearchClient extends DB {
@@ -67,6 +69,8 @@ public class ElasticsearchClient extends DB {
   private static final String DEFAULT_CLUSTER_NAME = "es.ycsb.cluster";
   private static final String DEFAULT_INDEX_KEY = "es.ycsb";
   private static final String DEFAULT_REMOTE_HOST = "localhost:9300";
+  private static final int NUMBER_OF_SHARDS = 1;
+  private static final int NUMBER_OF_REPLICAS = 0;
   private Node node;
   private Client client;
   private String indexKey;
@@ -82,6 +86,10 @@ public class ElasticsearchClient extends DB {
     this.indexKey = props.getProperty("es.index.key", DEFAULT_INDEX_KEY);
     String clusterName =
         props.getProperty("cluster.name", DEFAULT_CLUSTER_NAME);
+
+    int numberOfShards = parseIntegerProperty(props, "elasticsearch.number_of_shards", NUMBER_OF_SHARDS);
+    int numberOfReplicas = parseIntegerProperty(props, "elasticsearch.number_of_replicas", NUMBER_OF_REPLICAS);
+
     // Check if transport client needs to be used (To connect to multiple
     // elasticsearch nodes)
     remoteMode = Boolean
@@ -146,14 +154,19 @@ public class ElasticsearchClient extends DB {
               new CreateIndexRequest(indexKey)
                       .settings(
                               Settings.builder()
-                                      .put("index.number_of_shards", 1)
-                                      .put("index.number_of_replicas", 0)
+                                      .put("index.number_of_shards", numberOfShards)
+                                      .put("index.number_of_replicas", numberOfReplicas)
                                       .put("index.mapping._id.indexed", true)
                       )).actionGet();
       client.admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet();
     }
   }
 
+  private int parseIntegerProperty(Properties properties, String key, int defaultValue) {
+    String value = properties.getProperty(key);
+    return value == null ? defaultValue : Integer.parseInt(value);
+  }
+
   @Override
   public void cleanup() throws DBException {
     if (!remoteMode) {
-- 
GitLab