client.asciidoc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. [[client]]
  2. == Client
  3. Obtaining an elasticsearch Groovy `GClient` (a `GClient` is a simple
  4. wrapper on top of the Java `Client`) is simple. The most common way to
  5. get a client is by starting an embedded `Node` which acts as a node
  6. within the cluster.
  7. === Node Client
  8. A Node based client is the simplest form to get a `GClient` to start
  9. executing operations against elasticsearch.
  10. [source,js]
  11. --------------------------------------------------
  12. import org.elasticsearch.groovy.client.GClient
  13. import org.elasticsearch.groovy.node.GNode
  14. import static org.elasticsearch.groovy.node.GNodeBuilder.nodeBuilder
  15. // on startup
  16. GNode node = nodeBuilder().node();
  17. GClient client = node.client();
  18. // on shutdown
  19. node.close();
  20. --------------------------------------------------
  21. Since elasticsearch allows to configure it using JSON based settings,
  22. the configuration itself can be done using a closure that represent the
  23. JSON:
  24. [source,js]
  25. --------------------------------------------------
  26. import org.elasticsearch.groovy.node.GNode
  27. import org.elasticsearch.groovy.node.GNodeBuilder
  28. import static org.elasticsearch.groovy.node.GNodeBuilder.*
  29. // on startup
  30. GNodeBuilder nodeBuilder = nodeBuilder();
  31. nodeBuilder.settings {
  32. node {
  33. client = true
  34. }
  35. cluster {
  36. name = "test"
  37. }
  38. }
  39. GNode node = nodeBuilder.node()
  40. // on shutdown
  41. node.stop().close()
  42. --------------------------------------------------