WARNING: Version 5.0 of Elasticsearch has passed its EOL date.
This documentation is no longer being maintained and may be removed. If you are running this version, we strongly advise you to upgrade. For the latest information, see the current release documentation.
Groovy Scripting Languageedit
Deprecated in 5.0.0.
Groovy will be replaced by the new scripting language Painless
Groovy is available in Elasticsearch by default. Although
limited by the Java Security Manager, it is not a
sandboxed language and only file
scripts may be used by default.
Enabling inline
or stored
Groovy scripting is a security risk and should
only be considered if your Elasticsearch cluster is protected from the outside
world. Even a simple while (true) { }
loop could behave as a denialof
service attack on your cluster.
See Scripting and Security for details on security issues with scripts, including how to customize class whitelisting.
Doc value properties and methodsedit
Doc values in Groovy support the following properties and methods (depending on the underlying field type):

doc['field_name'].value
 The native value of the field. For example, if its a short type, it will be short.

doc['field_name'].values
 The native array values of the field. For example, if its a short type, it will be short[]. Remember, a field can have several values within a single doc. Returns an empty array if the field has no values.

doc['field_name'].empty
 A boolean indicating if the field has no values within the doc.

doc['field_name'].lat

The latitude of a geo point type, or
null
. 
doc['field_name'].lon

The longitude of a geo point type, or
null
. 
doc['field_name'].lats
 The latitudes of a geo point type, or an empty array.

doc['field_name'].lons
 The longitudes of a geo point type, or an empty array.

doc['field_name'].arcDistance(lat, lon)

The
arc
distance (in meters) of this geo point field from the provided lat/lon. 
doc['field_name'].arcDistanceWithDefault(lat, lon, default)

The
arc
distance (in meters) of this geo point field from the provided lat/lon with a default value for empty fields. 
doc['field_name'].planeDistance(lat, lon)

The
plane
distance (in meters) of this geo point field from the provided lat/lon. 
doc['field_name'].planeDistanceWithDefault(lat, lon, default)

The
plane
distance (in meters) of this geo point field from the provided lat/lon with a default value for empty fields. 
doc['field_name'].geohashDistance(geohash)

The
arc
distance (in meters) of this geo point field from the provided geohash. 
doc['field_name'].geohashDistanceWithDefault(geohash, default)

The
arc
distance (in meters) of this geo point field from the provided geohash with a default value for empty fields.
Groovy Built In Functionsedit
There are several built in functions that can be used within scripts. They include:
Function  Description 


Returns the trigonometric sine of an angle. 

Returns the trigonometric cosine of an angle. 

Returns the trigonometric tangent of an angle. 

Returns the arc sine of a value. 

Returns the arc cosine of a value. 

Returns the arc tangent of a value. 

Converts an angle measured in degrees to an approximately equivalent angle measured in radians 

Converts an angle measured in radians to an approximately equivalent angle measured in degrees. 

Returns Euler’s number e raised to the power of value. 

Returns the natural logarithm (base e) of a value. 

Returns the base 10 logarithm of a value. 

Returns the correctly rounded positive square root of a value. 

Returns the cube root of a double value. 

Computes the remainder operation on two arguments as prescribed by the IEEE 754 standard. 

Returns the smallest (closest to negative infinity) value that is greater than or equal to the argument and is equal to a mathematical integer. 

Returns the largest (closest to positive infinity) value that is less than or equal to the argument and is equal to a mathematical integer. 

Returns the value that is closest in value to the argument and is equal to a mathematical integer. 

Returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r,theta). 

Returns the value of the first argument raised to the power of the second argument. 

Returns the closest int to the argument. 

Returns a random double value. 

Returns the absolute value of a value. 

Returns the greater of two values. 

Returns the smaller of two values. 

Returns the size of an ulp of the argument. 

Returns the signum function of the argument. 

Returns the hyperbolic sine of a value. 

Returns the hyperbolic cosine of a value. 

Returns the hyperbolic tangent of a value. 

Returns sqrt(x2 + y2) without intermediate overflow or underflow. 