Search code examples
elasticsearchluceneelasticsearch-painless

Incrementing Datetime field by one day in Elasticsearch Production Cluster using painless script


I am facing difficulty in debugging a production level Elastic Search Index Date time field in yyyy-MM-dd format & i want to update/increment the datetime field by one day Eg- 2009-07-01 i want to update it to 2009-07-02 for all the documents in the index.

I also want to know whether i have to use the re index api or update by query api

Currently i tried to update the document using following painless script its not working

POST klaprod-11042022/_update_by_query


{
  "script": {
    "source": "def df = DateTimeFormatter.ofPattern('yyyy-MM-dd');def tmp = LocalDateTime.parse(ctx._source.debate_section_date,df);ctx._source.debate_section_date=tmp.plusDays(1);",
    "lang": "painless"
  },
  "query": {"match_all":{}}
}

Any advise by the community is appreciated


Solution

  • After going through the Documentation the following worked for me , you dont have to call the DateTimeFormatter in painless as LocalDate can parse the date time string when in raw yyyy-MM-dd format

    POST klaprod-11042022/_update_by_query
    {
      "script": {
        "source": "def tmp = LocalDate.parse(ctx._source.debate_section_date);ctx._source.debate_section_date=tmp.plusDays(1);",
        "lang": "painless"
      },
      "query": {"match_all":{}}
    }