You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Clearly mark the high level REST client as deprecated
And removed the doc about it as we don't want people to use it anymore.
People interested in it can still have a look at the older documentation
if needed.
Also did some minor editorialization and formatting improvements.
:summary: This guide covers how to use an Elasticsearch cluster using the low level REST client or the Java API client.
9
+
:summary: This guide covers how to interact with an Elasticsearch cluster using the low level REST client or the Elasticsearch Java client.
10
10
11
11
Elasticsearch is a well known full text search engine and NoSQL datastore.
12
12
13
-
In this guide, we will see how you can get your REST services to use an Elasticsearch cluster.
13
+
In this guide, we will see how you can get your REST services to interact with an Elasticsearch cluster.
14
14
15
-
Quarkus provides three ways of accessing Elasticsearch: via the lower level `RestClient`, via the high level `RestHighLevelClient`, or via the Java API client.
15
+
Quarkus provides two ways of accessing Elasticsearch:
16
+
17
+
* The lower level REST Client
18
+
* The Elasticsearch Java client
19
+
20
+
[WARNING]
21
+
====
22
+
A third Quarkus extension for the "high level REST Client" exists, but is deprecated and will be removed in a future version,
23
+
as this client has been deprecated by Elastic and has some licensing issues.
24
+
25
+
It is highly recommended to upgrade to the new Elasticsearch Java client extension.
26
+
====
16
27
17
28
== Prerequisites
18
29
@@ -35,20 +46,22 @@ First, we need a new project. Create a new project with the following command:
This command generates a Maven structure importing the RESTEasy Reactive/JAX-RS, Jackson, and the Elasticsearch low level client extensions.
39
-
After this, the `quarkus-elasticsearch-rest-client` extension has been added to your build file.
49
+
This command generates a Maven structure importing the RESTEasy Reactive, Jackson, and Elasticsearch low level REST client extensions.
50
+
51
+
The Elasticsearch low level REST client comes with the `quarkus-elasticsearch-rest-client` extension
52
+
that has been added to your build file.
40
53
41
-
If you want to use the Java API client instead, replace the `elasticsearch-rest-client` extension by the `elasticsearch-java-client` extension.
54
+
If you want to use the Elasticsearch Java client instead, replace the `quarkus-elasticsearch-rest-client` extension by the `quarkus-elasticsearch-java-client` extension.
42
55
43
56
[NOTE]
44
57
====
45
58
We use the `resteasy-reactive-jackson` extension here and not the JSON-B variant because we will use the Vert.x `JsonObject` helper
46
59
to serialize/deserialize our objects to/from Elasticsearch and it uses Jackson under the hood.
47
60
====
48
61
49
-
If you don’t want to generate a new project, add the following dependencies to your build file.
62
+
To add the extensions to an existing project, follow the instructions below.
50
63
51
-
For the Elasticsearch low level client, add:
64
+
For the Elasticsearch low level REST client, add the following dependency to your build file:
Nothing fancy. One important thing to note is that having a default constructor is required by the JSON serialization layer.
103
116
104
-
Now create a `org.acme.elasticsearch.FruitService` that will be the business layer of our application and store/load the fruits from the Elasticsearch instance.
105
-
Here we use the low level client, if you want to use the Java API client instead follow the instructions in the <<using-the-java-api-client,Using the Java API Client>> paragraph instead.
117
+
Now create a `org.acme.elasticsearch.FruitService` that will be the business layer of our application
118
+
and will store/load the fruits from the Elasticsearch instance.
119
+
Here we use the low level REST client, if you want to use the Java API client instead,
120
+
follow the instructions in the <<using-the-elasticsearch-java-client,Using the Elasticsearch Java Client>> paragraph instead.
106
121
107
122
[source,java]
108
123
----
@@ -178,14 +193,11 @@ public class FruitService {
178
193
}
179
194
}
180
195
----
181
-
182
-
In this example you can note the following:
183
-
184
-
1. We inject an Elasticsearch low level `RestClient` into our service.
185
-
2. We create an Elasticsearch request.
186
-
3. We use Vert.x `JsonObject` to serialize the object before sending it to Elasticsearch, you can use whatever you want to serialize to JSON.
187
-
4. We send the request (indexing request here) to Elasticsearch.
188
-
5. In order to deserialize the object from Elasticsearch, we again use Vert.x `JsonObject`.
196
+
<1> We inject an Elasticsearch low level `RestClient` into our service.
197
+
<2> We create an Elasticsearch request.
198
+
<3> We use Vert.x `JsonObject` to serialize the object before sending it to Elasticsearch, you can use whatever you want to serialize your objects to JSON.
199
+
<4> We send the request (indexing request here) to Elasticsearch.
200
+
<5> In order to deserialize the object from Elasticsearch, we again use Vert.x `JsonObject`.
189
201
190
202
Now, create the `org.acme.elasticsearch.FruitResource` class as follows:
191
203
@@ -244,15 +256,15 @@ The implementation is pretty straightforward and you just need to define your en
244
256
== Configuring Elasticsearch
245
257
The main property to configure is the URL to connect to the Elasticsearch cluster.
246
258
247
-
A sample configuration should look like this:
259
+
For a typical clustered Elasticsearch service, a sample configuration would look like the following:
248
260
249
261
[source,properties]
250
262
----
251
263
# configure the Elasticsearch client for a cluster of two nodes
If you need a more advanced configuration, you can find the comprehensive list of supported configuration properties at the end of this guide.
264
276
265
277
[[dev-services]]
266
-
=== Dev Services (Configuration Free Databases)
278
+
=== Dev Services
279
+
267
280
Quarkus supports a feature called Dev Services that allows you to start various containers without any config.
268
-
In the case of Elasticsearch this support extends to the default Elasticsearch connection.
281
+
In the case of Elasticsearch, this support extends to the default Elasticsearch connection.
269
282
What that means practically is that, if you have not configured `quarkus.elasticsearch.hosts`, Quarkus will automatically
270
283
start an Elasticsearch container when running tests or dev mode, and automatically configure the connection.
271
284
@@ -275,8 +288,8 @@ we recommend that you use the `%prod.` profile to define your Elasticsearch sett
275
288
276
289
For more information you can read the xref:elasticsearch-dev-services.adoc[Dev Services for Elasticsearch guide].
277
290
278
-
279
291
=== Programmatically Configuring Elasticsearch
292
+
280
293
On top of the parametric configuration, you can also programmatically apply additional configuration to the client by implementing a `RestClientBuilder.HttpClientConfigCallback` and annotating it with `ElasticsearchClientConfig`. You may provide multiple implementations and configuration provided by each implementation will be applied in a randomly ordered cascading manner.
281
294
282
295
For example, when accessing an Elasticsearch cluster that is set up for TLS on the HTTP layer, the client needs to trust the certificate that Elasticsearch is using. The following is an example of setting up the client to trust the CA that has signed the certificate that Elasticsearch is using, when that CA certificate is available in a PKCS#12 keystore.
@@ -320,6 +333,7 @@ public class SSLContextConfigurator implements RestClientBuilder.HttpClientConfi
320
333
}
321
334
}
322
335
----
336
+
323
337
See https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/_encrypted_communication.html[Elasticsearch documentation] for more details on this particular example.
And search for fruits by name or color via the flowing curl command:
371
+
And search for fruits by name or color via the following curl command:
358
372
359
373
[source,bash,subs=attributes+]
360
374
----
361
375
curl localhost:8080/fruits/search?color=yellow
362
376
----
363
377
364
-
== Using the Java API Client
378
+
== Using the Elasticsearch Java Client
365
379
366
-
Here is a version of the `FruitService` using the Java API client instead of the low level one:
380
+
Here is a version of the `FruitService` using the Elasticsearch Java Client instead of the low level one:
367
381
368
382
[source,java]
369
383
----
@@ -423,138 +437,25 @@ public class FruitService {
423
437
}
424
438
}
425
439
----
426
-
427
-
In this example you can note the following:
428
-
429
-
1. We inject an `ElasticsearchClient` inside the service.
430
-
2. We create an Elasticsearch index request using a builder.
431
-
3. We directly pass the object to the request as the Java API client has a serialization layer.
432
-
4. We send the request to Elasticsearch.
433
-
434
-
== Using the High Level REST Client
435
-
436
-
Quarkus provides support for the Elasticsearch High Level REST Client but keep in mind that it comes with some caveats:
437
-
438
-
- It drags a lot of dependencies - especially Lucene -, which doesn't fit well with Quarkus philosophy. The Elasticsearch team is aware of this issue, and it might improve sometime in the future.
439
-
- It is tied to a certain version of the Elasticsearch server: you cannot use a High Level REST Client version 7 to access a server version 6.
440
-
441
-
[WARNING]
442
-
====
443
-
Due to the license change made by Elastic for the Elasticsearch High Level REST Client,
444
-
we are keeping in Quarkus the last Open Source version of this particular client, namely 7.10,
445
-
and it won't be upgraded to newer versions.
446
-
447
-
Given this client was deprecated by Elastic and replaced by a new Open Source Java client,
448
-
the Elasticsearch High Level REST Client extension is considered deprecated and will be removed from the Quarkus codebase at some point in the future.
449
-
450
-
Note that contrary to the High Level REST client, we are using the latest version of the Low Level REST client (which is still Open Source),
451
-
and, while we believe it should work, the situation is less than ideal and might cause some issues.
452
-
Feel free to override the versions of the clients in your applications depending on your requirements,
453
-
but be aware of https://www.elastic.co/blog/elastic-license-v2[the new licence of the High Level REST Client] for versions 7.11+:
454
-
it is not Open Source and has several usage restrictions.
455
-
456
-
We provide an extension for the new Open Source Java API client that is a replacement for the High Level REST CLient,
457
-
but it will require changes in your applications as it is an entirely new client.
458
-
====
459
-
460
-
Here is a version of the `FruitService` using the high level client instead of the low level one:
List<Fruit> results = new ArrayList<>(hits.getHits().length);
526
-
for (SearchHit hit : hits.getHits()) {
527
-
String sourceAsString = hit.getSourceAsString();
528
-
JsonObject json = new JsonObject(sourceAsString);
529
-
results.add(json.mapTo(Fruit.class));
530
-
}
531
-
return results;
532
-
}
533
-
}
534
-
----
535
-
536
-
In this example you can note the following:
537
-
538
-
1. We inject an Elasticsearch `RestHighLevelClient` inside the service.
539
-
2. We create an Elasticsearch index request.
540
-
3. We use Vert.x `JsonObject` to serialize the object before sending it to Elasticsearch, you can use whatever you want to serialize to JSON.
541
-
4. We send the request to Elasticsearch.
542
-
5. In order to deserialize the object from Elasticsearch, we again use Vert.x `JsonObject`.
440
+
<1> We inject an `ElasticsearchClient` inside the service.
441
+
<2> We create an Elasticsearch index request using a builder.
442
+
<3> We directly pass the object to the request as the Java API client has a serialization layer.
443
+
<4> We send the request to Elasticsearch.
543
444
544
445
== Hibernate Search Elasticsearch
545
446
546
-
Quarkus supports Hibernate Search with Elasticsearch via the `hibernate-search-orm-elasticsearch` extension.
447
+
Quarkus supports Hibernate Search with Elasticsearch via the `quarkus-hibernate-search-orm-elasticsearch` extension.
547
448
548
449
Hibernate Search Elasticsearch allows to synchronize your JPA entities to an Elasticsearch cluster and offers a way to query your Elasticsearch cluster using the Hibernate Search API.
549
450
550
-
If you're interested in it, you can read the xref:hibernate-search-orm-elasticsearch.adoc[Hibernate Search with Elasticsearch guide].
451
+
If you are interested in it, please consult the xref:hibernate-search-orm-elasticsearch.adoc[Hibernate Search with Elasticsearch guide].
551
452
552
453
== Cluster Health Check
553
454
554
-
If you are using the `quarkus-smallrye-health` extension, both the extension will automatically add a readiness health check
455
+
If you are using the `quarkus-smallrye-health` extension, both extensions will automatically add a readiness health check
555
456
to validate the health of the cluster.
556
457
557
-
So when you access the `/q/health/ready` endpoint of your application you will have information about the cluster status.
458
+
So when you access the `/q/health/ready` endpoint of your application, you will have information about the cluster status.
558
459
It uses the cluster health endpoint, the check will be down if the status of the cluster is **red**, or the cluster is not available.
559
460
560
461
This behavior can be disabled by setting the `quarkus.elasticsearch.health.enabled` property to `false` in your `application.properties`.
@@ -573,7 +474,7 @@ You can then point your browser to `http://localhost:8080/fruits.html` and use y
573
474
574
475
== Conclusion
575
476
576
-
Accessing an Elasticsearch cluster from a low level or a high level client is easy with Quarkus as it provides easy configuration, CDI integration and native support for it.
477
+
Accessing an Elasticsearch cluster from the low level REST client or the Elasticsearch Java client is easy with Quarkus as it provides easy configuration, CDI integration and native support for it.
0 commit comments