Skip to content

Commit d7e42fc

Browse files
authored
Update documentation.
Original Pull Request #2361 Closes #2360
1 parent f8ddf16 commit d7e42fc

File tree

6 files changed

+105
-150
lines changed

6 files changed

+105
-150
lines changed

src/main/asciidoc/preface.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ built and tested.
3737
[cols="^,^,^,^,^",options="header"]
3838
|===
3939
| Spring Data Release Train | Spring Data Elasticsearch | Elasticsearch | Spring Framework | Spring Boot
40-
| 2022.0 (Turing) | 5.0.x | 8.5.0 | 6.0.x | 3.0.x?
40+
| 2022.0 (Turing) | 5.0.x | 8.5.0 | 6.0.x | 3.0.x
4141
| 2021.2 (Raj) | 4.4.x | 7.17.3 | 5.3.x | 2.7.x
4242
| 2021.1 (Q) | 4.3.x | 7.15.2 | 5.3.x | 2.6.x
4343
| 2021.0 (Pascal) | 4.2.xfootnote:oom[Out of maintenance] | 7.12.0 | 5.3.x | 2.5.x

src/main/asciidoc/reference/elasticsearch-clients.adoc

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
This chapter illustrates configuration and usage of supported Elasticsearch client implementations.
55

6-
Spring Data Elasticsearch operates upon an Elasticsearch client that is connected to a single Elasticsearch node or a cluster.
7-
Although the Elasticsearch Client can be used to work with the cluster, applications using Spring Data Elasticsearch normally use the higher level abstractions of <<elasticsearch.operations>> and <<elasticsearch.repositories>>.
6+
Spring Data Elasticsearch operates upon an Elasticsearch client (provided by Elasticsearch client libraries) that is
7+
connected to a single Elasticsearch node or a cluster.
8+
Although the Elasticsearch Client can be used directly to work with the cluster, applications using Spring Data
9+
Elasticsearch normally use the higher level abstractions of <<elasticsearch.operations>> and <<elasticsearch.repositories>>.
810

911
[[elasticsearch.clients.restclient]]
1012
== Imperative Rest Client
@@ -21,14 +23,20 @@ public class MyClientConfig extends ElasticsearchConfiguration {
2123
2224
@Override
2325
public ClientConfiguration clientConfiguration() {
24-
return ClientConfiguration.builder() //
25-
.connectedTo("localhost:9200") //
26+
return ClientConfiguration.builder() <.>
27+
.connectedTo("localhost:9200")
2628
.build();
2729
}
2830
}
31+
----
32+
<.> for a detailed description of the builder methods see <<elasticsearch.clients.configuration>>
33+
====
2934

30-
// ...
35+
The following beans can then be injected in other Spring components:
3136

37+
====
38+
[source,java]
39+
----
3240
@Autowired
3341
ElasticsearchOperations operations; <.>
3442
@@ -39,11 +47,8 @@ ElasticsearchClient elasticsearchClient; <.>
3947
RestClient restClient; <.>
4048
----
4149
42-
the following can be injected:
43-
4450
<.> an implementation of `ElasticsearchOperations`
4551
<.> the `co.elastic.clients.elasticsearch.ElasticsearchClient` that is used.
46-
This is new Elasticsearch client implementation.
4752
<.> the low level `RestClient` from the Elasticsearch libraries
4853
====
4954

