lxneng lxneng - 3 months ago 26
Groovy Question

how to convert date type field data to unixtimestamp in elasticsearch

in es,

created_at
as a date type

created_at: {
type: "date",
format: "strict_date_optional_time||epoch_millis"
},


i want use created_at as a UnixTimeStamp epoch seconds in
script_score
calculation. how to implement this?

(doc['created_at'].value.to_unixtimestamp? - 1412092800)/86400.0

Val Val
Answer

You have two options, either like this

((doc['created_at'].value / 1000) - 1412092800)/86400.0

or like this:

((doc['created_at'].date.millis / 1000) - 1412092800)/86400.0