# Date Dynamic Mapping

**URL:** https://discuss.elastic.co/t/date-dynamic-mapping/232981
**Category:** Elasticsearch
**Created:** [May 17, 2020, 9:58am UTC](https://discuss.elastic.co/t/date-dynamic-mapping/232981 "2020-05-17T09:58:50Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![AsfK](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/asfk/32/58995_2.png) [@AsfK](https://discuss.elastic.co/u/AsfK)
#### Post date: [May 17, 2020, 9:58am UTC](https://discuss.elastic.co/t/date-dynamic-mapping/232981/1 "2020-05-17T09:58:50Z")

</div>

Hello!

I using ES High rest api to upload json as document, no template is provide.  
I want to make an index pattern by date, therefore I added a 'time' field contains time in various formats but it recognized as string in the index pattern window.

I tried some formats:

- 04-06-2020 12:21:42
- 04-06-2020T12:21:42
- 04-06-2020T12:21:42Z
- 04062020T122143Z
- Epoch nanoseconds

I also tried to add "Scaled date format" in the advanced settings: `["P1TDF", "YYYY-MM-DD HH:mm:ss"]`:

![image](https://us1.discourse-cdn.com/elastic/original/3X/f/7/f730a470de24785a81170b559f6381720c149ab5.png)  
(not really understand the first key, just add format with full time, ss; UPDATE: I also added `MM-DD-YYYY HH:mm:ss`).

but 'time' field still recognized as string:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/4/2/428cfd8feeaf1b9a12ae3cf589972342bcc86906.png)

(I also haven't date option in the edit page).

How can I recognize this json field as date in dynamic mapping?

Thank you!

P.S. ES 7.6.2

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [May 17, 2020, 10:50am UTC](https://discuss.elastic.co/t/date-dynamic-mapping/232981/2 "2020-05-17T10:50:36Z")

</div>

The best practice is to define the mapping before indexing the first document so you force the field as a date.

Why not doing that?

---

<div class="post-metadata">

### Author: ![AsfK](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/asfk/32/58995_2.png) [@AsfK](https://discuss.elastic.co/u/AsfK)
#### Post date: [May 17, 2020, 12:31pm UTC](https://discuss.elastic.co/t/date-dynamic-mapping/232981/3 "2020-05-17T12:31:04Z")

</div>

Thank you very much, I thought it's not needed and ES know to recognized date as string...

(Avoid templates to not define the mapping in new environments...)

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [May 17, 2020, 3:28pm UTC](https://discuss.elastic.co/t/date-dynamic-mapping/232981/4 "2020-05-17T15:28:25Z")

</div>

It can. But I do see that as a bad practice. In my former job, I had a field in which users can enter whatever reference they want to use.  
The first user in my app, entered "2012-01-01". The second one entered "foo".  
The problem is that the first one was looking like a date so Elasticsearch set to this field the date data type. Obviously the second document was rejected as "foo" can't be a date.

That's why I recommend using explicit mapping.

---

<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: [June 14, 2020, 3:28pm UTC](https://discuss.elastic.co/t/date-dynamic-mapping/232981/5 "2020-06-14T15:28:25Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
