painless.asciidoc 19 KB

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