painless.asciidoc 19 KB

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