2013-08-28 19:24:34 -04:00
|
|
|
[[anatomy]]
|
|
|
|
== API Anatomy
|
|
|
|
|
|
|
|
Once a <<client,GClient>> has been
|
2014-01-06 15:58:46 -05:00
|
|
|
obtained, all of Elasticsearch APIs can be executed on it. Each Groovy
|
2013-08-28 19:24:34 -04:00
|
|
|
API is exposed using three different mechanisms.
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
[[closure]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== Closure Request
|
|
|
|
|
|
|
|
The first type is to simply provide the request as a Closure, which
|
|
|
|
automatically gets resolved into the respective request instance (for
|
|
|
|
the index API, its the `IndexRequest` class). The API returns a special
|
|
|
|
future, called `GActionFuture`. This is a groovier version of
|
2017-11-29 03:44:25 -05:00
|
|
|
Elasticsearch Java `ActionFuture` (in turn a nicer extension to Java own
|
2013-08-28 19:24:34 -04:00
|
|
|
`Future`) which allows to register listeners (closures) on it for
|
|
|
|
success and failures, as well as blocking for the response. For example:
|
|
|
|
|
2016-08-29 17:33:25 -04:00
|
|
|
[source,groovy]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
def indexR = client.index {
|
|
|
|
index "test"
|
|
|
|
type "type1"
|
|
|
|
id "1"
|
|
|
|
source {
|
|
|
|
test = "value"
|
|
|
|
complex {
|
|
|
|
value1 = "value1"
|
|
|
|
value2 = "value2"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
println "Indexed $indexR.response.id into $indexR.response.index/$indexR.response.type"
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
In the above example, calling `indexR.response` will simply block for
|
|
|
|
the response. We can also block for the response for a specific timeout:
|
|
|
|
|
2016-08-29 17:33:25 -04:00
|
|
|
[source,groovy]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
IndexResponse response = indexR.response "5s" // block for 5 seconds, same as:
|
|
|
|
response = indexR.response 5, TimeValue.SECONDS //
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
We can also register closures that will be called on success and on
|
|
|
|
failure:
|
|
|
|
|
2016-08-29 17:33:25 -04:00
|
|
|
[source,groovy]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
indexR.success = {IndexResponse response ->
|
2015-05-24 08:13:51 -04:00
|
|
|
println "Indexed $response.id into $response.index/$response.type"
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
indexR.failure = {Throwable t ->
|
|
|
|
println "Failed to index: $t.message"
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
[[request]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== Request
|
|
|
|
|
|
|
|
This option allows to pass the actual instance of the request (instead
|
|
|
|
of a closure) as a parameter. The rest is similar to the closure as a
|
|
|
|
parameter option (the `GActionFuture` handling). For example:
|
|
|
|
|
2016-08-29 17:33:25 -04:00
|
|
|
[source,groovy]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
def indexR = client.index (new IndexRequest(
|
|
|
|
index: "test",
|
|
|
|
type: "type1",
|
|
|
|
id: "1",
|
|
|
|
source: {
|
|
|
|
test = "value"
|
|
|
|
complex {
|
|
|
|
value1 = "value1"
|
|
|
|
value2 = "value2"
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
println "Indexed $indexR.response.id into $indexR.response.index/$indexR.response.type"
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
[[java-like]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== Java Like
|
|
|
|
|
|
|
|
The last option is to provide an actual instance of the API request, and
|
|
|
|
an `ActionListener` for the callback. This is exactly like the Java API
|
|
|
|
with the added `gexecute` which returns the `GActionFuture`:
|
|
|
|
|
2016-08-29 17:33:25 -04:00
|
|
|
[source,groovy]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
def indexR = node.client.prepareIndex("test", "type1", "1").setSource({
|
|
|
|
test = "value"
|
|
|
|
complex {
|
|
|
|
value1 = "value1"
|
|
|
|
value2 = "value2"
|
|
|
|
}
|
|
|
|
}).gexecute()
|
|
|
|
--------------------------------------------------
|