Skip to content

Commit c8908c8

Browse files
authored
Restructure reference docs (#988)
1 parent d02b391 commit c8908c8

File tree

87 files changed

+1877
-558
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+1877
-558
lines changed

docs/docset.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ project: 'Java API Client'
22
exclude:
33
- external-resources.md
44
- design/*
5-
- local/README.md
65
cross_links:
76
- docs-content
87
- elasticsearch
98
toc:
109
- toc: reference
1110
- toc: release-notes
1211
subs:
13-
serverless-full: "Elastic Cloud Serverless"
14-
es3: "Elasticsearch Serverless"
1512
es: "Elasticsearch"
13+
version: "9.0.0"
14+
# Not used by docs-builder, but present in `% :::{include-bloc}` preprocessing directives (see IncludeExpander.java)
15+
doc-tests-src: "../../java-client/src/test/java/co/elastic/clients/documentation"

docs/local/README.md

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The source code for the examples above can be found in the [Java API Client tests](https://github.com/elastic/elasticsearch-java/tree/main/java-client/src/test/java/co/elastic/clients/documentation).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:::{note}
2+
This is the legacy RestClient. Please migrate to [Rest5Client](/reference/transport/rest5-client/index.md), a drop-in replacement with an up-to-date http library.
3+
:::

docs/reference/api-conventions.md

-28
This file was deleted.

docs/reference/blocking-async.md docs/reference/api-conventions/blocking-async.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ API clients come in two flavors: blocking and asynchronous. All methods on async
99

1010
Both flavors can be used at the same time depending on your needs, sharing the same transport object:
1111

12+
% :::{include-code} src={{doc-tests-src}}/api_conventions/ApiConventionsTest.java tag=blocking-and-async
1213
```java
13-
ElasticsearchTransport transport = ...
14-
1514
// Synchronous blocking client
16-
ElasticsearchClient client = new ElasticsearchClient(transport);
15+
ElasticsearchClient esClient = new ElasticsearchClient(transport);
1716

18-
if (client.exists(b -> b.index("products").id("foo")).value()) {
17+
if (esClient.exists(b -> b.index("products").id("foo")).value()) {
1918
logger.info("product exists");
2019
}
2120

@@ -36,5 +35,6 @@ asyncClient
3635

3736
Although we won’t go in deeper details on asynchronous programming in Java, remember to handle failures of asynchronous tasks. It’s easy to overlook them and have errors go unnoticed.
3837

39-
The source code for the examples above can be found in the [Java API Client tests](https://github.com/elastic/elasticsearch-java/tree/master/java-client/src/test/java/co/elastic/clients/documentation).
38+
:::{include} /reference/_snippets/doc-tests-blurb.md
39+
:::
4040

docs/reference/building-objects.md docs/reference/api-conventions/building-objects.md

+15-10
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ mapped_pages:
1010

1111
All data types in the Java API Client are immutable. Object creation uses the [builder pattern](https://www.informit.com/articles/article.aspx?p=1216151&seqNum=2) that was popularized in **Effective Java** in 2008.
1212

13+
% :::{include-code} src={{doc-tests-src}}/api_conventions/ApiConventionsTest.java tag=builders
1314
```java
14-
ElasticsearchClient client = ...
15-
CreateIndexResponse createResponse = client.indices().create(
15+
ElasticsearchClient esClient = createClient();
16+
CreateIndexResponse createResponse = esClient.indices().create(
1617
new CreateIndexRequest.Builder()
1718
.index("my-index")
1819
.aliases("foo",
@@ -29,9 +30,10 @@ Note that a builder should not be reused after its `build()` method has been cal
2930

3031
Although this works nicely, having to instantiate builder classes and call the `build()` method is a bit verbose. So every property setter in the Java API Client also accepts a lambda expression that takes a newly created builder as a parameter and returns a populated builder. The snippet above can also be written as:
3132

33+
% :::{include-code} src={{doc-tests-src}}/api_conventions/ApiConventionsTest.java tag=builder-lambdas
3234
```java
33-
ElasticsearchClient client = ...
34-
CreateIndexResponse createResponse = client.indices()
35+
ElasticsearchClient esClient = createClient();
36+
CreateIndexResponse createResponse = esClient.indices()
3537
.create(createIndexBuilder -> createIndexBuilder
3638
.index("my-index")
3739
.aliases("foo", aliasBuilder -> aliasBuilder
@@ -44,9 +46,10 @@ This approach allows for much more concise code, and also avoids importing class
4446

4547
Note in the above example that builder variables are only used to start a chain of property setters. The names of these variables are therefore unimportant and can be shortened to improve readability:
4648

49+
% :::{include-code} src={{doc-tests-src}}/api_conventions/ApiConventionsTest.java tag=builder-lambdas-short
4750
```java
48-
ElasticsearchClient client = ...
49-
CreateIndexResponse createResponse = client.indices()
51+
ElasticsearchClient esClient = createClient();
52+
CreateIndexResponse createResponse = esClient.indices()
5053
.create(c -> c
5154
.index("my-index")
5255
.aliases("foo", a -> a
@@ -59,9 +62,10 @@ Builder lambdas become particularly useful with complex nested queries like the
5962

6063
This example also highlights a useful naming convention for builder parameters in deeply nested structures. For lambda expressions with a single argument, Kotlin provides the implicit `it` parameter and Scala allows use of `_`. This can be approximated in Java by using an underscore or a single letter prefix followed by a number representing the depth level (i.e. `_0`, `_1`, or `b0`, `b1` and so on). Not only does this remove the need to create throw-away variable names, but it also improves code readability. Correct indentation also allows the structure of the query to stand out.
6164

65+
% :::{include-code} src={{doc-tests-src}}/api_conventions/ApiConventionsTest.java tag=builder-intervals
6266
```java
63-
ElasticsearchClient client = ...
64-
SearchResponse<SomeApplicationData> results = client
67+
ElasticsearchClient esClient = createClient();
68+
SearchResponse<SomeApplicationData> results = esClient
6569
.search(b0 -> b0
6670
.query(b1 -> b1
6771
.intervals(b2 -> b2
@@ -92,12 +96,13 @@ SearchResponse<SomeApplicationData> results = client
9296
)
9397
)
9498
),
95-
SomeApplicationData.class <1>
99+
SomeApplicationData.class // <1>
96100
);
97101
```
98102

99103
1. Search results will be mapped to `SomeApplicationData` instances to be readily available to the application.
100104

101105

102-
The source code for the examples above can be found in the [Java API Client tests](https://github.com/elastic/elasticsearch-java/tree/master/java-client/src/test/java/co/elastic/clients/documentation).
106+
:::{include} /reference/_snippets/doc-tests-blurb.md
107+
:::
103108

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
mapped_pages:
3+
- https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/api-conventions.html
4+
---
5+
6+
# API conventions [api-conventions]
7+
8+
The Java API Client uses a very consistent code structure, using modern code patterns that make complex requests easier to write and complex responses easier to process. The sections below explain these in details.
9+
10+
* [Package structure and namespace clients](package-structure.md)
11+
* [Method naming conventions](method-naming.md)
12+
* [Blocking and asynchronous clients](blocking-async.md)
13+
* [Building API objects](building-objects.md)
14+
* [Lists and maps](lists-maps.md)
15+
* [Variant types](variant-types.md)
16+
* [Object life cycles and thread safety](object-lifecycles.md)
17+
* [Creating API objects from JSON data](loading-json.md)
18+
* [Exceptions](exception-conventions.md)
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+

docs/reference/lists-maps.md docs/reference/api-conventions/lists-maps.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Properties of type `List` and `Map` are exposed by object builders as a set of o
1212

1313
Object builders create immutable objects, and this also applies to list and map properties that are made immutable at object construction time.
1414

15+
% :::{include-code} src={{doc-tests-src}}/api_conventions/ApiConventionsTest.java tag=collections
1516
```java
1617
// Prepare a list of index names
1718
List<String> names = Arrays.asList("idx-a", "idx-b", "idx-c");
@@ -48,7 +49,6 @@ SearchRequest search = SearchRequest.of(r -> r
4849
);
4950
```
5051

51-
5252
## List and map values are never `null` [_list_and_map_values_are_never_null]
5353

5454
The {{es}} API has a lot of optional properties. For single-valued properties, the Java API Client represents missing optional values as `null`. Applications therefore have to null-check optional values before using them.
@@ -57,6 +57,7 @@ For lists and maps however, applications often only care about whether they’re
5757

5858
If you ever need to distinguish between a missing (undefined) optional collection and an effectively-empty collection returned by {{es}}, the `ApiTypeHelper` class provides a utility method to distinguish them:
5959

60+
% :::{include-code} src={{doc-tests-src}}/api_conventions/ApiConventionsTest.java tag=optional-collections
6061
```java
6162
NodeStatistics stats = NodeStatistics.of(b -> b
6263
.total(1)
@@ -73,5 +74,6 @@ assertEquals(0, stats.failures().size());
7374
assertFalse(ApiTypeHelper.isDefined(stats.failures()));
7475
```
7576

76-
The source code for the examples above can be found in the [Java API Client tests](https://github.com/elastic/elasticsearch-java/tree/master/java-client/src/test/java/co/elastic/clients/documentation).
77+
:::{include} /reference/_snippets/doc-tests-blurb.md
78+
:::
7779

docs/reference/loading-json.md docs/reference/api-conventions/loading-json.md

+21-16
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,17 @@ Consider a resource file `some-index.json` containing an index definition:
3131

3232
You can create an index from that definition as follows:
3333

34+
% :::{include-code} src={{doc-tests-src}}/api_conventions/LoadingJsonTest.java tag=load-index
3435
```java
3536
InputStream input = this.getClass()
36-
.getResourceAsStream("some-index.json"); <1>
37+
.getResourceAsStream("some-index.json"); // <1>
3738

3839
CreateIndexRequest req = CreateIndexRequest.of(b -> b
3940
.index("some-index")
40-
.withJson(input) <2>
41+
.withJson(input) // <2>
4142
);
4243

43-
boolean created = client.indices().create(req).acknowledged();
44+
boolean created = esClient.indices().create(req).acknowledged();
4445
```
4546

4647
1. open an input stream for the JSON resource file.
@@ -52,17 +53,18 @@ boolean created = client.indices().create(req).acknowledged();
5253

5354
Similarly, you can read documents to be stored in {{es}} from data files:
5455

56+
% :::{include-code} src={{doc-tests-src}}/api_conventions/LoadingJsonTest.java tag=ingest-data
5557
```java
5658
FileReader file = new FileReader(new File(dataDir, "document-1.json"));
5759

58-
IndexRequest<JsonData> req; <1>
60+
IndexRequest<JsonData> req; // <1>
5961

6062
req = IndexRequest.of(b -> b
6163
.index("some-index")
6264
.withJson(file)
6365
);
6466

65-
client.index(req);
67+
esClient.index(req);
6668
```
6769

6870
1. when calling `withJson()` on data structures that have generic type parameters, these generic types will be considered to be `JsonData`.
@@ -73,6 +75,7 @@ client.index(req);
7375

7476
You can combine `withJson()` with regular calls to setter methods. The example below loads the query part of a search request from a `String` and programmatically adds an aggregation.
7577

78+
% :::{include-code} src={{doc-tests-src}}/api_conventions/LoadingJsonTest.java tag=query
7679
```java
7780
Reader queryJson = new StringReader(
7881
"{" +
@@ -86,8 +89,8 @@ Reader queryJson = new StringReader(
8689
"}");
8790

8891
SearchRequest aggRequest = SearchRequest.of(b -> b
89-
.withJson(queryJson) <1>
90-
.aggregations("max-cpu", a1 -> a1 <2>
92+
.withJson(queryJson) // <1>
93+
.aggregations("max-cpu", a1 -> a1 // <2>
9194
.dateHistogram(h -> h
9295
.field("@timestamp")
9396
.calendarInterval(CalendarInterval.Hour)
@@ -99,8 +102,8 @@ SearchRequest aggRequest = SearchRequest.of(b -> b
99102
.size(0)
100103
);
101104

102-
Map<String, Aggregate> aggs = client
103-
.search(aggRequest, Void.class) <3>
105+
Map<String, Aggregate> aggs = esClient
106+
.search(aggRequest, Void.class) // <3>
104107
.aggregations();
105108
```
106109

@@ -114,6 +117,7 @@ Map<String, Aggregate> aggs = client
114117

115118
The `withJson()` methods are partial deserializers: the properties loaded from the JSON will set property values or replace the previous ones, but will not reset other properties not found in the JSON input. You can use this to combine multiple JSON snippets to build complex search requests. In the example below, we combine separate definitions of a query that selects some documents and an aggregation that is run on the results of this query.
116119

120+
% :::{include-code} src={{doc-tests-src}}/api_conventions/LoadingJsonTest.java tag=query-and-agg
117121
```java
118122
Reader queryJson = new StringReader(
119123
"{" +
@@ -124,12 +128,12 @@ Reader queryJson = new StringReader(
124128
" }" +
125129
" }" +
126130
" }," +
127-
" \"size\": 100" + <1>
131+
" \"size\": 100" + // <1>
128132
"}");
129133

130134
Reader aggregationJson = new StringReader(
131135
"{" +
132-
" \"size\": 0, " + <2>
136+
" \"size\": 0, " + // <2>
133137
" \"aggregations\": {" +
134138
" \"hours\": {" +
135139
" \"date_histogram\": {" +
@@ -148,12 +152,12 @@ Reader aggregationJson = new StringReader(
148152
"}");
149153

150154
SearchRequest aggRequest = SearchRequest.of(b -> b
151-
.withJson(queryJson) <3>
152-
.withJson(aggregationJson) <4>
153-
.ignoreUnavailable(true) <5>
155+
.withJson(queryJson) // <3>
156+
.withJson(aggregationJson) // <4>
157+
.ignoreUnavailable(true) // <5>
154158
);
155159

156-
Map<String, Aggregate> aggs = client
160+
Map<String, Aggregate> aggs = esClient
157161
.search(aggRequest, Void.class)
158162
.aggregations();
159163
```
@@ -167,5 +171,6 @@ Map<String, Aggregate> aggs = client
167171

168172
Notice that order matters when the JSON snippets have some common properties: just as when setting property values programmatically, the last value that is set for a property overwrites the previous one.
169173

170-
The source code for the examples above can be found in the [Java API Client tests](https://github.com/elastic/elasticsearch-java/tree/master/java-client/src/test/java/co/elastic/clients/documentation).
174+
:::{include} /reference/_snippets/doc-tests-blurb.md
175+
:::
171176

docs/reference/package-structure.md docs/reference/api-conventions/package-structure.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The Java API Client follows this structure: feature groups are called “namespa
1111

1212
Each of the namespace clients can be accessed from the top level {{es}} client. The only exceptions are the “search” and “document” APIs which are located in the `core` subpackage and can be accessed on the main {{es}} client object.
1313

14-
The snippet below shows how to use the indices namespace client to create an index (the lambda syntax is explained in [Building API objects](/reference/building-objects.md)):
14+
The snippet below shows how to use the indices namespace client to create an index (the lambda syntax is explained in [Building API objects](building-objects.md)):
1515

1616
```java
1717
// Create the "products" index

0 commit comments

Comments
 (0)