How to convert a date to string using local time

Hello,
I need to compute a field that concats a string and a date.
I don't know how to convert the date using the local time.
Can you help getting desired results below?

PUT my_index/x
{
  "ref": "x",
  "date" : "2019-11-15T15:16:16+01:00"
}

PUT my_index/y
{
  "ref": "y",
  "date" : "2019-09-10T00:00:00+02:00"
}

GET my_index/_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "ref_date": {
      "script": "doc['ref.keyword'].value + ':' + doc['date'].value.toString('yyyy-MM-dd')"
    }
  }
}

ACTUAL RESULTS:

{
        "_id" : "x",
        "fields" : {
          "ref_date" : [
            "x:2019-11-15"
          ]
        }
      },
      {
        "_id" : "y",
        "fields" : {
          "ref_date" : [
            "y:2019-09-09"
          ]
        }
      }

DESIRED RESULTS:

{
        "_id" : "x",
        "fields" : {
          "ref_date" : [
            "x:2019-11-15"
          ]
        }
      },
      {
        "_id" : "y",
        "fields" : {
          "ref_date" : [
            "y:2019-09-10"
          ]
        }
      }

So, this is how I solved it:

GET my_index/_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "ref_date": {
      "script": "doc['ref.keyword'].value + ':' + params['_source']['date'].substring(0,10)"
    }
  }
}

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