@@ -106,7 +106,7 @@ You can retrieve the last value in a similar way:
106106
107107[discrete]
108108[[painless-time-features]]
109- ==== Getting time features as scripted fields
109+ ==== Getting time features by using aggregations
110110
111111This snippet shows how to extract time based features by using Painless in a
112112{transform}. The snippet uses an index where `@timestamp` is defined as a `date`
@@ -115,37 +115,39 @@ type field.
115115[source,js]
116116--------------------------------------------------
117117"aggregations": {
118- "script_fields": {
119- "hour_of_day": { <1>
120- "script": {
121- "lang": "painless",
122- "source": """
123- ZonedDateTime date = doc['@timestamp'].value; <2>
124- return date.getHour(); <3>
125- """
126- }
127- },
128- "month_of_year": { <4>
129- "script": {
130- "lang": "painless",
131- "source": """
132- ZonedDateTime date = doc['@timestamp'].value; <5>
133- return date.getMonthValue(); <6>
134- """
135- }
118+ "avg_hour_of_day": { <1>
119+ "avg":{
120+ "script": { <2>
121+ "source": """
122+ ZonedDateTime date = doc['@timestamp'].value; <3>
123+ return date.getHour(); <4>
124+ """
136125 }
137- },
138- ...
126+ }
127+ },
128+ "avg_month_of_year": { <5>
129+ "avg":{
130+ "script": { <6>
131+ "source": """
132+ ZonedDateTime date = doc['@timestamp'].value; <7>
133+ return date.getMonthValue(); <8>
134+ """
135+ }
136+ }
137+ },
138+ ...
139139}
140140--------------------------------------------------
141141// NOTCONSOLE
142142
143- <1> Contains the Painless script that returns the hour of the day.
144- <2> Sets `date` based on the timestamp of the document.
145- <3> Returns the hour value from `date`.
146- <4> Contains the Painless script that returns the month of the year.
147- <5> Sets `date` based on the timestamp of the document.
148- <6> Returns the month value from `date`.
143+ <1> Name of the aggregation.
144+ <2> Contains the Painless script that returns the hour of the day.
145+ <3> Sets `date` based on the timestamp of the document.
146+ <4> Returns the hour value from `date`.
147+ <5> Name of the aggregation.
148+ <6> Contains the Painless script that returns the month of the year.
149+ <7> Sets `date` based on the timestamp of the document.
150+ <8> Returns the month value from `date`.
149151
150152
151153[discrete]
0 commit comments