From fb1d1ab3a9035c4dddfe3d5574bc9c4850b1f78e Mon Sep 17 00:00:00 2001
From: Jason Tedor <jason@tedor.me>
Date: Wed, 6 Apr 2016 09:46:27 -0400
Subject: [PATCH] [elasticsearch] Fix and cleanup index creation

This commit fixes an issue with the index creation logic when the
elasticsearch.newdb flag is set to true. Namely, when running in local
mode the index would never exist (since the node always starts with a
clean temporary directory) but with elasticsearch.newdb an attempt would
be made to delete the index which would cause the workload to fail.

This commit also does a cleanup of the index creation logic using an
explicit create index request and passing the index settings along as
part of that index creation.
---
 .../yahoo/ycsb/db/ElasticsearchClient.java    | 33 ++++++++++---------
 1 file changed, 17 insertions(+), 16 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 888c9d62..c91ba171 100644
--- a/elasticsearch/src/main/java/com/yahoo/ycsb/db/ElasticsearchClient.java
+++ b/elasticsearch/src/main/java/com/yahoo/ycsb/db/ElasticsearchClient.java
@@ -30,6 +30,7 @@ import com.yahoo.ycsb.Status;
 import com.yahoo.ycsb.StringByteIterator;
 
 import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
+import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
 import org.elasticsearch.action.get.GetResponse;
 import org.elasticsearch.action.search.SearchResponse;
 import org.elasticsearch.client.Client;
@@ -94,9 +95,6 @@ public class ElasticsearchClient extends DB {
     Builder settings = Settings.settingsBuilder()
         .put("node.local", "true")
         .put("path.data", System.getProperty("java.io.tmpdir") + "/esdata")
-        .put("index.mapping._id.indexed", "true")
-        .put("index.number_of_shards", "1")
-        .put("index.number_of_replicas", "0")
         .put("path.home", System.getProperty("java.io.tmpdir"));
 
     // if properties file contains elasticsearch user defined properties
@@ -141,20 +139,23 @@ public class ElasticsearchClient extends DB {
       client = node.client();
     }
 
-    //wait for shards to be ready
-    client.admin().cluster()
-      .health(new ClusterHealthRequest("lists").waitForActiveShards(1))
-      .actionGet();
-    if (newdb) {
+    final boolean exists =
+            client.admin().indices()
+                    .exists(Requests.indicesExistsRequest(indexKey)).actionGet()
+                    .isExists();
+    if (exists && newdb) {
       client.admin().indices().prepareDelete(indexKey).execute().actionGet();
-      client.admin().indices().prepareCreate(indexKey).execute().actionGet();
-    } else {
-      boolean exists = client.admin().indices()
-          .exists(Requests.indicesExistsRequest(indexKey)).actionGet()
-          .isExists();
-      if (!exists) {
-        client.admin().indices().prepareCreate(indexKey).execute().actionGet();
-      }
+    }
+    if (!exists || newdb) {
+      client.admin().indices().create(
+              new CreateIndexRequest(indexKey)
+                      .settings(
+                              Settings.builder()
+                                      .put("index.number_of_shards", 1)
+                                      .put("index.number_of_replicas", 0)
+                                      .put("index.mapping._id.indexed", true)
+                      )).actionGet();
+      client.admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet();
     }
   }
 
-- 
GitLab