summaryrefslogtreecommitdiff
path: root/docs/groovy-api/anatomy.asciidoc
blob: 4f0f18b4916a350beeb5a8885e0b824eb1a2425c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
[[anatomy]]
== API Anatomy

Once a <<client,GClient>> has been
obtained, all of ElasticSearch APIs can be executed on it. Each Groovy
API is exposed using three different mechanisms.

[float]
=== 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
elasticsearch Java `ActionFuture` (in turn a nicer extension to Java own
`Future`) which allows to register listeners (closures) on it for
success and failures, as well as blocking for the response. For example:

[source,js]
--------------------------------------------------
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:

[source,js]
--------------------------------------------------
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:

[source,js]
--------------------------------------------------
indexR.success = {IndexResponse response ->
    pritnln "Indexed $response.id into $response.index/$response.type"
}
indexR.failure = {Throwable t ->
    println "Failed to index: $t.message"
}
--------------------------------------------------

[float]
=== 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:

[source,js]
--------------------------------------------------
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"
--------------------------------------------------

[float]
=== 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`:

[source,js]
--------------------------------------------------
def indexR = node.client.prepareIndex("test", "type1", "1").setSource({
    test = "value"
    complex {
        value1 = "value1"
        value2 = "value2"
    }
}).gexecute()
--------------------------------------------------