Search code examples
elasticsearchelasticsearch-painless

Convert date to year-month-day format with Painless


I have dates in this format: 2018-07-24T08:27:59.259Z. What is the best way to convert this to 2018-07-24 in Painless? Looking through the Painless API reference, I realise there are methods such as getYear(), getMonth() and getDayOfMonth(), but I was wondering if there is a simpler way.


Solution

  • An easy way to achieve this without having to mess up with dates is to simply split on the T character, like this:

    POST test/_update_by_query
    {
      "script": {
        "source": "ctx._source.date = /T/.split(ctx._source.date)[0]"
      }
    }