Skip to content

Commit 877c3c2

Browse files
szabosteveswallez
authored andcommitted
[DOCS] Adds getting started content based on the template (#617)
Co-authored-by: Sylvain Wallez <[email protected]>
1 parent 96c3df4 commit 877c3c2

File tree

11 files changed

+247
-6
lines changed

11 files changed

+247
-6
lines changed

docs/getting-started.asciidoc

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
[[getting-started-java]]
2+
== Getting started
3+
4+
This page guides you through the installation process of the Java client, shows
5+
you how to instantiate the client, and how to perform basic Elasticsearch
6+
operations with it.
7+
8+
[discrete]
9+
=== Requirements
10+
11+
* Java 8 or later.
12+
* A JSON object mapping library to allow seamless integration of
13+
your application classes with the Elasticsearch API. The examples below
14+
show usage with Jackson.
15+
16+
[discrete]
17+
=== Installation
18+
19+
[discrete]
20+
==== Installation in a Gradle project by using Jackson
21+
22+
["source","groovy",subs="attributes+"]
23+
--------------------------------------------------
24+
dependencies {
25+
implementation 'co.elastic.clients:elasticsearch-java:{version}'
26+
implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.3'
27+
}
28+
--------------------------------------------------
29+
30+
[discrete]
31+
==== Installation in a Maven project by using Jackson
32+
33+
In the `pom.xml` of your project, add the following repository definition and
34+
dependencies:
35+
36+
["source","xml",subs="attributes+"]
37+
--------------------------------------------------
38+
<project>
39+
<dependencies>
40+
41+
<dependency>
42+
<groupId>co.elastic.clients</groupId>
43+
<artifactId>elasticsearch-java</artifactId>
44+
<version>{version}</version>
45+
</dependency>
46+
47+
<dependency>
48+
<groupId>com.fasterxml.jackson.core</groupId>
49+
<artifactId>jackson-databind</artifactId>
50+
<version>2.12.3</version>
51+
</dependency>
52+
53+
</dependencies>
54+
</project>
55+
--------------------------------------------------
56+
57+
58+
Refer to the <<installation>> page to learn more.
59+
60+
61+
[discrete]
62+
=== Connecting
63+
64+
You can connect to the Elastic Cloud using an API key and the Elasticsearch
65+
endpoint.
66+
67+
["source","java"]
68+
--------------------------------------------------
69+
include-tagged::{doc-tests-src}/getting_started/ConnectingTest.java[create-client]
70+
--------------------------------------------------
71+
72+
Your Elasticsearch endpoint can be found on the **My deployment** page of your
73+
deployment:
74+
75+
image::images/es-endpoint.jpg[alt="Finding Elasticsearch endpoint",align="center"]
76+
77+
You can generate an API key on the **Management** page under Security.
78+
79+
image::images/create-api-key.png[alt="Create API key",align="center"]
80+
81+
For other connection options, refer to the <<connecting>> section.
82+
83+
84+
[discrete]
85+
=== Operations
86+
87+
Time to use Elasticsearch! This section walks you through the basic, and most
88+
important, operations of Elasticsearch. For more operations and more advanced
89+
examples, refer to the <<usage>> page.
90+
91+
92+
[discrete]
93+
==== Creating an index
94+
95+
This is how you create the `product` index:
96+
97+
["source","java"]
98+
--------------------------------------------------
99+
include-tagged::{doc-tests-src}/usage/IndexingTest.java[create-products-index]
100+
--------------------------------------------------
101+
102+
[discrete]
103+
==== Indexing documents
104+
105+
This is a simple way of indexing a document, here a `Product` application object:
106+
107+
["source","java"]
108+
--------------------------------------------------
109+
include-tagged::{doc-tests-src}/usage/IndexingTest.java[single-doc-dsl]
110+
--------------------------------------------------
111+
112+
[discrete]
113+
==== Getting documents
114+
115+
You can get documents by using the following code:
116+
117+
["source","java"]
118+
--------------------------------------------------
119+
include-tagged::{doc-tests-src}/usage/ReadingTest.java[get-by-id]
120+
--------------------------------------------------
121+
<1> The get request, with the index name and identifier.
122+
<2> The target class, here `Product`.
123+
124+
125+
[discrete]
126+
==== Searching documents
127+
128+
This is how you can create a single match query with the Java client:
129+
130+
["source","java"]
131+
--------------------------------------------------
132+
include-tagged::{doc-tests-src}/usage/SearchingTest.java[search-getting-started]
133+
--------------------------------------------------
134+
135+
[discrete]
136+
==== Updating documents
137+
138+
This is how you can update a document, for example to add a new field:
139+
140+
["source","java"]
141+
--------------------------------------------------
142+
include-tagged::{doc-tests-src}/usage/IndexingTest.java[single-doc-update]
143+
--------------------------------------------------
144+
145+
146+
147+
[discrete]
148+
==== Deleting documents
149+
150+
["source","java"]
151+
--------------------------------------------------
152+
include-tagged::{doc-tests-src}/usage/IndexingTest.java[single-doc-delete]
153+
--------------------------------------------------
154+
155+
156+
[discrete]
157+
==== Deleting an index
158+
159+
["source","java"]
160+
--------------------------------------------------
161+
include-tagged::{doc-tests-src}/usage/IndexingTest.java[create-products-index]
162+
--------------------------------------------------
163+
164+
165+
[discrete]
166+
== Further reading
167+
168+
* Learn more about the <<api-conventions>> of the Java client.

docs/images/create-api-key.png

78.7 KB
Loading

docs/images/es-endpoint.jpg

361 KB
Loading

docs/index.asciidoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ ifeval::["{release-state}"!="unreleased"]
2727
endif::[]
2828

2929
include::introduction.asciidoc[]
30-
include::getting-started/index.asciidoc[]
30+
include::getting-started.asciidoc[]
31+
include::setup/index.asciidoc[]
3132
include::api-conventions/index.asciidoc[]
3233

3334
include::usage/index.asciidoc[]

docs/getting-started/index.asciidoc renamed to docs/setup/index.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
== Getting started
1+
== Setup
22

33
* <<installation>>
44
* <<connecting>>

java-client/src/test/java/co/elastic/clients/documentation/getting_started/ConnectingTest.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
import co.elastic.clients.transport.ElasticsearchTransport;
2828
import co.elastic.clients.transport.TransportUtils;
2929
import co.elastic.clients.transport.rest_client.RestClientTransport;
30+
import org.apache.http.Header;
3031
import org.apache.http.HttpHost;
3132
import org.apache.http.auth.AuthScope;
3233
import org.apache.http.auth.UsernamePasswordCredentials;
3334
import org.apache.http.impl.client.BasicCredentialsProvider;
35+
import org.apache.http.message.BasicHeader;
3436
import org.elasticsearch.client.RestClient;
3537
import org.junit.jupiter.api.Disabled;
3638
import org.junit.jupiter.api.Test;
@@ -44,20 +46,28 @@ public class ConnectingTest {
4446
@Test
4547
public void createClient() throws Exception {
4648
//tag::create-client
49+
// URL and API key
50+
String serverUrl = "https://localhost:9200";
51+
String apiKey = "VnVhQ2ZHY0JDZGJrU...";
52+
4753
// Create the low-level client
48-
RestClient restClient = RestClient.builder(
49-
new HttpHost("localhost", 9200)).build();
54+
RestClient restClient = RestClient
55+
.builder(HttpHost.create(serverUrl))
56+
.setDefaultHeaders(new Header[]{
57+
new BasicHeader("Authorization", "ApiKey " + apiKey)
58+
})
59+
.build();
5060

5161
// Create the transport with a Jackson mapper
5262
ElasticsearchTransport transport = new RestClientTransport(
5363
restClient, new JacksonJsonpMapper());
5464

5565
// And create the API client
56-
ElasticsearchClient client = new ElasticsearchClient(transport);
66+
ElasticsearchClient esClient = new ElasticsearchClient(transport);
5767
//end::create-client
5868

5969
//tag::first-request
60-
SearchResponse<Product> search = client.search(s -> s
70+
SearchResponse<Product> search = esClient.search(s -> s
6171
.index("products")
6272
.query(q -> q
6373
.term(t -> t

java-client/src/test/java/co/elastic/clients/documentation/usage/IndexingTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import co.elastic.clients.elasticsearch.core.IndexResponse;
2828
import co.elastic.clients.elasticsearch.model.ModelTestCase;
2929
import co.elastic.clients.json.JsonData;
30+
import org.junit.jupiter.api.Disabled;
3031
import org.junit.jupiter.api.Test;
3132
import org.slf4j.Logger;
3233
import org.slf4j.LoggerFactory;
@@ -51,6 +52,26 @@ public class IndexingTest extends ModelTestCase {
5152
.shards(s -> s.total(1).successful(1).failed(0))
5253
);
5354

55+
@Test
56+
@Disabled
57+
public void createIndex() throws Exception {
58+
//tag::create-products-index
59+
esClient.indices().create(c -> c
60+
.index("products")
61+
);
62+
//end::create-products-index
63+
}
64+
65+
@Test
66+
@Disabled
67+
public void deleteIndex() throws Exception {
68+
//tag::delete-products-index
69+
esClient.indices().delete(d -> d
70+
.index("products")
71+
);
72+
//end::delete-products-index
73+
}
74+
5475
@Test
5576
public void singleDocumentDSL() throws Exception {
5677

@@ -162,4 +183,26 @@ public void singleDocumentJson() throws Exception {
162183
toJson(request)
163184
);
164185
}
186+
187+
@Test
188+
public void deleteDocument() throws Exception {
189+
//tag::single-doc-delete
190+
esClient.delete(d -> d.index("products").id("bk-1"));
191+
//end::single-doc-delete
192+
}
193+
194+
@Test
195+
@Disabled
196+
public void updateDoc() throws Exception {
197+
//tag::single-doc-update
198+
Product product = new Product("bk-1", "City bike", 123.0);
199+
200+
esClient.update(u -> u
201+
.index("products")
202+
.id("bk-1")
203+
.upsert(product),
204+
Product.class
205+
);
206+
//end::single-doc-update
207+
}
165208
}

java-client/src/test/java/co/elastic/clients/documentation/usage/SearchingTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@ public class SearchingTest {
6161
)
6262
);
6363

64+
@Test
65+
public void searchSimpleMatch() throws Exception {
66+
transport.setResult(searchResponse);
67+
68+
//tag::search-getting-started
69+
String searchText = "bike";
70+
71+
SearchResponse<Product> response = esClient.search(s -> s
72+
.index("products")
73+
.query(q -> q
74+
.match(t -> t
75+
.field("name")
76+
.query(searchText)
77+
)
78+
),
79+
Product.class
80+
);
81+
//end::search-getting-started
82+
}
6483
@Test
6584
public void searchMatch() throws Exception {
6685

0 commit comments

Comments
 (0)