Kibana create script field : concatenated strings with IF conditions

Hello to the community,

I try to combine 2 date fields into a more meaning ful STRING scripted field based on conditions on the date to add "0" before 1 digit numbers ( May 2nd => 05-02).

This script is refused and I don't know how to solve this even after significant searches on forums
Could you please help?
I work with Elasticsearch 7.8
Thanks

if (doc['startDate'].value.getMonthValue() < 10) return ( 'Le 0'); else ('Le ') + doc['startDate'].value.getMonthValue() +
if (doc['startDate'].value.getDayOfMonth() < 10) return ( ' - 0'); else (' - ') + doc['startDate'].value.getDayOfMonth() +
if (doc['startDate'].value.getHour() < 10) return (' de 0'); else (' de ') + doc['startDate'].value.getHour() +
if (doc['endDate'].value.getHour() < 10) return ('h à 0'); else ('h à ') + doc['endDate'].value.getHour() +'h'

Hello, can you post a few examples on what you're trying to achieve? I'm trying to figure out if you can achieve this with field formatting or anything else.

thanks !
Indeed, formatting would be a solution, but I couldn't figure out how to do it,

[
 {
  "_id": "6148f71866c81e1752a9648aAntoine Renaud",
  "endDate": "2021-09-25T22:04:00.000Z",
  "startDate": "2021-09-25T22:00:00.000Z",
  "shfit name with date": [
   "Le 25 de 22h à 22h"
  ]
 },
 {
  "_id": "6148f71866c81e1752a9648aGregory Saint Etienne",
  "endDate": "2021-09-05T03:04:00.000Z",
  "startDate": "2021-09-05T02:00:00.000Z",
  "shfit name with date": [
   "Le 5 de  2h à 3h"    
  ]
 }]

I'd like to change the second one to "Le 05 de 02h à 03h" so that I can sort these new string fields by alphabetical order. The idea is to get the start date day and hour and concatenate with the end date hour. I also need to add the month first.

This is the current code

'Le '+doc['startDate'].value.getDayOfMonth()+' de '+doc['startDate'].value.getHour()+ 'h à ' + doc['endDate'].value.getHour()+'h'

thanks a lot

@Marius_Dragomir do you have enough to check with above data?
I would greatly appreciate it!
thanks

Someone shared the solution, here it is:

DecimalFormat df = new DecimalFormat("00");
'Le ' + df.format(doc['startDate'].value.getDayOfMonth()) + ' de ' + df.format(doc['startDate'].value.getHour()) + 'h à ' + df.format(doc['endDate'].value.getHour()) + 'h'

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