# How to deal with date type properties in elasticsearch

**URL:** <https://discuss.elastic.co/t/how-to-deal-with-date-type-properties-in-elasticsearch/135327>\
**Category:** Elasticsearch\
**Created:** [June 11, 2018, 9:53am UTC](https://discuss.elastic.co/t/how-to-deal-with-date-type-properties-in-elasticsearch/135327 "2018-06-11T09:53:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SharmaPrakhar25](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@SharmaPrakhar25](https://discuss.elastic.co/u/SharmaPrakhar25)\
**Post date:** [June 11, 2018, 9:53am UTC](https://discuss.elastic.co/t/how-to-deal-with-date-type-properties-in-elasticsearch/135327/1 "2018-06-11T09:53:44Z")

</div>

I am very new to elasticsearch and implementing elasticsearch in php-laravel based API using elasticsearch-php client. I am saving 2 datetime fields (created\_on, modified\_on) from the sql db which I want to index in problem document. Ar first I didn't give date format for properties in mapping.

```
        'index' => 'newproblemindex',
        'type' => 'newproblems2',
        'body' => [
            'newproblems2' => [
                '_source' => [
                    'enabled' => true
                ],
                'properties' => [
                    'subject' => [
                        'type' => 'text',
                        'analyzer' => 'standard',
                    ],
                    'notes' => [
                        'type' => 'text',
                        'analyzer' => 'standard'
                    ],
                    'created_on' => [
                        'type' => 'date',
                    ],
                    'modified_on' => [
                        'type' => 'date',
                    ],
                    'created_by' => [
                        'type' => 'integer',
                    ],
                    'modified_by' => [
                        'type' => 'integer',
                    ],
                    'status_enum' => [
                        'type' => 'integer',
                    ],
                    'rating' => [
                        'type' => 'integer',
                    ]
                 ]
            ]
        ]
    ];

```

so when I tried to index my document I got the error  
**Elasticsearch\Common\Exceptions\BadRequest400Exception: {"error":{"root\_cause":[{"type":"mapper\_parsing\_exception","reason":"failed to parse [created\_on]"}],"type":"mapper\_parsing\_exception","reason":"failed to parse [created\_on]","caused\_by":{"type":"illegal\_state\_exception","reason":"Can't get text on a START\_OBJECT at 1:100"}},"status":400} in file /var/www/development/api/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php on line 615**

So I tried to update the mapping using put mapping

```
        'index' => 'newproblemindex',
        'type' => 'newproblems2',
        'body' => [
            'newproblems2' => [
                '_source' => [
                    'enabled' => true
                ],
                'properties' => [
                    'subject' => [
                        'type' => 'text',
                        'analyzer' => 'standard',
                    ],
                    'notes' => [
                        'type' => 'text',
                        'analyzer' => 'standard'
                    ],
                    'created_on' => [
                        'type' => 'date',
                        'format' => "Y-m-d H:i:s"
                    ],
                    'modified_on' => [
                        'type' => 'date',
                        'format' => "Y-m-d H:i:s"
                    ],
                    'created_by' => [
                        'type' => 'integer',
                    ],
                    'modified_by' => [
                        'type' => 'integer',
                    ],
                    'status_enum' => [
                        'type' => 'integer',
                    ],
                    'rating' => [
                        'type' => 'integer',
                    ]

                ]
            ]
        ]
    ];

```

but now I am getting error  
**{"error":{"root\_cause":[{"type":"illegal\_argument\_exception","reason":"Invalid format: [Y-m-d H:i:s]: Illegal pattern component: i"}],"type":"illegal\_argument\_exception","reason":"Invalid format: [Y-m-d H:i:s]: Illegal pattern component: i","caused\_by":{"type":"illegal\_argument\_exception","reason":"Illegal pattern component: i"}},"status":400}**

how can I remove the error what am I doing wrong, any help would be appreciated  
Thanks for your help in advance

---

<div class="post-metadata">

**Author:** ![mjunaidmuzammil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mjunaidmuzammil/32/56910_2.png) [@mjunaidmuzammil](https://discuss.elastic.co/u/mjunaidmuzammil)\
**Post date:** [June 11, 2018, 10:27am UTC](https://discuss.elastic.co/t/how-to-deal-with-date-type-properties-in-elasticsearch/135327/2 "2018-06-11T10:27:23Z")

</div>

> [@SharmaPrakhar25](#):
>
> I am very new to elasticsearch and implementing elasticsearch in php-laravel based API using elasticsearch-php client. I am saving 2 datetime fields (created\_on, modified\_on) from the sql db which I want to index in problem document.

Once mappings are created they cannot be altered. Existing mappings can only be changed through reindexing.

---

<div class="post-metadata">

**Author:** ![SharmaPrakhar25](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@SharmaPrakhar25](https://discuss.elastic.co/u/SharmaPrakhar25)\
**Post date:** [June 11, 2018, 10:38am UTC](https://discuss.elastic.co/t/how-to-deal-with-date-type-properties-in-elasticsearch/135327/3 "2018-06-11T10:38:30Z")

</div>

I tried reindexing but the problem still remains

---

<div class="post-metadata">

**Author:** ![mjunaidmuzammil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mjunaidmuzammil/32/56910_2.png) [@mjunaidmuzammil](https://discuss.elastic.co/u/mjunaidmuzammil)\
**Post date:** [June 11, 2018, 10:54am UTC](https://discuss.elastic.co/t/how-to-deal-with-date-type-properties-in-elasticsearch/135327/4 "2018-06-11T10:54:24Z")

</div>

The second error is infact due to incorrect format specification for date type. Try the builtin formats or your own custom formats by following the guide here.

[https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html#custom-date-formats](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html#custom-date-formats)

Can you share the JSON document which you are indexing?

---

<div class="post-metadata">

**Author:** ![SharmaPrakhar25](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@SharmaPrakhar25](https://discuss.elastic.co/u/SharmaPrakhar25)\
**Post date:** [June 12, 2018, 9:45am UTC](https://discuss.elastic.co/t/how-to-deal-with-date-type-properties-in-elasticsearch/135327/5 "2018-06-12T09:45:00Z")

</div>

I am facing problem when I am trying to assigning this date format ---- 2018-06-12 09:42:30.920889  
[created\_on] =\> DateTime Object  
(  
 =\> 2018-06-12 09:43:56.216460  
[timezone\_type] =\> 3  
[timezone] =\> UTC  
)

```
        [created_by] => 4
        [status_enum] => 2
        [subject] => Elasticsearch Problem_12thJune
        [notes] => I am submitting problem in elasticsearch
        [id] => 1096
```

---

<div class="post-metadata">

**Author:** ![SharmaPrakhar25](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@SharmaPrakhar25](https://discuss.elastic.co/u/SharmaPrakhar25)\
**Post date:** [June 12, 2018, 9:47am UTC](https://discuss.elastic.co/t/how-to-deal-with-date-type-properties-in-elasticsearch/135327/6 "2018-06-12T09:47:07Z")

</div>

I have put new mappings and used this format "format"=\> "yyyy-MM-dd HH:mm:ss.SSSZ", for date type fields but now I am getting this error

Elasticsearch\Common\Exceptions\BadRequest400Exception: {"error":{"root\_cause":[{"type":"mapper\_parsing\_exception","reason":"failed to parse [created\_on]"}],"type":"mapper\_parsing\_exception","reason":"failed to parse [created\_on]","caused\_by":{"type":"illegal\_argument\_exception","reason":"Invalid format: &quot;2018-06-12 09:45:29.960449&quot; is malformed at &quot;449&quot;"}},"status":400} in file /var/www/development/api/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php on line 615

---

<div class="post-metadata">

**Author:** ![mjunaidmuzammil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mjunaidmuzammil/32/56910_2.png) [@mjunaidmuzammil](https://discuss.elastic.co/u/mjunaidmuzammil)\
**Post date:** [June 13, 2018, 8:14am UTC](https://discuss.elastic.co/t/how-to-deal-with-date-type-properties-in-elasticsearch/135327/7 "2018-06-13T08:14:09Z")

</div>

@SharmaPrakhar25 This is because your pattern doesn't match the format that you have specified in your mappings. For your current date formats (`2018-06-12 09:45:29.960449`) use the following mapping.

`{"properties": {"created_on": {"type": "date", "format": "yyyy-MM-dd HH:mm:ss.SSSSSS"}}}`

---

<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 11, 2018, 8:14am UTC](https://discuss.elastic.co/t/how-to-deal-with-date-type-properties-in-elasticsearch/135327/8 "2018-07-11T08:14:18Z")

</div>

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