title: Setting Time To Live (TTL) from Java - sample requested
body: <p><strong>EDIT:</strong>
  <a href="http://comments.gmane.org/gmane.comp.search.elasticsearch.user/5446" rel="nofollow">This is basically what I want to do, only in Java</a></p>
  
  <p>Using ElasticSearch, we add documents to an index bypassing IndexRequest items to a BulkRequestBuilder.</p>
  
  <p>I would like for the documents to be dropped from the index after some time has passed (time to live/ttl)</p>
  
  <p>This can be done either by setting a default for the index, or on a per-document basis. <strong>Either approach is fine by me</strong>.</p>
  
  <p>The code below is an attempt to do it per document. It does not work. I think it's because TTL is not enabled for the index. <strong>Either show me what Java code I need to add to enable TTL so the code below works, or show me different code that enables TTL + sets default TTL value for the index in Java</strong> I know <a href="http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-ttl-field.html#_enabled_2" rel="nofollow">how to do it from the REST API</a> but I need to do it from Java code, if at all possible.</p>
  
  <pre><code>    logger.debug("Indexing record ({}): {}", id, map);
      final IndexRequest indexRequest = new IndexRequest(_indexName, _documentType, id);
      final long debug = indexRequest.ttl();
      if (_ttl &gt; 0) {
          indexRequest.ttl(_ttl);
          System.out.println("Setting TTL to " + _ttl);
          System.out.println("IndexRequest now has ttl of " + indexRequest.ttl());
      }
      indexRequest.source(map);
      indexRequest.operationThreaded(false);
      bulkRequestBuilder.add(indexRequest);
  }
  
  // execute and block until done.
  BulkResponse response;
  try {
      response = bulkRequestBuilder.execute().actionGet();
  </code></pre>
  
  <p>Later I check in my unit test by polling this method, but the document count never goes down.</p>
  
  <pre><code>public long getDocumentCount() throws Exception {
      Client client = getClient();
      try {
          client.admin().indices().refresh(new RefreshRequest(INDEX_NAME)).actionGet();
  
          ActionFuture&lt;CountResponse&gt; response = client.count(new CountRequest(INDEX_NAME).types(DOCUMENT_TYPE));
          CountResponse countResponse = response.get();
          return countResponse.getCount();
      } finally {
          client.close();
      }
  }
  </code></pre>