Scripted Field for Date to Quarter and adding names


#1

Im pretty new to scripted fields. I read the elastic blog post on it and checked lucene capabilities. So far I have the simple scripted field to convert the date to month.
doc['@timestamp'].date.monthOfYear
which returns 1-12. what I'd like to do 2-fold.

  1. Convert the number to the name of the month or at least add the name to it. aka 1 --> January or to 1-January.

  2. I'd like to create another scripted field which actually returns 1-4 for accounting quarterly reports. aka Jan-Mar=1, Apr-Jun=2, Jul-Sep=3 and finally Oct-Dec=4 I know accounting quarters may actually be slightly different, but im sure solution could easily be modified to set the proper values if they are indeed different.

thanks for any help.


(Tyler Smalley) #2

Something like this should work:

def month = doc['timestamp'].value.monthOfYear;

def months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ]; 

return months[month-1];

#3

cool, so i did the following:
type=string

scripted field:

def month = doc['timestamp'].value.monthOfYear;

def months = ['01-January', '02-February', '03-March', '04-April', '05-May', '06-June', '07-July', '08-August', '09-September', '10-October', '11-November', '12-December' ]; 

return months[month-1];

That way its sortable as well. For the quarterly bit I did a very similar bit except:

def month = doc['timestamp'].value.monthOfYear;

def quarters  = ['01', '01', '01', '02', '02', '02', '03', '03', '03', '04', '04', '04' ]; 

return quarters[month-1];

Thank you for the help here!