2013-08-28 19:24:34 -04:00
|
|
|
[[client]]
|
|
|
|
== Client
|
|
|
|
|
2017-11-29 03:44:25 -05:00
|
|
|
Obtaining an Elasticsearch Groovy `GClient` (a `GClient` is a simple
|
2013-08-28 19:24:34 -04:00
|
|
|
wrapper on top of the Java `Client`) is simple. The most common way to
|
|
|
|
get a client is by starting an embedded `Node` which acts as a node
|
|
|
|
within the cluster.
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
[[node-client]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== Node Client
|
|
|
|
|
|
|
|
A Node based client is the simplest form to get a `GClient` to start
|
2017-11-29 03:44:25 -05:00
|
|
|
executing operations against Elasticsearch.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2016-08-29 17:33:25 -04:00
|
|
|
[source,groovy]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
import org.elasticsearch.groovy.client.GClient
|
|
|
|
import org.elasticsearch.groovy.node.GNode
|
|
|
|
import static org.elasticsearch.groovy.node.GNodeBuilder.nodeBuilder
|
|
|
|
|
|
|
|
// on startup
|
|
|
|
|
|
|
|
GNode node = nodeBuilder().node();
|
|
|
|
GClient client = node.client();
|
|
|
|
|
|
|
|
// on shutdown
|
|
|
|
|
|
|
|
node.close();
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2017-11-29 03:44:25 -05:00
|
|
|
Since Elasticsearch allows to configure it using JSON based settings,
|
2013-08-28 19:24:34 -04:00
|
|
|
the configuration itself can be done using a closure that represent the
|
|
|
|
JSON:
|
|
|
|
|
2016-08-29 17:33:25 -04:00
|
|
|
[source,groovy]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
import org.elasticsearch.groovy.node.GNode
|
|
|
|
import org.elasticsearch.groovy.node.GNodeBuilder
|
|
|
|
import static org.elasticsearch.groovy.node.GNodeBuilder.*
|
|
|
|
|
|
|
|
// on startup
|
|
|
|
|
|
|
|
GNodeBuilder nodeBuilder = nodeBuilder();
|
|
|
|
nodeBuilder.settings {
|
|
|
|
node {
|
|
|
|
client = true
|
|
|
|
}
|
|
|
|
cluster {
|
|
|
|
name = "test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GNode node = nodeBuilder.node()
|
|
|
|
|
|
|
|
// on shutdown
|
|
|
|
|
|
|
|
node.stop().close()
|
|
|
|
--------------------------------------------------
|