Skip to content

Commit 2397302

Browse files
authored
Fill the blanks in the Serverless getting started (#681)
1 parent 4e6219d commit 2397302

File tree

1 file changed

+95
-22
lines changed

1 file changed

+95
-22
lines changed

java-client-serverless/docs/getting-started.mdx

Lines changed: 95 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,77 @@ date: 2023-06-27
77
tags: ['serverless','Java client','docs', 'getting started', 'Java']
88
---
99

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
1111
client, shows you how to instantiate the client, and how to perform basic
1212
Elasticsearch operations with it.
1313

1414
## Requirements
1515

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.
1720

1821
## Installation
1922

20-
### Using the command line
23+
You can add the Elasticsearch Serverless Java client to your Java project using either Gradle or Maven.
2124

22-
You can install the Elasticsearch Serverless Java client with the following
23-
commands:
25+
### Using Gradle
2426

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+
}
2632
```
2733

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+
```
2857

2958
## Instantiate a client
3059

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:
3261

3362
```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);
3581
```
3682

3783
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**:
4692
## Using the API
4793

4894
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.
5296

5397

5498
### Creating an index and ingesting documents
5599

56100
You can call the `bulk` API with a body parameter, an array of hashes that
57101
define the action, and a document.
58102

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:
61105

62106
```java
107+
Product product = new Product("bk-1", "City bike", 123.0);
63108

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+
);
71114

115+
logger.info("Indexed with version " + response.version());
116+
```
72117

73118
### Searching
74119

75120
Now that some data is available, you can search your documents using the
76121
**Search API**:
77122

78123
```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+
);
79136
```
80137

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+
81145
### Updating
82146

83-
You can update your documents using the Bulk API:
147+
You can update your documents using the Update API:
84148

85149
```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+
);
86158
```
87159

88160
### Delete
89161

90162
You can also delete documents:
91163

92164
```java
93-
```
165+
esClient.delete(d -> d.index("products").id("bk-1"));
166+
```

0 commit comments

Comments
 (0)