painless.asciidoc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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. // AUTOSENSE
  54. // TESTSETUP
  55. [float]
  56. === Accessing Doc Values from Painless
  57. All Painless scripts take in a `Map<String,def>` of values called `input`. Document values can be accessed through another `Map<String,def>` within the `input` variable.
  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 < input.doc['goals'].size(); ++i) { total += input.doc['goals'][i]; } return total;"
  69. }
  70. }
  71. }
  72. }
  73. }
  74. ----------------------------------------------------------------
  75. // AUTOSENSE
  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 < input.doc['goals'].size(); ++i) { total += input.doc['goals'][i]; } return total;"
  89. }
  90. }
  91. }
  92. }
  93. ----------------------------------------------------------------
  94. // AUTOSENSE
  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. `input.doc['first'].value` and `input.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": "input.doc['first'].value + ' ' + input.doc['last'].value"
  111. }
  112. }
  113. }
  114. }
  115. ----------------------------------------------------------------
  116. // AUTOSENSE
  117. [float]
  118. === Updating Fields with Painless
  119. You can also easily update fields. You access the original source for a field as `input.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. // AUTOSENSE
  137. To change player 1's last name to `hockey`, simply set `input.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": "input.ctx._source.last = input.last",
  145. "params": {
  146. "last": "hockey"
  147. }
  148. }
  149. }
  150. ----------------------------------------------------------------
  151. // AUTOSENSE
  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": "input.ctx._source.last = input.last input.ctx._source.nick = input.nick",
  161. "params": {
  162. "last": "gaudreau",
  163. "nick": "hockey"
  164. }
  165. }
  166. }
  167. ----------------------------------------------------------------
  168. // AUTOSENSE
  169. [float]
  170. === Writing Type-Safe Scripts to Improve Performance
  171. If you explicitly specify types, the compiler doesn't have to perform type lookups at runtime, which can significantly
  172. improve performance. For example, the following script performs the same first name, last name sort we showed before,
  173. but it's fully type-safe.
  174. [source,js]
  175. ----------------------------------------------------------------
  176. GET hockey/_search
  177. {
  178. "query": {
  179. "match_all": {}
  180. },
  181. "script_fields": {
  182. "full_name_dynamic": {
  183. "script": {
  184. "lang": "painless",
  185. "inline": "def first = input.doc['first'].value; def last = input.doc['last'].value; return first + ' ' + last;"
  186. }
  187. },
  188. "full_name_static": {
  189. "script": {
  190. "lang": "painless",
  191. "inline": "String first = (String)((List)((Map)input.get('doc')).get('first')).get(0); String last = (String)((List)((Map)input.get('doc')).get('last')).get(0); return first + ' ' + last;"
  192. }
  193. }
  194. }
  195. }
  196. ----------------------------------------------------------------
  197. // AUTOSENSE
  198. [[painless-api]]
  199. [float]
  200. == Painless API
  201. 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].
  202. [float]
  203. === Dynamic Types
  204. * `def` (This type can be used to represent any other type.)
  205. [float]
  206. === Basic Types
  207. * `void`
  208. * `boolean`
  209. * `short`
  210. * `char`
  211. * `int`
  212. * `long`
  213. * `float`
  214. * `double`
  215. [float]
  216. === Complex Types
  217. Non-static methods/members in superclasses are available to subclasses.
  218. Generic types with unspecified generic parameters are parameters of type `def`.
  219. -----
  220. ArithmeticException extends Exception
  221. <init>()
  222. -----
  223. -----
  224. ArrayList extends List
  225. <init>()
  226. -----
  227. -----
  228. ArrayList<Object> extends List<Object>
  229. <init>()
  230. -----
  231. -----
  232. ArrayList<String> extends List<String>
  233. <init>()
  234. -----
  235. -----
  236. Boolean extends Object
  237. <init>(boolean)
  238. static Boolean valueOf(boolean)
  239. boolean booleanValue()
  240. -----
  241. -----
  242. Character extends Object
  243. <init>(char)
  244. static Character valueOf(char)
  245. char charValue()
  246. static char MIN_VALUE
  247. static char MAX_VALUE
  248. -----
  249. -----
  250. CharSequence extends Object
  251. char charAt(int)
  252. int length()
  253. -----
  254. -----
  255. Collection extends Object
  256. boolean add(def)
  257. void clear()
  258. boolean contains(def)
  259. boolean isEmpty()
  260. Iterator iterator()
  261. boolean remove(def)
  262. int size()
  263. -----
  264. -----
  265. Collection<Object> extends Object
  266. boolean add(Object)
  267. void clear()
  268. boolean contains(Object)
  269. boolean isEmpty()
  270. Iterator iterator()
  271. boolean remove(Object)
  272. int size()
  273. -----
  274. -----
  275. Collection<String> extends Object
  276. boolean add(String)
  277. void clear()
  278. boolean contains(String)
  279. boolean isEmpty()
  280. Iterator iterator()
  281. boolean remove(String)
  282. int size()
  283. -----
  284. -----
  285. Double extends Number
  286. <init>(double)
  287. static Double valueOf(double)
  288. static double MIN_VALUE
  289. static double MAX_VALUE
  290. -----
  291. -----
  292. Exception extends Object
  293. String getMessage()
  294. -----
  295. -----
  296. Float extends Number
  297. <init>(float)
  298. static Float valueOf(float)
  299. static float MIN_VALUE
  300. static float MAX_VALUE
  301. -----
  302. -----
  303. HashMap extends Map
  304. <init>()
  305. -----
  306. -----
  307. HashMap<Object,Object> extends Map<Object,Object>
  308. <init>()
  309. -----
  310. -----
  311. HashMap<String,def> extends Map<String,def>
  312. <init>()
  313. -----
  314. -----
  315. HashMap<String,Object> extends Map<String,Object>
  316. <init>()
  317. -----
  318. -----
  319. IllegalArgument extends Exception
  320. <init>()
  321. -----
  322. -----
  323. IllegalState extends Exception
  324. <init>()
  325. -----
  326. -----
  327. Integer extends Number
  328. <init>(int)
  329. static Integer valueOf(int)
  330. static int MIN_VALUE
  331. static int MAX_VALUE
  332. -----
  333. -----
  334. Iterator extends Object
  335. boolean hasNext()
  336. def next()
  337. void remove()
  338. -----
  339. -----
  340. Iterator<String> extends Object
  341. boolean hasNext()
  342. String next()
  343. void remove()
  344. -----
  345. -----
  346. List extends Collection
  347. def set(int, def)
  348. def get(int)
  349. def remove(int)
  350. -----
  351. -----
  352. List<Object> extends Collection
  353. Object set(int, Object)
  354. Object get(int)
  355. Object remove(int)
  356. -----
  357. -----
  358. List<String> extends Collection
  359. String set(int, String)
  360. String get(int)
  361. String remove(int)
  362. -----
  363. -----
  364. Long extends Number
  365. <init>(long)
  366. static Long valueOf(long)
  367. static long MIN_VALUE
  368. static long MAX_VALUE
  369. -----
  370. -----
  371. Map extends Object
  372. def put (def, def)
  373. def get (def)
  374. def remove (def)
  375. boolean isEmpty()
  376. int size()
  377. boolean containsKey(def)
  378. boolean containsValue(def)
  379. Set keySet()
  380. Collection values()
  381. -----
  382. -----
  383. Map<Object,Object> extends Object
  384. Object put (Object, Object)
  385. Object get (Object)
  386. Object remove (Object)
  387. boolean isEmpty()
  388. int size()
  389. boolean containsKey(Object)
  390. boolean containsValue(Object)
  391. Set keySet()
  392. Collection values()
  393. -----
  394. -----
  395. Map<String,def> extends Object
  396. def put (String, def)
  397. def get (String)
  398. def remove (String)
  399. boolean isEmpty()
  400. int size()
  401. boolean containsKey(String)
  402. boolean containsValue(def)
  403. Set<String> keySet()
  404. Collection values()
  405. -----
  406. -----
  407. Map<String,Object> extends Object
  408. Object put (String, Object)
  409. Object get (String)
  410. Object remove (String)
  411. boolean isEmpty()
  412. int size()
  413. boolean containsKey(String)
  414. boolean containsValue(Object)
  415. Set<String> keySet()
  416. Collection values()
  417. -----
  418. -----
  419. Number extends Object
  420. short shortValue()
  421. short shortValue()
  422. int intValue()
  423. long longValue()
  424. float floatValue()
  425. double doubleValue()
  426. -----
  427. -----
  428. Object
  429. String toString()
  430. boolean equals(Object)
  431. int hashCode()
  432. -----
  433. -----
  434. Set extends Collection
  435. -----
  436. -----
  437. Set<Object> extends Collection<Object>
  438. -----
  439. -----
  440. Set<String> extends Collection<String>
  441. -----
  442. -----
  443. Short extends Number
  444. <init>(short)
  445. static Short valueOf(short)
  446. static short MIN_VALUE
  447. static short MAX_VALUE
  448. -----
  449. -----
  450. String extends CharSequence
  451. <init>(String)
  452. int codePointAt(int)
  453. int compareTo(String)
  454. String concat(String)
  455. boolean endsWith(String)
  456. int indexOf(String, int)
  457. boolean isEmpty()
  458. String replace(CharSequence, CharSequence)
  459. boolean startsWith(String)
  460. String substring(int, int)
  461. char[] toCharArray()
  462. String trim()
  463. -----
  464. -----
  465. NumberFormatException extends Exception
  466. <init>()
  467. -----
  468. -----
  469. Void extends Object
  470. -----
  471. [float]
  472. ==== Utility Classes
  473. -----
  474. Math
  475. static double abs(double)
  476. static float fabs(float)
  477. static long labs(long)
  478. static int iabs(int)
  479. static double acos(double)
  480. static double asin(double)
  481. static double atan(double)
  482. static double atan2(double)
  483. static double cbrt(double)
  484. static double ceil(double)
  485. static double cos(double)
  486. static double cosh(double)
  487. static double exp(double)
  488. static double expm1(double)
  489. static double floor(double)
  490. static double hypt(double, double)
  491. static double abs(double)
  492. static double log(double)
  493. static double log10(double)
  494. static double log1p(double)
  495. static double max(double, double)
  496. static float fmax(float, float)
  497. static long lmax(long, long)
  498. static int imax(int, int)
  499. static double min(double, double)
  500. static float fmin(float, float)
  501. static long lmin(long, long)
  502. static int imin(int, int)
  503. static double pow(double, double)
  504. static double random()
  505. static double rint(double)
  506. static long round(double)
  507. static double sin(double)
  508. static double sinh(double)
  509. static double sqrt(double)
  510. static double tan(double)
  511. static double tanh(double)
  512. static double toDegrees(double)
  513. static double toRadians(double)
  514. -----
  515. -----
  516. Utility
  517. static boolean NumberToboolean(Number)
  518. static char NumberTochar(Number)
  519. static Boolean NumberToBoolean(Number)
  520. static Short NumberToShort(Number)
  521. static Character NumberToCharacter(Number)
  522. static Integer NumberToInteger(Number)
  523. static Long NumberToLong(Number)
  524. static Float NumberToFloat(Number)
  525. static Double NumberToDouble(Number)
  526. static byte booleanTobyte(boolean)
  527. static short booleanToshort(boolean)
  528. static char booleanTochar(boolean)
  529. static int booleanToint(boolean)
  530. static long booleanTolong(boolean)
  531. static float booleanTofloat(boolean)
  532. static double booleanTodouble(boolean)
  533. static Integer booleanToInteger(boolean)
  534. static byte BooleanTobyte(Boolean)
  535. static short BooleanToshort(Boolean)
  536. static char BooleanTochar(Boolean)
  537. static int BooleanToint(Boolean)
  538. static long BooleanTolong(Boolean)
  539. static float BooleanTofloat(Boolean)
  540. static double BooleanTodouble(Boolean)
  541. static Byte BooleanToByte(Boolean)
  542. static Short BooleanToShort(Boolean)
  543. static Character BooleanToCharacter(Boolean)
  544. static Integer BooleanToInteger(Boolean)
  545. static Long BooleanToLong(Boolean)
  546. static Float BooleanToFloat(Boolean)
  547. static Double BooleanToDouble(Boolean)
  548. static boolean byteToboolean(byte)
  549. static Short byteToShort(byte)
  550. static Character byteToCharacter(byte)
  551. static Integer byteToInteger(byte)
  552. static Long byteToLong(byte)
  553. static Float byteToFloat(byte)
  554. static Double byteToDouble(byte)
  555. static boolean ByteToboolean(Byte)
  556. static char ByteTochar(Byte)
  557. static boolean shortToboolean(short)
  558. static Byte shortToByte(short)
  559. static Character shortToCharacter(short)
  560. static Integer shortToInteger(short)
  561. static Long shortToLong(short)
  562. static Float shortToFloat(short)
  563. static Double shortToDouble(short)
  564. static boolean ShortToboolean(Short)
  565. static char ShortTochar(Short)
  566. static boolean charToboolean(char)
  567. static Byte charToByte(char)
  568. static Short charToShort(char)
  569. static Integer charToInteger(char)
  570. static Long charToLong(char)
  571. static Float charToFloat(char)
  572. static Double charToDouble(char)
  573. static boolean CharacterToboolean(Character)
  574. static byte CharacterTobyte(Character)
  575. static short CharacterToshort(Character)
  576. static int CharacterToint(Character)
  577. static long CharacterTolong(Character)
  578. static float CharacterTofloat(Character)
  579. static double CharacterTodouble(Character)
  580. static Boolean CharacterToBoolean(Character)
  581. static Byte CharacterToByte(Character)
  582. static Short CharacterToShort(Character)
  583. static Integer CharacterToInteger(Character)
  584. static Long CharacterToLong(Character)
  585. static Float CharacterToFloat(Character)
  586. static Double CharacterToDouble(Character)
  587. static boolean intToboolean(int)
  588. static Byte intToByte(int)
  589. static Short intToShort(int)
  590. static Character intToCharacter(int)
  591. static Long intToLong(int)
  592. static Float intToFloat(int)
  593. static Double intToDouble(int)
  594. static boolean IntegerToboolean(Integer)
  595. static char IntegerTochar(Integer)
  596. static boolean longToboolean(long)
  597. static Byte longToByte(long)
  598. static Short longToShort(long)
  599. static Character longToCharacter(long)
  600. static Integer longToInteger(long)
  601. static Float longToFloat(long)
  602. static Double longToDouble(long)
  603. static boolean LongToboolean(Long)
  604. static char LongTochar(Long)
  605. static boolean floatToboolean(float)
  606. static Byte floatToByte(float)
  607. static Short floatToShort(float)
  608. static Character floatToCharacter(float)
  609. static Integer floatToInteger(float)
  610. static Long floatToLong(float)
  611. static Double floatToDouble(float)
  612. static boolean FloatToboolean(Float)
  613. static char FloatTochar(Float)
  614. static boolean doubleToboolean(double)
  615. static Byte doubleToByte(double)
  616. static Short doubleToShort(double)
  617. static Character doubleToCharacter(double)
  618. static Integer doubleToInteger(double)
  619. static Long doubleToLong(double)
  620. static Float doubleToFloat(double)
  621. static boolean DoubleToboolean(Double)
  622. static char DoubleTochar(Double)
  623. -----
  624. -----
  625. Def
  626. static boolean defToboolean(def)
  627. static byte defTobyte(def)
  628. static short defToshort(def)
  629. static char defTochar(def)
  630. static int defToint(def)
  631. static long defTolong(def)
  632. static float defTofloat(def)
  633. static double defTodouble(def)
  634. static Boolean defToBoolean(def)
  635. static Byte defToByte(def)
  636. static Character defToCharacter(def)
  637. static Integer defToInteger(def)
  638. static Long defToLong(def)
  639. static Float defToFloat(def)
  640. static Double defToDouble(def)
  641. -----