painless.asciidoc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. [[modules-scripting-painless]]
  2. === Painless Scripting Language
  3. experimental[The Painless scripting language is new and is still marked as experimental. The syntax or API may be changed in the future in non-backwards compatible ways if required.]
  4. _Painless_ is a simple, secure scripting language available in Elasticsearch
  5. by default. It is designed specifically for use with Elasticsearch and can
  6. safely be used with `inline` and `stored` scripting, which is enabled by
  7. default.
  8. A Painless script is essentially a single function. Painless does not provide support
  9. for defining multiple functions within a script. The Painless syntax is similar to
  10. http://groovy-lang.org/index.html[Groovy].
  11. You can use Painless anywhere a script can be used in Elasticsearch--simply set the `lang` parameter
  12. to `painless`.
  13. [[painless-features]]
  14. [float]
  15. == Painless Features
  16. * Control flow: `for` loops, `while` loops, `do/while` loops, `if/else`
  17. * Fully Typed: all available types/methods described in <<painless-api, Painless API>>
  18. * Arithmetic operators: multiplication `*`, division `/`, addition `+`, subtraction `-`, precedence `( )`
  19. * Comparison operators: less than `<`, less than or equal to `<=`, greater than `>`, greater than or equal to `>=`, equal to `==`, and not equal to `!=`, reference equals `===`, reference not equals `!==`
  20. * Boolean operators: not `!`, and `&&`, or `||`
  21. * Bitwise operators: shift left `<<`, shift right `>>`, unsigned shift `>>>`, and `&`, or `|`, xor `^`, not `~`
  22. * Shortcuts for list, map access using the dot `.` operator
  23. [[painless-examples]]
  24. [float]
  25. == Painless Examples
  26. To illustrate how Painless works, let's load some hockey stats into an Elasticsearch index:
  27. [source,js]
  28. ----------------------------------------------------------------
  29. PUT hockey/player/_bulk?refresh
  30. {"index":{"_id":1}}
  31. {"first":"johnny","last":"gaudreau","goals":[9,27,1],"assists":[17,46,0],"gp":[26,82,1]}
  32. {"index":{"_id":2}}
  33. {"first":"sean","last":"monohan","goals":[7,54,26],"assists":[11,26,13],"gp":[26,82,82]}
  34. {"index":{"_id":3}}
  35. {"first":"jiri","last":"hudler","goals":[5,34,36],"assists":[11,62,42],"gp":[24,80,79]}
  36. {"index":{"_id":4}}
  37. {"first":"micheal","last":"frolik","goals":[4,6,15],"assists":[8,23,15],"gp":[26,82,82]}
  38. {"index":{"_id":5}}
  39. {"first":"sam","last":"bennett","goals":[5,0,0],"assists":[8,1,0],"gp":[26,1,0]}
  40. {"index":{"_id":6}}
  41. {"first":"dennis","last":"wideman","goals":[0,26,15],"assists":[11,30,24],"gp":[26,81,82]}
  42. {"index":{"_id":7}}
  43. {"first":"david","last":"jones","goals":[7,19,5],"assists":[3,17,4],"gp":[26,45,34]}
  44. {"index":{"_id":8}}
  45. {"first":"tj","last":"brodie","goals":[2,14,7],"assists":[8,42,30],"gp":[26,82,82]}
  46. {"index":{"_id":39}}
  47. {"first":"mark","last":"giordano","goals":[6,30,15],"assists":[3,30,24],"gp":[26,60,63]}
  48. {"index":{"_id":10}}
  49. {"first":"mikael","last":"backlund","goals":[3,15,13],"assists":[6,24,18],"gp":[26,82,82]}
  50. {"index":{"_id":11}}
  51. {"first":"joe","last":"colborne","goals":[3,18,13],"assists":[6,20,24],"gp":[26,67,82]}
  52. ----------------------------------------------------------------
  53. // CONSOLE
  54. // TESTSETUP
  55. [float]
  56. === Accessing Doc Values from Painless
  57. Document values can be accessed from a `Map<String,def>` named `doc`.
  58. For example, the following script calculates a player's total goals. This example uses a strongly typed `int` and a `for` loop.
  59. [source,js]
  60. ----------------------------------------------------------------
  61. GET hockey/_search
  62. {
  63. "query": {
  64. "function_score": {
  65. "script_score": {
  66. "script": {
  67. "lang": "painless",
  68. "inline": "int total = 0; for (int i = 0; i < doc['goals'].length; ++i) { total += doc['goals'][i]; } return total;"
  69. }
  70. }
  71. }
  72. }
  73. }
  74. ----------------------------------------------------------------
  75. // CONSOLE
  76. Alternatively, you could do the same thing using a script field instead of a function score:
  77. [source,js]
  78. ----------------------------------------------------------------
  79. GET hockey/_search
  80. {
  81. "query": {
  82. "match_all": {}
  83. },
  84. "script_fields": {
  85. "total_goals": {
  86. "script": {
  87. "lang": "painless",
  88. "inline": "int total = 0; for (int i = 0; i < doc['goals'].length; ++i) { total += doc['goals'][i]; } return total;"
  89. }
  90. }
  91. }
  92. }
  93. ----------------------------------------------------------------
  94. // CONSOLE
  95. The following example uses a Painless script to sort the players by their combined first and last names. The names are accessed using
  96. `doc['first'].value` and `doc['last'].value`.
  97. [source,js]
  98. ----------------------------------------------------------------
  99. GET hockey/_search
  100. {
  101. "query": {
  102. "match_all": {}
  103. },
  104. "sort": {
  105. "_script": {
  106. "type": "string",
  107. "order": "asc",
  108. "script": {
  109. "lang": "painless",
  110. "inline": "doc['first'].value + ' ' + doc['last'].value"
  111. }
  112. }
  113. }
  114. }
  115. ----------------------------------------------------------------
  116. // CONSOLE
  117. [float]
  118. === Updating Fields with Painless
  119. You can also easily update fields. You access the original source for a field as `ctx._source.<field-name>`.
  120. First, let's look at the source data for a player by submitting the following request:
  121. [source,js]
  122. ----------------------------------------------------------------
  123. GET hockey/_search
  124. {
  125. "fields": [
  126. "_id",
  127. "_source"
  128. ],
  129. "query": {
  130. "term": {
  131. "_id": 1
  132. }
  133. }
  134. }
  135. ----------------------------------------------------------------
  136. // CONSOLE
  137. To change player 1's last name to `hockey`, simply set `ctx._source.last` to the new value:
  138. [source,js]
  139. ----------------------------------------------------------------
  140. POST hockey/player/1/_update
  141. {
  142. "script": {
  143. "lang": "painless",
  144. "inline": "ctx._source.last = params.last",
  145. "params": {
  146. "last": "hockey"
  147. }
  148. }
  149. }
  150. ----------------------------------------------------------------
  151. // CONSOLE
  152. You can also add fields to a document. For example, this script adds a new field that contains
  153. the player's nickname, _hockey_.
  154. [source,js]
  155. ----------------------------------------------------------------
  156. POST hockey/player/1/_update
  157. {
  158. "script": {
  159. "lang": "painless",
  160. "inline": "ctx._source.last = params.last; ctx._source.nick = params.nick",
  161. "params": {
  162. "last": "gaudreau",
  163. "nick": "hockey"
  164. }
  165. }
  166. }
  167. ----------------------------------------------------------------
  168. // CONSOLE
  169. [[painless-api]]
  170. [float]
  171. == Painless API
  172. The following types are available for use in the Painless language. Most types and methods map directly to their Java equivalents--for more information, see the corresponding https://docs.oracle.com/javase/8/docs/api/java/lang/package-summary.html[Javadoc].
  173. [float]
  174. === Dynamic Types
  175. * `def` (This type can be used to represent any other type.)
  176. [float]
  177. === Basic Types
  178. * `void`
  179. * `boolean`
  180. * `short`
  181. * `char`
  182. * `int`
  183. * `long`
  184. * `float`
  185. * `double`
  186. [float]
  187. === Complex Types
  188. Non-static methods/members in superclasses are available to subclasses.
  189. Generic types with unspecified generic parameters are parameters of type `def`.
  190. -----
  191. ArithmeticException extends Exception
  192. <init>()
  193. -----
  194. -----
  195. ArrayList extends List
  196. <init>()
  197. -----
  198. -----
  199. ArrayList<Object> extends List<Object>
  200. <init>()
  201. -----
  202. -----
  203. ArrayList<String> extends List<String>
  204. <init>()
  205. -----
  206. -----
  207. Boolean extends Object
  208. <init>(boolean)
  209. static Boolean valueOf(boolean)
  210. boolean booleanValue()
  211. -----
  212. -----
  213. Character extends Object
  214. <init>(char)
  215. static Character valueOf(char)
  216. char charValue()
  217. static char MIN_VALUE
  218. static char MAX_VALUE
  219. -----
  220. -----
  221. CharSequence extends Object
  222. char charAt(int)
  223. int length()
  224. -----
  225. -----
  226. Collection extends Object
  227. boolean add(def)
  228. void clear()
  229. boolean contains(def)
  230. boolean isEmpty()
  231. Iterator iterator()
  232. boolean remove(def)
  233. int size()
  234. -----
  235. -----
  236. Collection<Object> extends Object
  237. boolean add(Object)
  238. void clear()
  239. boolean contains(Object)
  240. boolean isEmpty()
  241. Iterator iterator()
  242. boolean remove(Object)
  243. int size()
  244. -----
  245. -----
  246. Collection<String> extends Object
  247. boolean add(String)
  248. void clear()
  249. boolean contains(String)
  250. boolean isEmpty()
  251. Iterator iterator()
  252. boolean remove(String)
  253. int size()
  254. -----
  255. -----
  256. Double extends Number
  257. <init>(double)
  258. static Double valueOf(double)
  259. static double MIN_VALUE
  260. static double MAX_VALUE
  261. -----
  262. -----
  263. Exception extends Object
  264. String getMessage()
  265. -----
  266. -----
  267. Float extends Number
  268. <init>(float)
  269. static Float valueOf(float)
  270. static float MIN_VALUE
  271. static float MAX_VALUE
  272. -----
  273. -----
  274. HashMap extends Map
  275. <init>()
  276. -----
  277. -----
  278. HashMap<Object,Object> extends Map<Object,Object>
  279. <init>()
  280. -----
  281. -----
  282. HashMap<String,def> extends Map<String,def>
  283. <init>()
  284. -----
  285. -----
  286. HashMap<String,Object> extends Map<String,Object>
  287. <init>()
  288. -----
  289. -----
  290. IllegalArgument extends Exception
  291. <init>()
  292. -----
  293. -----
  294. IllegalState extends Exception
  295. <init>()
  296. -----
  297. -----
  298. Integer extends Number
  299. <init>(int)
  300. static Integer valueOf(int)
  301. static int MIN_VALUE
  302. static int MAX_VALUE
  303. -----
  304. -----
  305. Iterator extends Object
  306. boolean hasNext()
  307. def next()
  308. void remove()
  309. -----
  310. -----
  311. Iterator<String> extends Object
  312. boolean hasNext()
  313. String next()
  314. void remove()
  315. -----
  316. -----
  317. List extends Collection
  318. def set(int, def)
  319. def get(int)
  320. def remove(int)
  321. -----
  322. -----
  323. List<Object> extends Collection
  324. Object set(int, Object)
  325. Object get(int)
  326. Object remove(int)
  327. -----
  328. -----
  329. List<String> extends Collection
  330. String set(int, String)
  331. String get(int)
  332. String remove(int)
  333. -----
  334. -----
  335. Long extends Number
  336. <init>(long)
  337. static Long valueOf(long)
  338. static long MIN_VALUE
  339. static long MAX_VALUE
  340. -----
  341. -----
  342. Map extends Object
  343. def put (def, def)
  344. def get (def)
  345. def remove (def)
  346. boolean isEmpty()
  347. int size()
  348. boolean containsKey(def)
  349. boolean containsValue(def)
  350. Set keySet()
  351. Collection values()
  352. -----
  353. -----
  354. Map<Object,Object> extends Object
  355. Object put (Object, Object)
  356. Object get (Object)
  357. Object remove (Object)
  358. boolean isEmpty()
  359. int size()
  360. boolean containsKey(Object)
  361. boolean containsValue(Object)
  362. Set keySet()
  363. Collection values()
  364. -----
  365. -----
  366. Map<String,def> extends Object
  367. def put (String, def)
  368. def get (String)
  369. def remove (String)
  370. boolean isEmpty()
  371. int size()
  372. boolean containsKey(String)
  373. boolean containsValue(def)
  374. Set<String> keySet()
  375. Collection values()
  376. -----
  377. -----
  378. Map<String,Object> extends Object
  379. Object put (String, Object)
  380. Object get (String)
  381. Object remove (String)
  382. boolean isEmpty()
  383. int size()
  384. boolean containsKey(String)
  385. boolean containsValue(Object)
  386. Set<String> keySet()
  387. Collection values()
  388. -----
  389. -----
  390. Number extends Object
  391. short shortValue()
  392. short shortValue()
  393. int intValue()
  394. long longValue()
  395. float floatValue()
  396. double doubleValue()
  397. -----
  398. -----
  399. Object
  400. String toString()
  401. boolean equals(Object)
  402. int hashCode()
  403. -----
  404. -----
  405. Set extends Collection
  406. -----
  407. -----
  408. Set<Object> extends Collection<Object>
  409. -----
  410. -----
  411. Set<String> extends Collection<String>
  412. -----
  413. -----
  414. Short extends Number
  415. <init>(short)
  416. static Short valueOf(short)
  417. static short MIN_VALUE
  418. static short MAX_VALUE
  419. -----
  420. -----
  421. String extends CharSequence
  422. <init>(String)
  423. int codePointAt(int)
  424. int compareTo(String)
  425. String concat(String)
  426. boolean endsWith(String)
  427. int indexOf(String, int)
  428. boolean isEmpty()
  429. String replace(CharSequence, CharSequence)
  430. boolean startsWith(String)
  431. String substring(int, int)
  432. char[] toCharArray()
  433. String trim()
  434. -----
  435. -----
  436. NumberFormatException extends Exception
  437. <init>()
  438. -----
  439. -----
  440. Void extends Object
  441. -----
  442. [float]
  443. ==== Utility Classes
  444. -----
  445. Math
  446. static double abs(double)
  447. static float fabs(float)
  448. static long labs(long)
  449. static int iabs(int)
  450. static double acos(double)
  451. static double asin(double)
  452. static double atan(double)
  453. static double atan2(double)
  454. static double cbrt(double)
  455. static double ceil(double)
  456. static double cos(double)
  457. static double cosh(double)
  458. static double exp(double)
  459. static double expm1(double)
  460. static double floor(double)
  461. static double hypt(double, double)
  462. static double abs(double)
  463. static double log(double)
  464. static double log10(double)
  465. static double log1p(double)
  466. static double max(double, double)
  467. static float fmax(float, float)
  468. static long lmax(long, long)
  469. static int imax(int, int)
  470. static double min(double, double)
  471. static float fmin(float, float)
  472. static long lmin(long, long)
  473. static int imin(int, int)
  474. static double pow(double, double)
  475. static double random()
  476. static double rint(double)
  477. static long round(double)
  478. static double sin(double)
  479. static double sinh(double)
  480. static double sqrt(double)
  481. static double tan(double)
  482. static double tanh(double)
  483. static double toDegrees(double)
  484. static double toRadians(double)
  485. -----
  486. -----
  487. Utility
  488. static boolean NumberToboolean(Number)
  489. static char NumberTochar(Number)
  490. static Boolean NumberToBoolean(Number)
  491. static Short NumberToShort(Number)
  492. static Character NumberToCharacter(Number)
  493. static Integer NumberToInteger(Number)
  494. static Long NumberToLong(Number)
  495. static Float NumberToFloat(Number)
  496. static Double NumberToDouble(Number)
  497. static byte booleanTobyte(boolean)
  498. static short booleanToshort(boolean)
  499. static char booleanTochar(boolean)
  500. static int booleanToint(boolean)
  501. static long booleanTolong(boolean)
  502. static float booleanTofloat(boolean)
  503. static double booleanTodouble(boolean)
  504. static Integer booleanToInteger(boolean)
  505. static byte BooleanTobyte(Boolean)
  506. static short BooleanToshort(Boolean)
  507. static char BooleanTochar(Boolean)
  508. static int BooleanToint(Boolean)
  509. static long BooleanTolong(Boolean)
  510. static float BooleanTofloat(Boolean)
  511. static double BooleanTodouble(Boolean)
  512. static Byte BooleanToByte(Boolean)
  513. static Short BooleanToShort(Boolean)
  514. static Character BooleanToCharacter(Boolean)
  515. static Integer BooleanToInteger(Boolean)
  516. static Long BooleanToLong(Boolean)
  517. static Float BooleanToFloat(Boolean)
  518. static Double BooleanToDouble(Boolean)
  519. static boolean byteToboolean(byte)
  520. static Short byteToShort(byte)
  521. static Character byteToCharacter(byte)
  522. static Integer byteToInteger(byte)
  523. static Long byteToLong(byte)
  524. static Float byteToFloat(byte)
  525. static Double byteToDouble(byte)
  526. static boolean ByteToboolean(Byte)
  527. static char ByteTochar(Byte)
  528. static boolean shortToboolean(short)
  529. static Byte shortToByte(short)
  530. static Character shortToCharacter(short)
  531. static Integer shortToInteger(short)
  532. static Long shortToLong(short)
  533. static Float shortToFloat(short)
  534. static Double shortToDouble(short)
  535. static boolean ShortToboolean(Short)
  536. static char ShortTochar(Short)
  537. static boolean charToboolean(char)
  538. static Byte charToByte(char)
  539. static Short charToShort(char)
  540. static Integer charToInteger(char)
  541. static Long charToLong(char)
  542. static Float charToFloat(char)
  543. static Double charToDouble(char)
  544. static boolean CharacterToboolean(Character)
  545. static byte CharacterTobyte(Character)
  546. static short CharacterToshort(Character)
  547. static int CharacterToint(Character)
  548. static long CharacterTolong(Character)
  549. static float CharacterTofloat(Character)
  550. static double CharacterTodouble(Character)
  551. static Boolean CharacterToBoolean(Character)
  552. static Byte CharacterToByte(Character)
  553. static Short CharacterToShort(Character)
  554. static Integer CharacterToInteger(Character)
  555. static Long CharacterToLong(Character)
  556. static Float CharacterToFloat(Character)
  557. static Double CharacterToDouble(Character)
  558. static boolean intToboolean(int)
  559. static Byte intToByte(int)
  560. static Short intToShort(int)
  561. static Character intToCharacter(int)
  562. static Long intToLong(int)
  563. static Float intToFloat(int)
  564. static Double intToDouble(int)
  565. static boolean IntegerToboolean(Integer)
  566. static char IntegerTochar(Integer)
  567. static boolean longToboolean(long)
  568. static Byte longToByte(long)
  569. static Short longToShort(long)
  570. static Character longToCharacter(long)
  571. static Integer longToInteger(long)
  572. static Float longToFloat(long)
  573. static Double longToDouble(long)
  574. static boolean LongToboolean(Long)
  575. static char LongTochar(Long)
  576. static boolean floatToboolean(float)
  577. static Byte floatToByte(float)
  578. static Short floatToShort(float)
  579. static Character floatToCharacter(float)
  580. static Integer floatToInteger(float)
  581. static Long floatToLong(float)
  582. static Double floatToDouble(float)
  583. static boolean FloatToboolean(Float)
  584. static char FloatTochar(Float)
  585. static boolean doubleToboolean(double)
  586. static Byte doubleToByte(double)
  587. static Short doubleToShort(double)
  588. static Character doubleToCharacter(double)
  589. static Integer doubleToInteger(double)
  590. static Long doubleToLong(double)
  591. static Float doubleToFloat(double)
  592. static boolean DoubleToboolean(Double)
  593. static char DoubleTochar(Double)
  594. -----
  595. -----
  596. Def
  597. static boolean defToboolean(def)
  598. static byte defTobyte(def)
  599. static short defToshort(def)
  600. static char defTochar(def)
  601. static int defToint(def)
  602. static long defTolong(def)
  603. static float defTofloat(def)
  604. static double defTodouble(def)
  605. static Boolean defToBoolean(def)
  606. static Byte defToByte(def)
  607. static Character defToCharacter(def)
  608. static Integer defToInteger(def)
  609. static Long defToLong(def)
  610. static Float defToFloat(def)
  611. static Double defToDouble(def)
  612. -----