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
CodePudding user response:
You are getting exception because your date is not contaning any time. You need to use atStartOfDay()
method of LocalDate
to add one day.
POST datecheck/_update_by_query
{
"script": {
"source": "def df = DateTimeFormatter.ofPattern('yyyy-MM-dd');def tmp = LocalDate.parse(ctx._source.date,df).atStartOfDay();ctx._source.date=tmp.plusDays(1);",
"lang": "painless"
},
"query": {"match_all":{}}
}
This will generate date with plus one date and add time as well. So if you not need time then you can again format date before setting to ctx._source.date
field in script.
I also want to know whether i have to use the re index api or update by query api
If you want to copy data to the new index then you can use reindex
api otherwise _update_by_query
will be used for same index update.
CodePudding user response:
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":{}}
}