# Using TTL and Timestamp

**URL:** <https://discuss.elastic.co/t/using-ttl-and-timestamp/7255>\
**Category:** Elasticsearch\
**Created:** [April 6, 2012, 7:33pm UTC](https://discuss.elastic.co/t/using-ttl-and-timestamp/7255 "2012-04-06T19:33:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ravi\_Dontharaju](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ravi_dontharaju/32/2929_2.png) [@Ravi\_Dontharaju](https://discuss.elastic.co/u/Ravi_Dontharaju)\
**Post date:** [April 6, 2012, 7:33pm UTC](https://discuss.elastic.co/t/using-ttl-and-timestamp/7255/1 "2012-04-06T19:33:01Z")

</div>

Hi,  
I am using the Java API to do my operations on ES.

I am trying to insert/use the \_ttl and \_timestamp features.  
The question that I had is, should I be setting the \_ttl and \_timestamp in the XContentBuilder source document that I pass to .setSource while building by IndexRequestBuilder?  
Or should I be setting my \_ttl and \_timestamp using the .setTTL(long) and .setTimestamp(String) methods while building my IndexRequestBuilder?

I tried both, and both did not remove my documents after a given while. The input for setTTL is long, and hence I am assuming that it accepts milliseconds, right?

Also, when I use .setTTL and .setTimestamp, while debugging, I can see that ttl and timestamp values have been updated for the request field under IndexRequestBuilder, but it does not reflect the fields in the document when I curl for it.

I am using Joda.DateTime.toString to add to the timestamp.

TL;DR  
How can I utilize/use \_ttl and \_timestamp using JavaAPI 🙂

Thanks,  
Ravi Dontharaju

---

<div class="post-metadata">

**Author:** ![Benjamin\_Deveze](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benjamin_deveze/32/1577_2.png) [@Benjamin\_Deveze](https://discuss.elastic.co/u/Benjamin_Deveze)\
**Post date:** [April 8, 2012, 3:03pm UTC](https://discuss.elastic.co/t/using-ttl-and-timestamp/7255/2 "2012-04-08T15:03:26Z")

</div>

Hey Ravi

Have you enabled ttl and timestamp fields in your mapping?

You should use setTTL and setTimestamp methods when using the java API.

Le vendredi 6 avril 2012, Ravi Dontharaju a écrit :

> Hi,  
> I am using the Java API to do my operations on ES.
> 
> I am trying to insert/use the \_ttl and \_timestamp features.  
> The question that I had is, should I be setting the \_ttl and \_timestamp in  
> the XContentBuilder source document that I pass to .setSource while  
> building  
> by IndexRequestBuilder?  
> Or should I be setting my \_ttl and \_timestamp using the .setTTL(long) and  
> .setTimestamp(String) methods while building my IndexRequestBuilder?
> 
> I tried both, and both did not remove my documents after a given while. The  
> input for setTTL is long, and hence I am assuming that it accepts  
> milliseconds, right?
> 
> Also, when I use .setTTL and .setTimestamp, while debugging, I can see that  
> ttl and timestamp values have been updated for the request field under  
> IndexRequestBuilder, but it does not reflect the fields in the document  
> when  
> I curl for it.
> 
> I am using Joda.DateTime.toString to add to the timestamp.
> 
> TL;DR  
> How can I utilize/use \_ttl and \_timestamp using JavaAPI 🙂
> 
> Thanks,  
> Ravi Dontharaju
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Using-TTL-and-Timestamp-tp3891197p3891197.html](http://elasticsearch-users.115913.n3.nabble.com/Using-TTL-and-Timestamp-tp3891197p3891197.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

--  
Benjamin DEVEZE

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 3:33am UTC](https://discuss.elastic.co/t/using-ttl-and-timestamp/7255/3 "2017-07-06T03:33:17Z")

</div>


