painless-syntax.asciidoc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. [[painless-syntax]]
  2. === Painless Syntax
  3. [float]
  4. [[control-flow]]
  5. ==== Control flow
  6. Painless supports all of Java's https://docs.oracle.com/javase/tutorial/java/nutsandbolts/flow.html[
  7. control flow statements] except the `switch` statement.
  8. Painless also supports the `for in` syntax from Groovy:
  9. [source,painless]
  10. ---------------------------------------------------------
  11. for (item : list) {
  12. ...
  13. }
  14. ---------------------------------------------------------
  15. [float]
  16. [[functions]]
  17. ==== Functions
  18. You can declare functions at the beginning of a Painless script, for example:
  19. [source,painless]
  20. ---------------------------------------------------------
  21. boolean isNegative(def x) { x < 0 }
  22. ...
  23. if (isNegative(someVar)) {
  24. ...
  25. }
  26. ---------------------------------------------------------
  27. [float]
  28. [[lambda-expressions]]
  29. ==== Lambda expressions
  30. Lambda expressions and method references work the same as in https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html[Java].
  31. [source,painless]
  32. ---------------------------------------------------------
  33. list.removeIf(item -> item == 2);
  34. list.removeIf((int item) -> item == 2);
  35. list.removeIf((int item) -> { item == 2 });
  36. list.sort((x, y) -> x - y);
  37. list.sort(Integer::compare);
  38. ---------------------------------------------------------
  39. You can make method references to functions within the script with `this`,
  40. for example `list.sort(this::mycompare)`.
  41. [float]
  42. [[patterns]]
  43. ==== Patterns
  44. Regular expression constants are directly supported. To ensure fast performance,
  45. this is the only mechanism for creating patterns. Regular expressions
  46. are always constants and compiled efficiently a single time.
  47. [source,painless]
  48. ---------------------------------------------------------
  49. Pattern p = /[aeiou]/
  50. ---------------------------------------------------------
  51. [float]
  52. [[pattern-flags]]
  53. ===== Pattern flags
  54. You can define flags on patterns in Painless by adding characters after the
  55. trailing `/` like `/foo/i` or `/foo \w #comment/iUx`. Painless exposes all of
  56. the flags from Java's
  57. https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html[
  58. Pattern class] using these characters:
  59. [cols="<,<,<",options="header",]
  60. |=======================================================================
  61. | Character | Java Constant | Example
  62. |`c` | CANON_EQ | `'å' ==~ /å/c` (open in hex editor to see)
  63. |`i` | CASE_INSENSITIVE | `'A' ==~ /a/i`
  64. |`l` | LITERAL | `'[a]' ==~ /[a]/l`
  65. |`m` | MULTILINE | `'a\nb\nc' =~ /^b$/m`
  66. |`s` | DOTALL (aka single line) | `'a\nb\nc' =~ /.b./s`
  67. |`U` | UNICODE_CHARACTER_CLASS | `'Ɛ' ==~ /\\w/U`
  68. |`u` | UNICODE_CASE | `'Ɛ' ==~ /ɛ/iu`
  69. |`x` | COMMENTS (aka extended) | `'a' ==~ /a #comment/x`
  70. |=======================================================================
  71. [float]
  72. [[painless-deref]]
  73. ==== Dereferences
  74. Like lots of languages, Painless uses `.` to reference fields and call methods:
  75. [source,painless]
  76. ---------------------------------------------------------
  77. String foo = 'foo';
  78. TypeWithGetterOrPublicField bar = new TypeWithGetterOrPublicField()
  79. return foo.length() + bar.x
  80. ---------------------------------------------------------
  81. Like Groovy, Painless uses `?.` to perform null-safe references, with the
  82. result being `null` if the left hand side is `null`:
  83. [source,painless]
  84. ---------------------------------------------------------
  85. String foo = null;
  86. return foo?.length() // Returns null
  87. ---------------------------------------------------------
  88. Unlike Groovy, Painless doesn't support writing to `null` values with this
  89. operator:
  90. [source,painless]
  91. ---------------------------------------------------------
  92. TypeWithSetterOrPublicField foo = null;
  93. foo?.x = 'bar' // Compile error
  94. ---------------------------------------------------------