@@ -61,18 +66,24 @@ When working with the reactive stack, the configuration must be derived from a d
6166
import org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchConfiguration;
6267
6368
@Configuration
64-
public class MyClientConfig extends ElasticsearchConfiguration {
69+
public class MyClientConfig extends ReactiveElasticsearchConfiguration {
6570
6671
@Override
6772
public ClientConfiguration clientConfiguration() {
68-
return ClientConfiguration.builder() //
69-
.connectedTo("localhost:9200") //
73+
return ClientConfiguration.builder() <.>
74+
.connectedTo("localhost:9200")
7075
.build();
7176
}
7277
}
78+
----
79+
<.> for a detailed description of the builder methods see <<elasticsearch.clients.configuration>>
80+
====
7381

74-
// ...
82+
The following beans can then be injected in other Spring components:
7583

84+
====
85+
[source,java]
86+
----
7687
@Autowired
7788
ReactiveElasticsearchOperations operations; <.>
7889
@@ -85,9 +96,9 @@ RestClient restClient; <.>
8596
8697
the following can be injected:
8798
88-
<.> an implementation of `ElasticsearchOperations`
99+
<.> an implementation of `ReactiveElasticsearchOperations`
89100
<.> the `org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchClient` that is used.
90-
This is based on the new Elasticsearch client implementation.
101+
This is a reactive implementation based on the Elasticsearch client implementation.
91102
<.> the low level `RestClient` from the Elasticsearch libraries
92103
====
93104

@@ -97,8 +108,14 @@ When using repositories, this instance is used under the hood as well.
97108
[[elasticsearch.clients.resthighlevelclient]]
98109
== High Level REST Client (deprecated)
99110

100-
The Java RestHighLevelClient is deprecated, but still can be configured like shown (read
101-
<<elasticsearch-migration-guide-4.4-5.0.old-client>> as well):
111+
[CAUTION]
112+
====
113+
The Elasticsearch Java RestHighLevelClient is deprecated, but still can be configured like shown (make sure to read
114+
<<elasticsearch-migration-guide-4.4-5.0.old-client>> as well).
115+
116+
It should only be used to access an Elasticsearch cluster running version 7, even with the compatibility headers set
117+
there are cases where the `RestHighLevelClient` cannot read the responses sent from a version 8 cluster.
118+
====
102119

103120
.RestHighLevelClient
104121
====
@@ -127,15 +144,6 @@ public class RestClientConfig extends AbstractElasticsearchConfiguration {
127144
RestHighLevelClient highLevelClient;
128145
129146
RestClient lowLevelClient = highLevelClient.lowLevelClient(); <3>
130-
131-
// ...
132-
133-
IndexRequest request = new IndexRequest("spring-data")
134-
.id(randomID())
135-
.source(singletonMap("feature", "high-level-rest-client"))
136-
.setRefreshPolicy(IMMEDIATE);
137-
138-
IndexResponse response = highLevelClient.index(request,RequestOptions.DEFAULT);
139147
----
140148
141149
<1> Use the builder to provide cluster addresses, set default `HttpHeaders` or enable SSL.
@@ -150,8 +158,12 @@ The `org.springframework.data.elasticsearch.client.erhlc.ReactiveElasticsearchCl
150158
It uses the request/response objects provided by the Elasticsearch core project.
151159
Calls are directly operated on the reactive stack, **not** wrapping async (thread pool bound) responses into reactive types.
152160

153-
This was the first reactive implementation Spring Data Elasticsearch provided, but now is deprecated in favour of the `org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchClient`
161+
[CAUTION]
162+
====
163+
This was the first reactive implementation Spring Data Elasticsearch provided, but now is deprecated in favour
164+
of the `org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchClient`
154165
which uses the functionality offered by the new Elasticsearch client libraries.
166+
====
155167

156168
.Reactive REST Client (deprecated)
157169
====
@@ -194,6 +206,11 @@ Client behaviour can be changed via the `ClientConfiguration` that allows to set
194206
====
195207
[source,java]
196208
----
209+
import org.springframework.data.elasticsearch.client.ClientConfiguration;
210+
import org.springframework.data.elasticsearch.support.HttpHeaders;
211+
212+
import static org.springframework.data.elasticsearch.client.elc.ElasticsearchClients.*;
213+
197214
HttpHeaders httpHeaders = new HttpHeaders();
198215
httpHeaders.add("some-header", "on every request") <.>
199216
@@ -212,7 +229,7 @@ ClientConfiguration clientConfiguration = ClientConfiguration.builder()
212229
return headers;
213230
})
214231
.withClientConfigurer( <.>
215-
ElasticsearchClients.ElasticsearchClientConfigurationCallback.from(clientBuilder -> {
232+
ElasticsearchClientConfigurationCallback.from(clientBuilder -> {
216233
// ...
217234
return clientBuilder;
218235
}))
@@ -227,12 +244,10 @@ ClientConfiguration clientConfiguration = ClientConfiguration.builder()
227244
<.> Optionally set a proxy.
228245
<.> Optionally set a path prefix, mostly used when different clusters a behind some reverse proxy.
229246
<.> Set the connection timeout.
230-
Default is 10 sec.
231247
<.> Set the socket timeout.
232-
Default is 5 sec.
233248
<.> Optionally set headers.
234249
<.> Add basic authentication.
235-
<.> A `Supplier<Header>` function can be specified which is called every time before a request is sent to Elasticsearch - here, as an example, the current time is written in a header.
250+
<.> A `Supplier<HttpHeaders>` function can be specified which is called every time before a request is sent to Elasticsearch - here, as an example, the current time is written in a header.
236251
<.> a function to configure the created client (see <<elasticsearch.clients.configuration.callbacks>>), can be added
237252
multiple times.
238253
====
@@ -251,6 +266,8 @@ The following callbacks are provided:
251266
[[elasticsearch.clients.configuration.callbacks.rest]]
252267
==== Configuration of the low level Elasticsearch `RestClient`:
253268

269+
This callback provides a `org.elasticsearch.client.RestClientBuilder` that can be used to configure the Elasticsearch
270+
`RestClient`:
254271
====
255272
[source,java]
256273
----
@@ -266,6 +283,9 @@ ClientConfiguration.builder()
266283
[[elasticsearch.clients.configurationcallbacks.httpasync]]
267284
==== Configuration of the HttpAsyncClient used by the low level Elasticsearch `RestClient`:
268285

286+
This callback provides a `org.apache.http.impl.nio.client.HttpAsyncClientBuilder` to configure the HttpCLient that is
287+
used by the `RestClient`.
288+
269289
====
270290
[source,java]
271291
----
@@ -287,6 +307,9 @@ documentation].
287307

288308
For the imperative client this must be done by setting the default headers, for the reactive code this must be done using a header supplier:
289309

310+
CAUTION: Even when these headers are set, there are cases where the response returned from the cluster cannot be
311+
parsed with the client. This is not an error in Spring Data Elasticsearch.
312+
290313
====
291314
[source,java]
292315
----
@@ -310,12 +333,13 @@ ClientConfiguration clientConfiguration = ClientConfiguration.builder()
310333
[[elasticsearch.clients.logging]]
311334
== Client Logging
312335

313-
To see what is actually sent to and received from the server `Request` / `Response` logging on the transport level needs to be turned on as outlined in the snippet below.
336+
To see what is actually sent to and received from the server `Request` / `Response` logging on the transport level
337+
needs to be turned on as outlined in the snippet below. This can be enabled in the Elasticsearch client by setting
338+
the level of the `tracer` package to "trace" (see
339+
https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/java-rest-low-usage-logging.html)
314340

315341
.Enable transport layer logging
316342
[source,xml]
317343
----
318-
<logger name="org.springframework.data.elasticsearch.client.WIRE" level="trace"/>
344+
<logger name="tracer" level="trace"/>
319345
----
320-
321-
NOTE: The above applies to both the `RestHighLevelClient` and `ReactiveElasticsearchClient` when obtained via `RestClients` respectively `ReactiveRestClients`.

src/main/asciidoc/reference/elasticsearch-new.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
* Upgrade to Java 17 baseline
88
* Upgrade to Spring Framework 6
9+
* Upograde to Elasticsearch 8.5.0
910
* Use the new Elasticsearch client library
1011

1112
[[new-features.4-4-0]]

src/main/asciidoc/reference/elasticsearch-object-mapping.adoc

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
11
[[elasticsearch.mapping]]
22
= Elasticsearch Object Mapping
33

4-
Spring Data Elasticsearch Object Mapping is the process that maps a Java object - the domain entity - into the JSON representation that is stored in Elasticsearch and back.
5-
6-
Earlier versions of Spring Data Elasticsearch used a Jackson based conversion, Spring Data Elasticsearch 3.2.x introduced the <<elasticsearch.mapping.meta-model>>.
7-
As of version 4.0 only the Meta Object Mapping is used, the Jackson based mapper is not available anymore and the `MappingElasticsearchConverter` is used.
8-
9-
The main reasons for the removal of the Jackson based mapper are:
10-
11-
* Custom mappings of fields needed to be done with annotations like `@JsonFormat` or `@JsonInclude`.
12-
This often caused problems when the same object was used in different JSON based datastores or sent over a JSON based API.
13-
* Custom field types and formats also need to be stored into the Elasticsearch index mappings.
14-
The Jackson based annotations did not fully provide all the information that is necessary to represent the types of Elasticsearch.
15-
* Fields must be mapped not only when converting from and to entities, but also in query argument, returned data and on other places.
16-
17-
Using the `MappingElasticsearchConverter` now covers all these cases.
4+
Spring Data Elasticsearch Object Mapping is the process that maps a Java object - the domain entity - into the JSON
5+
representation that is stored in Elasticsearch and back. The class that is internally used for this mapping is the
6+
`MappingElasticsearcvhConverter`.
187

198
[[elasticsearch.mapping.meta-model]]
209
== Meta Model Object Mapping
@@ -31,14 +20,13 @@ The metadata is taken from the entity's properties which can be annotated.
3120
The following annotations are available:
3221

3322
* `@Document`: Applied at the class level to indicate this class is a candidate for mapping to the database.
34-
The most important attributes are:
23+
The most important attributes are (check the API documentation for the complete list of attributes):
3524
** `indexName`: the name of the index to store this entity in.
3625
This can contain a SpEL template expression like `"log-#{T(java.time.LocalDate).now().toString()}"`
3726
** `createIndex`: flag whether to create an index on repository bootstrapping.
3827
Default value is _true_.
3928
See <<elasticsearch.repositories.autocreation>>
40-
** `versionType`: Configuration of version management.
41-
Default value is _EXTERNAL_.
29+
4230

4331
* `@Id`: Applied at the field level to mark the field used for identity purpose.
4432
* `@Transient`: By default all fields are mapped to the document when it is stored or retrieved, this annotation excludes the field.
@@ -103,6 +91,9 @@ The following table shows the different attributes and the mapping created from
10391
NOTE: If you are using a custom date format, you need to use _uuuu_ for the year instead of _yyyy_.
10492
This is due to a https://www.elastic.co/guide/en/elasticsearch/reference/current/migrate-to-java-time.html#java-time-migration-incompatible-date-formats[change in Elasticsearch 7].
10593

94+
Check the code of the `org.springframework.data.elasticsearch.annotations.DateFormat` enum for a complete list of
95+
predefined values and their patterns.
96+
10697
[[elasticsearch.mapping.meta-model.annotations.range]]
10798
==== Range types
10899

@@ -194,7 +185,6 @@ Those type hints are represented as `_class` attributes within the document and
194185
[source,java]
195186
----
196187
public class Person { <1>
197-
198188
@Id String id;
199189
String firstname;
200190
String lastname;
@@ -274,7 +264,6 @@ Geospatial types like `Point` & `GeoPoint` are converted into _lat/lon_ pairs.
274264
[source,java]
275265
----
276266
public class Address {
277-
278267
String city, street;
279268
Point location;
280269
}

src/main/asciidoc/reference/elasticsearch-operations.adoc

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ There is support for automatic creation of indices and writing the mappings when
3434
== Usage examples
3535

3636
The example shows how to use an injected `ElasticsearchOperations` instance in a Spring REST controller.
37-
37+
The example assumes that `Person` is a class that is annotated with `@Document`, `@Id` etc (see <<elasticsearch.mapping.meta-model.annotations>>).
3838
.ElasticsearchOperations usage
3939
====
4040
[source,java]
@@ -45,34 +45,29 @@ public class TestController {
4545
4646
private ElasticsearchOperations elasticsearchOperations;
4747
48-
public TestController(ElasticsearchOperations elasticsearchOperations) { <1>
48+
public TestController(ElasticsearchOperations elasticsearchOperations) { <.>
4949
this.elasticsearchOperations = elasticsearchOperations;
5050
}
5151
5252
@PostMapping("/person")
53-
public String save(@RequestBody Person person) { <2>
54-
55-
IndexQuery indexQuery = new IndexQueryBuilder()
56-
.withId(person.getId().toString())
57-
.withObject(person)
58-
.build();
59-
String documentId = elasticsearchOperations.index(indexQuery);
60-
return documentId;
53+
public String save(@RequestBody Person person) { <.>
54+
Person savedEntity = elasticsearchOperations.save(person);
55+
return savedEntity.getId();
6156
}
6257
6358
@GetMapping("/person/{id}")
64-
public Person findById(@PathVariable("id") Long id) { <3>
65-
Person person = elasticsearchOperations
66-
.queryForObject(GetQuery.getById(id.toString()), Person.class);
59+
public Person findById(@PathVariable("id") Long id) { <.>
60+
Person person = elasticsearchOperations.get(id.toString(), Person.class);
6761
return person;
6862
}
6963
}
7064
7165
----
7266
73-
<1> Let Spring inject the provided `ElasticsearchOperations` bean in the constructor.
74-
<2> Store some entity in the Elasticsearch cluster.
75-
<3> Retrieve the entity with a query by id.
67+
<.> Let Spring inject the provided `ElasticsearchOperations` bean in the constructor.
68+
<.> Store some entity in the Elasticsearch cluster. The id is read from the returned entity, as it might have been
69+
null in the `person` object and been created by Elasticsearch.
70+
<.> Retrieve the entity with a get by id.
7671
====
7772

7873
To see the full possibilities of `ElasticsearchOperations` please refer to the API documentation.

0 commit comments

Comments
 (0)