groovy.asciidoc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. [[modules-scripting-groovy]]
  2. === Groovy Scripting Language
  3. Groovy is the default scripting language available in Elasticsearch. Although
  4. limited by the <<java-security-manager,Java Security Manager>>, it is not a
  5. sandboxed language and only `file` scripts may be used by default.
  6. Enabling `inline` or `stored` Groovy scripting is a security risk and should
  7. only be considered if your Elasticsearch cluster is protected from the outside
  8. world. Even a simple `while (true) { }` loop could behave as a denial-of-
  9. service attack on your cluster.
  10. See <<modules-scripting-security, Scripting and Security>> for details
  11. on security issues with scripts, including how to customize class
  12. whitelisting.
  13. [float]
  14. === Doc value properties and methods
  15. Doc values in Groovy support the following properties and methods (depending
  16. on the underlying field type):
  17. `doc['field_name'].value`::
  18. The native value of the field. For example, if its a short type, it will be short.
  19. `doc['field_name'].values`::
  20. The native array values of the field. For example, if its a short type,
  21. it will be short[]. Remember, a field can have several values within a
  22. single doc. Returns an empty array if the field has no values.
  23. `doc['field_name'].empty`::
  24. A boolean indicating if the field has no values within the doc.
  25. `doc['field_name'].multiValued`::
  26. A boolean indicating that the field has several values within the corpus.
  27. `doc['field_name'].lat`::
  28. The latitude of a geo point type, or `null`.
  29. `doc['field_name'].lon`::
  30. The longitude of a geo point type, or `null`.
  31. `doc['field_name'].lats`::
  32. The latitudes of a geo point type, or an empty array.
  33. `doc['field_name'].lons`::
  34. The longitudes of a geo point type, or an empty array.
  35. `doc['field_name'].distance(lat, lon)`::
  36. The `plane` distance (in meters) of this geo point field from the provided lat/lon.
  37. `doc['field_name'].distanceWithDefault(lat, lon, default)`::
  38. The `plane` distance (in meters) of this geo point field from the provided lat/lon with a default value.
  39. `doc['field_name'].distanceInMiles(lat, lon)`::
  40. The `plane` distance (in miles) of this geo point field from the provided lat/lon.
  41. `doc['field_name'].distanceInMilesWithDefault(lat, lon, default)`::
  42. The `plane` distance (in miles) of this geo point field from the provided lat/lon with a default value.
  43. `doc['field_name'].distanceInKm(lat, lon)`::
  44. The `plane` distance (in km) of this geo point field from the provided lat/lon.
  45. `doc['field_name'].distanceInKmWithDefault(lat, lon, default)`::
  46. The `plane` distance (in km) of this geo point field from the provided lat/lon with a default value.
  47. `doc['field_name'].arcDistance(lat, lon)`::
  48. The `arc` distance (in meters) of this geo point field from the provided lat/lon.
  49. `doc['field_name'].arcDistanceWithDefault(lat, lon, default)`::
  50. The `arc` distance (in meters) of this geo point field from the provided lat/lon with a default value.
  51. `doc['field_name'].arcDistanceInMiles(lat, lon)`::
  52. The `arc` distance (in miles) of this geo point field from the provided lat/lon.
  53. `doc['field_name'].arcDistanceInMilesWithDefault(lat, lon, default)`::
  54. The `arc` distance (in miles) of this geo point field from the provided lat/lon with a default value.
  55. `doc['field_name'].arcDistanceInKm(lat, lon)`::
  56. The `arc` distance (in km) of this geo point field from the provided lat/lon.
  57. `doc['field_name'].arcDistanceInKmWithDefault(lat, lon, default)`::
  58. The `arc` distance (in km) of this geo point field from the provided lat/lon with a default value.
  59. `doc['field_name'].factorDistance(lat, lon)`::
  60. The distance factor of this geo point field from the provided lat/lon.
  61. `doc['field_name'].factorDistance(lat, lon, default)`::
  62. The distance factor of this geo point field from the provided lat/lon with a default value.
  63. `doc['field_name'].geohashDistance(geohash)`::
  64. The `arc` distance (in meters) of this geo point field from the provided geohash.
  65. `doc['field_name'].geohashDistanceInKm(geohash)`::
  66. The `arc` distance (in km) of this geo point field from the provided geohash.
  67. `doc['field_name'].geohashDistanceInMiles(geohash)`::
  68. The `arc` distance (in miles) of this geo point field from the provided geohash.
  69. [float]
  70. === Groovy Built In Functions
  71. There are several built in functions that can be used within scripts.
  72. They include:
  73. [cols="<,<",options="header",]
  74. |=======================================================================
  75. |Function |Description
  76. |`sin(a)` |Returns the trigonometric sine of an angle.
  77. |`cos(a)` |Returns the trigonometric cosine of an angle.
  78. |`tan(a)` |Returns the trigonometric tangent of an angle.
  79. |`asin(a)` |Returns the arc sine of a value.
  80. |`acos(a)` |Returns the arc cosine of a value.
  81. |`atan(a)` |Returns the arc tangent of a value.
  82. |`toRadians(angdeg)` |Converts an angle measured in degrees to an
  83. approximately equivalent angle measured in radians
  84. |`toDegrees(angrad)` |Converts an angle measured in radians to an
  85. approximately equivalent angle measured in degrees.
  86. |`exp(a)` |Returns Euler's number _e_ raised to the power of value.
  87. |`log(a)` |Returns the natural logarithm (base _e_) of a value.
  88. |`log10(a)` |Returns the base 10 logarithm of a value.
  89. |`sqrt(a)` |Returns the correctly rounded positive square root of a
  90. value.
  91. |`cbrt(a)` |Returns the cube root of a double value.
  92. |`IEEEremainder(f1, f2)` |Computes the remainder operation on two
  93. arguments as prescribed by the IEEE 754 standard.
  94. |`ceil(a)` |Returns the smallest (closest to negative infinity) value
  95. that is greater than or equal to the argument and is equal to a
  96. mathematical integer.
  97. |`floor(a)` |Returns the largest (closest to positive infinity) value
  98. that is less than or equal to the argument and is equal to a
  99. mathematical integer.
  100. |`rint(a)` |Returns the value that is closest in value to the argument
  101. and is equal to a mathematical integer.
  102. |`atan2(y, x)` |Returns the angle _theta_ from the conversion of
  103. rectangular coordinates (_x_, _y_) to polar coordinates (r,_theta_).
  104. |`pow(a, b)` |Returns the value of the first argument raised to the
  105. power of the second argument.
  106. |`round(a)` |Returns the closest _int_ to the argument.
  107. |`random()` |Returns a random _double_ value.
  108. |`abs(a)` |Returns the absolute value of a value.
  109. |`max(a, b)` |Returns the greater of two values.
  110. |`min(a, b)` |Returns the smaller of two values.
  111. |`ulp(d)` |Returns the size of an ulp of the argument.
  112. |`signum(d)` |Returns the signum function of the argument.
  113. |`sinh(x)` |Returns the hyperbolic sine of a value.
  114. |`cosh(x)` |Returns the hyperbolic cosine of a value.
  115. |`tanh(x)` |Returns the hyperbolic tangent of a value.
  116. |`hypot(x, y)` |Returns sqrt(_x2_ + _y2_) without intermediate overflow
  117. or underflow.
  118. |=======================================================================