client.asciidoc 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. === Node Client
  9. A Node based client is the simplest form to get a `GClient` to start
  10. executing operations against elasticsearch.
  11. [source,js]
  12. --------------------------------------------------
  13. import org.elasticsearch.groovy.client.GClient
  14. import org.elasticsearch.groovy.node.GNode
  15. import static org.elasticsearch.groovy.node.GNodeBuilder.nodeBuilder
  16. // on startup
  17. GNode node = nodeBuilder().node();
  18. GClient client = node.client();
  19. // on shutdown
  20. node.close();
  21. --------------------------------------------------
  22. Since elasticsearch allows to configure it using JSON based settings,
  23. the configuration itself can be done using a closure that represent the
  24. JSON:
  25. [source,js]
  26. --------------------------------------------------
  27. import org.elasticsearch.groovy.node.GNode
  28. import org.elasticsearch.groovy.node.GNodeBuilder
  29. import static org.elasticsearch.groovy.node.GNodeBuilder.*
  30. // on startup
  31. GNodeBuilder nodeBuilder = nodeBuilder();
  32. nodeBuilder.settings {
  33. node {
  34. client = true
  35. }
  36. cluster {
  37. name = "test"
  38. }
  39. }
  40. GNode node = nodeBuilder.node()
  41. // on shutdown
  42. node.stop().close()
  43. --------------------------------------------------