title: ElasticSearch index exists not working / reliable
body: <p>I am writing a simple Java wrapper around ElasticSearch's admin client. To test it I have a main method that first checks if an index exists (IndicesExistsRequest), if so deletes it (DeleteIndexRequest), and creates the index again. See code below. Yet I consistently get an IndexAlreadyExistsException.</p>
  <p>By the way I am trying to get a client for the node that you start from the command prompt (by simply typing &quot;elastic search&quot;). I have tried every combination of methods on nodeBuilder's fluent interface, but I can't seem to get one.</p>
  <pre><code>public static void main(String[] args) {
      ElasticSearchJavaClient esjc = new ElasticSearchJavaClient(&quot;nda&quot;);
      if (esjc.indexExists()) {
          esjc.deleteIndex();
      }
      esjc.createIndex();
      URL url = SchemaCreator.class.getResource(&quot;/elasticsearch/specimen.type.json&quot;);
      String mappings = FileUtil.getContents(url);
      esjc.createType(&quot;specimen&quot;, mappings);
  }
  
  final Client esClient;
  final IndicesAdminClient adminClient;
  final String indexName;
  
  public ElasticSearchJavaClient(String indexName) {
      this.indexName = indexName;
      esClient = nodeBuilder().clusterName(&quot;elasticsearch&quot;).client(true).node().client();
      adminClient = esClient.admin().indices();
  }
  
  public boolean deleteIndex() {
      logger.info(&quot;Deleting index &quot; + indexName);
      DeleteIndexRequest request = new DeleteIndexRequest(indexName);
      try {
          DeleteIndexResponse response = adminClient.delete(request).actionGet();
          if (!response.isAcknowledged()) {
              throw new Exception(&quot;Failed to delete index &quot; + indexName);
          }
          logger.info(&quot;Index deleted&quot;);
          return true;
      } catch (IndexMissingException e) {
          logger.info(&quot;No such index: &quot; + indexName);
          return false;
      }
  }
  
  public boolean indexExists() {
      logger.info(String.format(&quot;Verifying existence of index \&quot;%s\&quot;&quot;, indexName));
      IndicesExistsRequest request = new IndicesExistsRequest(indexName);
      IndicesExistsResponse response = adminClient.exists(request).actionGet();
      if (response.isExists()) {
          logger.info(&quot;Index exists&quot;);
          return true;
      }
      logger.info(&quot;No such index&quot;);
      return false;
  }
  
  public void createIndex() {
      logger.info(&quot;Creating index &quot; + indexName);
      CreateIndexRequest request = new CreateIndexRequest(indexName);
      IndicesAdminClient iac = esClient.admin().indices();
      CreateIndexResponse response = iac.create(request).actionGet();
      if (!response.isAcknowledged()) {
          throw new Exception(&quot;Failed to create index &quot; + indexName);
      }
      logger.info(&quot;Index created&quot;);
  }
  </code></pre>