# Generate dynamic date for ES Index

**URL:** <https://discuss.elastic.co/t/generate-dynamic-date-for-es-index/35797>\
**Category:** Logstash\
**Created:** [November 28, 2015, 7:44am UTC](https://discuss.elastic.co/t/generate-dynamic-date-for-es-index/35797 "2015-11-28T07:44:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mvenkat\_in](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@mvenkat\_in](https://discuss.elastic.co/u/mvenkat_in)\
**Post date:** [November 28, 2015, 7:44am UTC](https://discuss.elastic.co/t/generate-dynamic-date-for-es-index/35797/1 "2015-11-28T07:44:15Z")

</div>

Hi  
Below is the simple ES output which indexes to e2e-YYYY.MM.dd index in ES. i.e., of the Today's date.  
output {  
elasticsearch {  
hosts =\> "host:9200"  
index =\> "e2e-%{+YYYY.MM.dd}"  
document\_type =\> "log"  
}  
}

However I want the Logstash to generate the date or yesterday's date (for example trunc(sysdate)-1) and specify the ES Index accordingly.

And similarly would like to search for day-1 timestamped log files in the file input.

How Can I generate the same.

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [November 28, 2015, 11:50am UTC](https://discuss.elastic.co/t/generate-dynamic-date-for-es-index/35797/2 "2015-11-28T11:50:37Z")

</div>

You could generate a field (under `@metadata` so it isn't included in the document sent to ES) with whatever date you like and refer to that in the `index` setting. But why? What problem are you trying to solve by doing this?

---

<div class="post-metadata">

**Author:** ![mvenkat\_in](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@mvenkat\_in](https://discuss.elastic.co/u/mvenkat_in)\
**Post date:** [November 28, 2015, 1:47pm UTC](https://discuss.elastic.co/t/generate-dynamic-date-for-es-index/35797/3 "2015-11-28T13:47:00Z")

</div>

Hi OK I would have to little bit research on how to do it with @metadata.

My Use case is - I would require to ship all the day wise logs of various applications to ES and planning to have day wise Index. So for instance e2e-28-11-2015 index would have all the logs of the transactions of 28th Nov 15 and so on.

The problem statement is for one of the applications I would need to collect the data/logs from the mirror database the next day i.e., on 29th Nov 15 the logstash should run to fetch 28th Nov 15 transactions from that application, hence I would need these data to be indexed to 28-11-2015. Therefore need the logic in logstash to get the "SYSDATE-1' to construct the index name.

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [November 28, 2015, 4:46pm UTC](https://discuss.elastic.co/t/generate-dynamic-date-for-es-index/35797/4 "2015-11-28T16:46:56Z")

</div>

Just use the [date filter](https://www.elastic.co/guide/en/logstash/current/plugins-filters-date.html) to parse the timestamps from the logs and populate the `@timestamp` field with the actual time each event occurred. Then logs from Nov 28 will end up in e2e-2015.11.28 regardless of when you let Logstash process the logs (but keep in mind that `@timestamp` is UTC so depending on your timezone you might see messages from Nov 28 in your local timezone end up in either e2e-2015.11.27 or e2e-2015.11.29.

---

<div class="post-metadata">

**Author:** ![mvenkat\_in](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@mvenkat\_in](https://discuss.elastic.co/u/mvenkat_in)\
**Post date:** [November 29, 2015, 12:03am UTC](https://discuss.elastic.co/t/generate-dynamic-date-for-es-index/35797/5 "2015-11-29T00:03:51Z")

</div>

Thanks Magnus. I thought of the same as well to parse one of the event date field and add to metadata.

---

<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, 5:20am UTC](https://discuss.elastic.co/t/generate-dynamic-date-for-es-index/35797/6 "2017-07-06T05:20:55Z")

</div>


