# Dynamic mapping of date fields present in epoch\_millis format

**URL:** https://discuss.elastic.co/t/dynamic-mapping-of-date-fields-present-in-epoch-millis-format/168575
**Category:** Elasticsearch
**Created:** [February 15, 2019, 11:44am UTC](https://discuss.elastic.co/t/dynamic-mapping-of-date-fields-present-in-epoch-millis-format/168575 "2019-02-15T11:44:17Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Nikesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikesh/32/86579_2.png) [@Nikesh](https://discuss.elastic.co/u/Nikesh)
#### Post date: [February 15, 2019, 11:44am UTC](https://discuss.elastic.co/t/dynamic-mapping-of-date-fields-present-in-epoch-millis-format/168575/1 "2019-02-15T11:44:17Z")

</div>

Hi,

I was observing how date fields can be dynamically mapped.In my use case, fields that to be mapped as date are in epoch\_millis format.  
I have a dynamic template set for date field as :

```
{
          "dates": {
            "match_mapping_type": "date",
                       "mapping": {
              "type": "date",
              "dynamic_date_formats" : "epoch_millis",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              },
                        "copy_to": [
                            "COMMON_FIELD"
                        ]
            }
          }
        }

```

1. While indexing a new field present in epoch\_millis format within double quotes, this is considered as string.

2. While indexing the same value in another new field present in same format(epoch\_millis) without double quotes, this is considered as an integer and throws an mapper parsing exception  
as this number is out of range of int.

How do I ensure these values are taken as date rather than string and integer? Please note, I only receive values in epoch\_millis format?

---

<div class="post-metadata">

### Author: ![Nikesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikesh/32/86579_2.png) [@Nikesh](https://discuss.elastic.co/u/Nikesh)
#### Post date: [February 19, 2019, 1:59pm UTC](https://discuss.elastic.co/t/dynamic-mapping-of-date-fields-present-in-epoch-millis-format/168575/2 "2019-02-19T13:59:15Z")

</div>

@elastic Please provide a suggestion

---

<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: [February 19, 2019, 2:13pm UTC](https://discuss.elastic.co/t/dynamic-mapping-of-date-fields-present-in-epoch-millis-format/168575/3 "2019-02-19T14:13:04Z")

</div>

Why not using a match field name pattern instead? [https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-templates.html#match-unmatch](https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-templates.html#match-unmatch)

---

<div class="post-metadata">

### Author: ![Nikesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikesh/32/86579_2.png) [@Nikesh](https://discuss.elastic.co/u/Nikesh)
#### Post date: [February 19, 2019, 2:37pm UTC](https://discuss.elastic.co/t/dynamic-mapping-of-date-fields-present-in-epoch-millis-format/168575/4 "2019-02-19T14:37:17Z")

</div>

Thanks @dadoonet for the response

I did use match field pattern too. Although this didn't work in my case.

`{ "date_field1":"1536517800000" }` was taken as text where as `{ "date_field1":1536517800000 }` was taken as long.

However, I can't completely depend on this match field pattern.

Does not Elasticsearch handle text present in epoch\_millis as a part of dynamic mapping ?

---

<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: [February 19, 2019, 2:37pm UTC](https://discuss.elastic.co/t/dynamic-mapping-of-date-fields-present-in-epoch-millis-format/168575/5 "2019-02-19T14:37:40Z")

</div>

> [@Nikesh](#):
>
> I did use match field pattern too. Although this didn't work in my case.

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

---

<div class="post-metadata">

### Author: ![Nikesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikesh/32/86579_2.png) [@Nikesh](https://discuss.elastic.co/u/Nikesh)
#### Post date: [February 19, 2019, 2:48pm UTC](https://discuss.elastic.co/t/dynamic-mapping-of-date-fields-present-in-epoch-millis-format/168575/6 "2019-02-19T14:48:56Z")

</div>

Hi @dadoonet  
My Bad.  
match field pattern did work fine.

My mapping :

```
 { "mappings": {
            "_doc": {
                "dynamic_templates": [
                    {
                        "date": {
                           "match": "*_date_*",
                            "mapping": {
                                "fields": {
                                    "keyword": {
                                        "ignore_above": 256,
                                        "type": "keyword"
                                    }
                                },
                                
                                "type": "date"
                            }
                        }
                    }
                ]
            }
        }
 }

```

Data indexed :

```
{
	
	"a_date_bc":1527359400000
}

```

Another data

```
{
	
	"a_date_bcc":"1527359400000"
}

```

GET \_mapping

```
 "a_date_bc": {
                        "type": "date",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "a_date_bcc": {
                        "type": "date",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }

```

Thanks for your suggestion @dadoonet

However sometimes my field names may not always match a particular pattern.  
Also, my other fields might match this pattern and might not actually contain a date.  
How do I overcome these issues?

---

<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: [February 19, 2019, 3:21pm UTC](https://discuss.elastic.co/t/dynamic-mapping-of-date-fields-present-in-epoch-millis-format/168575/7 "2019-02-19T15:21:34Z")

</div>

If you have fields like `foo` that might have a value like `1527359400000` and that might be a date, well... `0` looks like a valid date to me as well.

There is not really something you can do. It can be a number or a date. Who can know?

I mean that either you add some constraints to the sender and ask him to send dates with a prefix in the field name, like `dt*` or `a_date_` or you try to do with best effort with the risk of rejecting lot of documents. You can try this though: [https://www.elastic.co/guide/en/elasticsearch/reference/current/ignore-malformed.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/ignore-malformed.html)

---

<div class="post-metadata">

### Author: ![Nikesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikesh/32/86579_2.png) [@Nikesh](https://discuss.elastic.co/u/Nikesh)
#### Post date: [February 19, 2019, 3:31pm UTC](https://discuss.elastic.co/t/dynamic-mapping-of-date-fields-present-in-epoch-millis-format/168575/8 "2019-02-19T15:31:35Z")

</div>

okay! Thanks @dadoonet for quick solutions

---

<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: [March 19, 2019, 3:31pm UTC](https://discuss.elastic.co/t/dynamic-mapping-of-date-fields-present-in-epoch-millis-format/168575/9 "2019-03-19T15:31:37Z")

</div>

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