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
This command generates a Maven structure importing the RESTEasy Reactive/JAX-RS, Jackson, and the Elasticsearch low level client extensions.
40
-
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.
41
53
42
-
If you want to use the high level client instead, replace the `elasticsearch-rest-client` extension by the `elasticsearch-rest-high-level-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.
43
55
44
56
[NOTE]
45
57
====
46
58
We use the `resteasy-reactive-jackson` extension here and not the JSON-B variant because we will use the Vert.x `JsonObject` helper
47
59
to serialize/deserialize our objects to/from Elasticsearch and it uses Jackson under the hood.
48
60
====
49
61
50
-
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.
51
63
52
-
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.
104
116
105
-
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.
106
-
Here we use the low level client, if you want to use the high level client instead follow the instructions in the <<using-the-high-level-rest-client,Using the High Level REST 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.
107
121
108
122
[source,java]
109
123
----
@@ -179,14 +193,11 @@ public class FruitService {
179
193
}
180
194
}
181
195
----
182
-
183
-
In this example you can note the following:
184
-
185
-
1. We inject an Elasticsearch low level `RestClient` into our service.
186
-
2. We create an Elasticsearch request.
187
-
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.
188
-
4. We send the request (indexing request here) to Elasticsearch.
189
-
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`.
190
201
191
202
Now, create the `org.acme.elasticsearch.FruitResource` class as follows:
192
203
@@ -245,15 +256,15 @@ The implementation is pretty straightforward and you just need to define your en
245
256
== Configuring Elasticsearch
246
257
The main property to configure is the URL to connect to the Elasticsearch cluster.
247
258
248
-
A sample configuration should look like this:
259
+
For a typical clustered Elasticsearch service, a sample configuration would look like the following:
249
260
250
261
[source,properties]
251
262
----
252
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.
265
276
266
277
[[dev-services]]
267
-
=== Dev Services (Configuration Free Databases)
278
+
=== Dev Services
279
+
268
280
Quarkus supports a feature called Dev Services that allows you to start various containers without any config.
269
-
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.
270
282
What that means practically is that, if you have not configured `quarkus.elasticsearch.hosts`, Quarkus will automatically
271
283
start an Elasticsearch container when running tests or dev mode, and automatically configure the connection.
272
284
@@ -276,8 +288,8 @@ we recommend that you use the `%prod.` profile to define your Elasticsearch sett
276
288
277
289
For more information you can read the xref:elasticsearch-dev-services.adoc[Dev Services for Elasticsearch guide].
278
290
279
-
280
291
=== Programmatically Configuring Elasticsearch
292
+
281
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.
282
294
283
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.
@@ -321,6 +333,7 @@ public class SSLContextConfigurator implements RestClientBuilder.HttpClientConfi
321
333
}
322
334
}
323
335
----
336
+
324
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:
362
372
363
373
[source,bash,subs=attributes+]
364
374
----
365
375
curl localhost:8080/fruits/search?color=yellow
366
376
----
367
377
368
-
== Using the High Level REST Client
369
-
370
-
Quarkus provides support for the Elasticsearch High Level REST Client but keep in mind that it comes with some caveats:
371
-
372
-
- 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.
373
-
- 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.
374
-
375
-
[WARNING]
376
-
====
377
-
Due to the license change made by Elastic for the Elasticsearch High Level REST Client,
378
-
we are keeping in Quarkus the last Open Source version of this particular client, namely 7.10,
379
-
and it won't be upgraded to newer versions.
378
+
== Using the Elasticsearch Java Client
380
379
381
-
Given this client was deprecated by Elastic and replaced by a new Open Source Java client,
382
-
the Elasticsearch High Level REST Client extension is considered deprecated and will be removed from the Quarkus codebase at some point in the future.
383
-
384
-
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),
385
-
and, while we believe it should work, the situation is less than ideal and might cause some issues.
386
-
Feel free to override the versions of the clients in your applications depending on your requirements,
387
-
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+:
388
-
it is not Open Source and has several usage restrictions.
389
-
390
-
We will eventually provide an extension for the new Open Source Java client, but it will require changes in your applications
391
-
as it is an entirely new client.
392
-
====
393
-
394
-
Here is a version of the `FruitService` using the high level 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:
1. We inject an Elasticsearch `RestHighLevelClient` inside the service.
473
-
2. We create an Elasticsearch index request.
474
-
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.
475
-
4. We send the request to Elasticsearch.
476
-
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.
477
444
478
445
== Hibernate Search Elasticsearch
479
446
480
-
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.
481
448
482
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.
483
450
484
-
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].
485
452
486
453
== Cluster Health Check
487
454
488
-
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
489
456
to validate the health of the cluster.
490
457
491
-
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.
492
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.
493
460
494
461
This behavior can be disabled by setting the `quarkus.elasticsearch.health.enabled` property to `false` in your `application.properties`.
@@ -507,7 +474,7 @@ You can then point your browser to `http://localhost:8080/fruits.html` and use y
507
474
508
475
== Conclusion
509
476
510
-
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