# Date Field with Millis stored as double leading to error

**URL:** <https://discuss.elastic.co/t/date-field-with-millis-stored-as-double-leading-to-error/277519>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [July 1, 2021, 7:01am UTC](https://discuss.elastic.co/t/date-field-with-millis-stored-as-double-leading-to-error/277519 "2021-07-01T07:01:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bertram](https://avatars.discourse-cdn.com/v4/letter/b/e8c25b/32.png) [@Bertram](https://discuss.elastic.co/u/Bertram)\
**Post date:** [July 1, 2021, 7:01am UTC](https://discuss.elastic.co/t/date-field-with-millis-stored-as-double-leading-to-error/277519/1 "2021-07-01T07:01:51Z")

</div>

Hey there,

I recently got this strange problem in a new index:  
Basic info:  
Elasticsearch Version: 7.13.2  
Nodes: 1 Node (localhost in Dev-Env)  
Middleware: [Elasticsearch-PHP] API-Wrapper

The index got a date field and a numeric field to count up with the explicit mapping:

```auto
//PHP-Syntax but should be readable
"click_count" => [
          "type" => "long",
],
"created" => [
          "type" => "date",
          "format" => "strict_date_optional_time||epoch_millis"
],

```

If a document gets indexed with a epochMillis value everything is fine.  
However if want to increase the counter by an update call with scripted update:

```auto
POST test/_update/sZwXXXoBfo59-9IusoIu
{
  "script" : {
    "source": "ctx._source.click_count += params.count",
    "lang": "painless",
    "params" : {
      "count" : 1
    }
  }
}

```

I am getting the following Exception:

..._"type":"mapper\_parsing\_exception","reason":"failed to parse field [created] of type [date] in document with id 'sZwXXXoBfo59-9IusoIu'. Preview of field's value: '1.625059471792E12'","caused\_by":{"type":"illegal\_argument\_exception","reason":"failed to parse date field [1.625059471792E12] with format [strict\_date\_optional\_time||epoch\_millis]"_...

If i use the following script it works:

```auto
POST test/_update/sZwXXXoBfo59-9IusoIu
{
  "script" : {
    "source": "ctx._source.click_count += params.count; if(ctx._source.created instanceof double) {ctx._source.created = (long)ctx._source.created}",
    "lang": "painless",
    "params" : {
      "count" : 1
    }
  }
}

```

I am wondering now is this the intended behaviour? Feels a bit hacky?  
What i am doing wrong? What is best practice here?

Thank you for your tipps and time.

Best Regards  
Bertram

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [July 1, 2021, 2:17pm UTC](https://discuss.elastic.co/t/date-field-with-millis-stored-as-double-leading-to-error/277519/2 "2021-07-01T14:17:23Z")

</div>

Can you share a sample document and how it is stored in Elasticsearch? You may want to store dates as strings in a human readable format, instead as epoch milliseconds/seconds...

---

<div class="post-metadata">

**Author:** ![Bertram](https://avatars.discourse-cdn.com/v4/letter/b/e8c25b/32.png) [@Bertram](https://discuss.elastic.co/u/Bertram)\
**Post date:** [July 2, 2021, 8:38am UTC](https://discuss.elastic.co/t/date-field-with-millis-stored-as-double-leading-to-error/277519/3 "2021-07-02T08:38:24Z")

</div>

Hey thank you for your reply,

This is an example document (there are some more fields, but those are obmitted as they don't make any difference)

```auto
{
   "click_count": 1,
   "created": 1625061253285
}

```

If I get this indexed document:

```auto
{
   "_index": "myindex",
   "_type": "_doc",
    //...
    "_id": "spwyXXoBfo59-9Iu4YIO",
    "_source": {
        "click_count": 1,
        "created": 1625061253285
    }
}

```

If i use String based Date Values instead, everything works fine on update and no error occurs in any case:

```auto
{
   "click_count": 1,
   "created": "2021-07-01T09:25:10.129+02:00"
}

```

If I get this indexed document:

```auto
{
   "_index": "myindex",
   "_type": "_doc",
    //...
    "_id": "spwyXXoBfo59-9Iu4YIO",
    "_source": {
        "click_count": 1,
        "created": "2021-07-01T09:25:10.129+02:00"
    }
}

```

You are right, human readable date may be better solution and i may keep it anyway. However i want to understand why elasticsearch fails to reindex a document with epochMilli date field value, without the special treatment on updates.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [July 6, 2021, 3:54pm UTC](https://discuss.elastic.co/t/date-field-with-millis-stored-as-double-leading-to-error/277519/4 "2021-07-06T15:54:18Z")

</div>

So I tried the below on 7.13.1 and it worked for me

```auto
DELETE test

PUT test 
{
  "mappings": {
    "properties": {
      "created" : {
        "type": "date",
        "format": "strict_date_optional_time||epoch_millis"
      }
    }
  }
}

PUT test/_doc/1
{
   "click_count": 1,
   "created": 1625061253285
}

POST test/_update/1
{
  "script" : {
    "source": "ctx._source.click_count += params.count; if(ctx._source.created instanceof double) {ctx._source.created = (long)ctx._source.created}",
    "lang": "painless",
    "params" : {
      "count" : 1
    }
  }
}

GET test/_doc/1

```

is there any difference compared to your example that I am missing?

---

<div class="post-metadata">

**Author:** ![Bertram](https://avatars.discourse-cdn.com/v4/letter/b/e8c25b/32.png) [@Bertram](https://discuss.elastic.co/u/Bertram)\
**Post date:** [July 9, 2021, 7:25am UTC](https://discuss.elastic.co/t/date-field-with-millis-stored-as-double-leading-to-error/277519/5 "2021-07-09T07:25:40Z")

</div>

> [@spinscale](#):
>
> ```auto
> POST test/_update/1
> {
> "script" : {
> "source": "ctx._source.click_count += params.count; if(ctx._source.created instanceof double) {ctx._source.created = (long)ctx._source.created}",
> "lang": "painless",
> "params" : {
> "count" : 1
> }
> }
> }
> 
> ```

This is already the workarround. Which is working. But I do not understand that elastic is not capable to run the following with no error:

```auto
POST test/_update/1
{
  "script" : {
    "source": "ctx._source.click_count += params.count",
    "lang": "painless",
    "params" : {
      "count" : 1
    }
  }
}

```

And if the workarround is intended to be used, why does elasticsearch support the format "epoch\_millis" at all, if you have to look and take care on every update of documents with casting.

---

<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:** [August 6, 2021, 7:25am UTC](https://discuss.elastic.co/t/date-field-with-millis-stored-as-double-leading-to-error/277519/6 "2021-08-06T07:25:48Z")

</div>

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