anatomy.asciidoc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. [[anatomy]]
  2. == API Anatomy
  3. Once a <<client,GClient>> has been
  4. obtained, all of Elasticsearch APIs can be executed on it. Each Groovy
  5. API is exposed using three different mechanisms.
  6. [[closure]]
  7. === Closure Request
  8. The first type is to simply provide the request as a Closure, which
  9. automatically gets resolved into the respective request instance (for
  10. the index API, its the `IndexRequest` class). The API returns a special
  11. future, called `GActionFuture`. This is a groovier version of
  12. elasticsearch Java `ActionFuture` (in turn a nicer extension to Java own
  13. `Future`) which allows to register listeners (closures) on it for
  14. success and failures, as well as blocking for the response. For example:
  15. [source,groovy]
  16. --------------------------------------------------
  17. def indexR = client.index {
  18. index "test"
  19. type "type1"
  20. id "1"
  21. source {
  22. test = "value"
  23. complex {
  24. value1 = "value1"
  25. value2 = "value2"
  26. }
  27. }
  28. }
  29. println "Indexed $indexR.response.id into $indexR.response.index/$indexR.response.type"
  30. --------------------------------------------------
  31. In the above example, calling `indexR.response` will simply block for
  32. the response. We can also block for the response for a specific timeout:
  33. [source,groovy]
  34. --------------------------------------------------
  35. IndexResponse response = indexR.response "5s" // block for 5 seconds, same as:
  36. response = indexR.response 5, TimeValue.SECONDS //
  37. --------------------------------------------------
  38. We can also register closures that will be called on success and on
  39. failure:
  40. [source,groovy]
  41. --------------------------------------------------
  42. indexR.success = {IndexResponse response ->
  43. println "Indexed $response.id into $response.index/$response.type"
  44. }
  45. indexR.failure = {Throwable t ->
  46. println "Failed to index: $t.message"
  47. }
  48. --------------------------------------------------
  49. [[request]]
  50. === Request
  51. This option allows to pass the actual instance of the request (instead
  52. of a closure) as a parameter. The rest is similar to the closure as a
  53. parameter option (the `GActionFuture` handling). For example:
  54. [source,groovy]
  55. --------------------------------------------------
  56. def indexR = client.index (new IndexRequest(
  57. index: "test",
  58. type: "type1",
  59. id: "1",
  60. source: {
  61. test = "value"
  62. complex {
  63. value1 = "value1"
  64. value2 = "value2"
  65. }
  66. }))
  67. println "Indexed $indexR.response.id into $indexR.response.index/$indexR.response.type"
  68. --------------------------------------------------
  69. [[java-like]]
  70. === Java Like
  71. The last option is to provide an actual instance of the API request, and
  72. an `ActionListener` for the callback. This is exactly like the Java API
  73. with the added `gexecute` which returns the `GActionFuture`:
  74. [source,groovy]
  75. --------------------------------------------------
  76. def indexR = node.client.prepareIndex("test", "type1", "1").setSource({
  77. test = "value"
  78. complex {
  79. value1 = "value1"
  80. value2 = "value2"
  81. }
  82. }).gexecute()
  83. --------------------------------------------------