@@ -7,31 +7,77 @@ date: 2023-06-27
7
7
tags : ['serverless','Java client','docs', 'getting started', 'Java']
8
8
---
9
9
10
- This page guides you through the installation process of the Serverless Java
10
+ This page guides you through the installation process of the Serverless Java
11
11
client, shows you how to instantiate the client, and how to perform basic
12
12
Elasticsearch operations with it.
13
13
14
14
## Requirements
15
15
16
- [ TO DO]
16
+ * Java 8 or later.
17
+ * A JSON object mapping library to allow seamless integration of
18
+ your application classes with the Elasticsearch API. The examples below
19
+ show usage with Jackson.
17
20
18
21
## Installation
19
22
20
- ### Using the command line
23
+ You can add the Elasticsearch Serverless Java client to your Java project using either Gradle or Maven.
21
24
22
- You can install the Elasticsearch Serverless Java client with the following
23
- commands:
25
+ ### Using Gradle
24
26
25
- ``` bash
27
+ ``` groovy
28
+ dependencies {
29
+ implementation 'co.elastic.clients:elasticsearch-java-serverless:1.0.0-20231031'
30
+ implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.3'
31
+ }
26
32
```
27
33
34
+ ### Using Maven
35
+
36
+ In the ` pom.xml ` of your project, add the following dependencies:
37
+
38
+ ``` xml
39
+ <project >
40
+ <dependencies >
41
+
42
+ <dependency >
43
+ <groupId >co.elastic.clients</groupId >
44
+ <artifactId >elasticsearch-java-serverless</artifactId >
45
+ <version >1.0.0-20231031</version >
46
+ </dependency >
47
+
48
+ <dependency >
49
+ <groupId >com.fasterxml.jackson.core</groupId >
50
+ <artifactId >jackson-databind</artifactId >
51
+ <version >2.12.3</version >
52
+ </dependency >
53
+
54
+ </dependencies >
55
+ </project >
56
+ ```
28
57
29
58
## Instantiate a client
30
59
31
- You can instantiate a client by running the following command :
60
+ You can connect to the Elasticsearch Service using an API key and the Elasticsearch endpoint :
32
61
33
62
``` java
34
-
63
+ // URL and API key
64
+ String serverUrl = " https://...elastic.cloud" ;
65
+ String apiKey = " VnVhQ2ZHY0JDZGJrU..." ;
66
+
67
+ // Create the low-level client
68
+ RestClient restClient = RestClient
69
+ .builder(HttpHost . create(serverUrl))
70
+ .setDefaultHeaders(new Header []{
71
+ new BasicHeader (" Authorization" , " ApiKey " + apiKey)
72
+ })
73
+ .build();
74
+
75
+ // Create the transport with a Jackson mapper
76
+ ElasticsearchTransport transport = new RestClientTransport (
77
+ restClient, new JacksonJsonpMapper ());
78
+
79
+ // And create the API client
80
+ ElasticsearchClient esClient = new ElasticsearchClient (transport);
35
81
```
36
82
37
83
You can find the Elasticsearch endpoint on the Cloud deployment management page.
@@ -46,48 +92,75 @@ You can create a new API Key under **Stack Management** > **Security**:
46
92
## Using the API
47
93
48
94
After you instantiated a client with your API key and Elasticsearch endpoint,
49
- you can start ingesting documents into the Elasticsearch Service. You can use
50
- the Bulk API for this. This API enables you to index, update, and delete several
51
- documents in one request.
95
+ you can start ingesting documents into the Elasticsearch Service.
52
96
53
97
54
98
### Creating an index and ingesting documents
55
99
56
100
You can call the ` bulk ` API with a body parameter, an array of hashes that
57
101
define the action, and a document.
58
102
59
- The following is an example of indexing some classic books into the ` books `
60
- index:
103
+ The following is an example of indexing a document, here a ` Product ` application object
104
+ in the ` products ` index:
61
105
62
106
``` java
107
+ Product product = new Product (" bk-1" , " City bike" , 123.0 );
63
108
64
- ```
65
-
66
- When you use the client to make a request to Elasticsearch, it returns an API
67
- response object. You can check the HTTP return code by calling ` status ` and the
68
- HTTP headers by calling ` headers ` on the response object. The response object
69
- also behaves as a Hash, so you can access the body values directly as seen on
70
- the previous example with ``.
109
+ IndexResponse response = esClient. index(i - > i
110
+ .index(" products" )
111
+ .id(product. getSku())
112
+ .document(product)
113
+ );
71
114
115
+ logger. info(" Indexed with version " + response. version());
116
+ ```
72
117
73
118
### Searching
74
119
75
120
Now that some data is available, you can search your documents using the
76
121
** Search API** :
77
122
78
123
``` java
124
+ String searchText = " bike" ;
125
+
126
+ SearchResponse<Product > response = esClient. search(s - > s
127
+ .index(" products" )
128
+ .query(q - > q
129
+ .match(t - > t
130
+ .field(" name" )
131
+ .query(searchText)
132
+ )
133
+ ),
134
+ Product . class
135
+ );
79
136
```
80
137
138
+ A few things to note in the above example:
139
+ * the search query is built using a hierarchy of lambda expressions that closely follows the
140
+ Elasticsearch JSON API. Lambda expressions allows you to be guided by your IDE's autocompletion, without
141
+ having to import (or even know!) the actual classes representing a query.
142
+ * The last parameter ` Product.class ` instructs the client to return results as ` Product ` application objects
143
+ instead of raw JSON.
144
+
81
145
### Updating
82
146
83
- You can update your documents using the Bulk API:
147
+ You can update your documents using the Update API:
84
148
85
149
``` java
150
+ Product product = new Product (" bk-1" , " City bike" , 123.0 );
151
+
152
+ esClient. update(u - > u
153
+ .index(" products" )
154
+ .id(" bk-1" )
155
+ .upsert(product),
156
+ Product . class
157
+ );
86
158
```
87
159
88
160
### Delete
89
161
90
162
You can also delete documents:
91
163
92
164
``` java
93
- ```
165
+ esClient. delete(d - > d. index(" products" ). id(" bk-1" ));
166
+ ```
0 commit comments