InitIndex: Set Elasticsearch index config under elasticsearch section

When initializing the site with elasticsearch secondary index, the
elasticsearch prefix should be under a section named "elasticsearch",
and the server-specific configuration should be under a subsection
of "elasticsearch" with the server name as subsection name:

  [index]
        type = ELASTICSEARCH
  [elasticsearch]
        prefix = gerrit
  [elasticsearch "default"]
        protocol = http
        hostname = localhost
        port = 9200

rather than all under the "index" section:

  [index]
        type = ELASTICSEARCH
        protocol = http
        hostname = localhost
        port = 9200
        name = local

Bug: Issue 8523
Change-Id: I544769f25876735249404eb1c7eabece31ab3660
diff --git a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/InitIndex.java b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/InitIndex.java
index 0d9a822..801f695 100644
--- a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/InitIndex.java
+++ b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/InitIndex.java
@@ -40,6 +40,7 @@
   private final SitePaths site;
   private final InitFlags initFlags;
   private final Section gerrit;
+  private final Section.Factory sections;
 
   @Inject
   InitIndex(ConsoleUI ui, Section.Factory sections, SitePaths site, InitFlags initFlags) {
@@ -48,6 +49,7 @@
     this.gerrit = sections.get("gerrit", null);
     this.site = site;
     this.initFlags = initFlags;
+    this.sections = sections;
   }
 
   @Override
@@ -59,10 +61,15 @@
     }
 
     if (type == IndexType.ELASTICSEARCH) {
-      index.select("Transport protocol", "protocol", "http", Sets.newHashSet("http", "https"));
-      index.string("Hostname", "hostname", "localhost");
-      index.string("Port", "port", "9200");
-      index.string("Index Name", "name", "gerrit");
+      Section elasticsearch = sections.get("elasticsearch", null);
+      elasticsearch.string("Index Prefix", "prefix", "gerrit");
+      String name = ui.readString("default", "Server Name");
+
+      Section defaultServer = sections.get("elasticsearch", name);
+      defaultServer.select(
+          "Transport protocol", "protocol", "http", Sets.newHashSet("http", "https"));
+      defaultServer.string("Hostname", "hostname", "localhost");
+      defaultServer.string("Port", "port", "9200");
     }
 
     if ((site.isNew || isEmptySite()) && type == IndexType.LUCENE) {