Skip to content

Commit 3abc68f

Browse files
committed
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.
1 parent f70ff1b commit 3abc68f

File tree

1 file changed

+51
-150
lines changed

1 file changed

+51
-150
lines changed

docs/src/main/asciidoc/elasticsearch.adoc

Lines changed: 51 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,24 @@ https://github.com/quarkusio/quarkus/tree/main/docs/src/main/asciidoc
66
= Connecting to an Elasticsearch cluster
77
include::_attributes.adoc[]
88
:categories: data
9-
: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.
1010

1111
Elasticsearch is a well known full text search engine and NoSQL datastore.
1212

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.
1414

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+
====
1627

1728
== Prerequisites
1829

@@ -35,20 +46,22 @@ First, we need a new project. Create a new project with the following command:
3546
:create-app-extensions: resteasy-reactive-jackson,elasticsearch-rest-client
3647
include::{includes}/devtools/create-app.adoc[]
3748

38-
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.
4053

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.
4255

4356
[NOTE]
4457
====
4558
We use the `resteasy-reactive-jackson` extension here and not the JSON-B variant because we will use the Vert.x `JsonObject` helper
4659
to serialize/deserialize our objects to/from Elasticsearch and it uses Jackson under the hood.
4760
====
4861

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.
5063

51-
For the Elasticsearch low level client, add:
64+
For the Elasticsearch low level REST client, add the following dependency to your build file:
5265

5366
[source,xml,role="primary asciidoc-tabs-target-sync-cli asciidoc-tabs-target-sync-maven"]
5467
.pom.xml
@@ -65,7 +78,7 @@ For the Elasticsearch low level client, add:
6578
implementation("io.quarkus:quarkus-elasticsearch-rest-client")
6679
----
6780

68-
For the Elasticsearch Java API client, add:
81+
For the Elasticsearch Java client, add the following dependency to your build file:
6982

7083
[source,xml,role="primary asciidoc-tabs-target-sync-cli asciidoc-tabs-target-sync-maven"]
7184
.pom.xml
@@ -101,8 +114,10 @@ public class Fruit {
101114

102115
Nothing fancy. One important thing to note is that having a default constructor is required by the JSON serialization layer.
103116

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.
106121

107122
[source,java]
108123
----
@@ -178,14 +193,11 @@ public class FruitService {
178193
}
179194
}
180195
----
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`.
189201

190202
Now, create the `org.acme.elasticsearch.FruitResource` class as follows:
191203

@@ -244,15 +256,15 @@ The implementation is pretty straightforward and you just need to define your en
244256
== Configuring Elasticsearch
245257
The main property to configure is the URL to connect to the Elasticsearch cluster.
246258

247-
A sample configuration should look like this:
259+
For a typical clustered Elasticsearch service, a sample configuration would look like the following:
248260

249261
[source,properties]
250262
----
251263
# configure the Elasticsearch client for a cluster of two nodes
252264
quarkus.elasticsearch.hosts = elasticsearch1:9200,elasticsearch2:9200
253265
----
254266

255-
In this example, we are using a single instance running on localhost:
267+
In our case, we are using a single instance running on localhost:
256268

257269
[source,properties]
258270
----
@@ -263,9 +275,10 @@ quarkus.elasticsearch.hosts = localhost:9200
263275
If you need a more advanced configuration, you can find the comprehensive list of supported configuration properties at the end of this guide.
264276

265277
[[dev-services]]
266-
=== Dev Services (Configuration Free Databases)
278+
=== Dev Services
279+
267280
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.
269282
What that means practically is that, if you have not configured `quarkus.elasticsearch.hosts`, Quarkus will automatically
270283
start an Elasticsearch container when running tests or dev mode, and automatically configure the connection.
271284

@@ -275,8 +288,8 @@ we recommend that you use the `%prod.` profile to define your Elasticsearch sett
275288

276289
For more information you can read the xref:elasticsearch-dev-services.adoc[Dev Services for Elasticsearch guide].
277290

278-
279291
=== Programmatically Configuring Elasticsearch
292+
280293
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.
281294

282295
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
320333
}
321334
}
322335
----
336+
323337
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.
324338

325339
[NOTE]
@@ -343,7 +357,7 @@ docker run --name elasticsearch -e "discovery.type=single-node" -e "ES_JAVA_OPT
343357

344358
== Running the application
345359

346-
Now let's run our application via Quarkus dev mode:
360+
Let's start our application in dev mode:
347361

348362
include::{includes}/devtools/dev.adoc[]
349363

@@ -354,16 +368,16 @@ You can add new fruits to the list via the following curl command:
354368
curl localhost:8080/fruits -d '{"name": "bananas", "color": "yellow"}' -H "Content-Type: application/json"
355369
----
356370

357-
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:
358372

359373
[source,bash,subs=attributes+]
360374
----
361375
curl localhost:8080/fruits/search?color=yellow
362376
----
363377

364-
== Using the Java API Client
378+
== Using the Elasticsearch Java Client
365379

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:
367381

368382
[source,java]
369383
----
@@ -423,138 +437,25 @@ public class FruitService {
423437
}
424438
}
425439
----
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:
461-
462-
[source,java]
463-
----
464-
import java.io.IOException;
465-
import java.util.ArrayList;
466-
import java.util.List;
467-
468-
import jakarta.enterprise.context.ApplicationScoped;
469-
import jakarta.inject.Inject;
470-
471-
import org.elasticsearch.action.get.GetRequest;
472-
import org.elasticsearch.action.get.GetResponse;
473-
import org.elasticsearch.action.index.IndexRequest;
474-
import org.elasticsearch.action.search.SearchRequest;
475-
import org.elasticsearch.action.search.SearchResponse;
476-
import org.elasticsearch.client.RequestOptions;
477-
import org.elasticsearch.client.RestHighLevelClient;
478-
import org.elasticsearch.common.xcontent.XContentType;
479-
import org.elasticsearch.index.query.QueryBuilders;
480-
import org.elasticsearch.search.SearchHit;
481-
import org.elasticsearch.search.SearchHits;
482-
import org.elasticsearch.search.builder.SearchSourceBuilder;
483-
484-
import io.vertx.core.json.JsonObject;
485-
486-
@ApplicationScoped
487-
public class FruitService {
488-
@Inject
489-
RestHighLevelClient restHighLevelClient; // <1>
490-
491-
public void index(Fruit fruit) throws IOException {
492-
IndexRequest request = new IndexRequest("fruits"); // <2>
493-
request.id(fruit.id);
494-
request.source(JsonObject.mapFrom(fruit).toString(), XContentType.JSON); // <3>
495-
restHighLevelClient.index(request, RequestOptions.DEFAULT); // <4>
496-
}
497-
498-
public Fruit get(String id) throws IOException {
499-
GetRequest getRequest = new GetRequest("fruits", id);
500-
GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
501-
if (getResponse.isExists()) {
502-
String sourceAsString = getResponse.getSourceAsString();
503-
JsonObject json = new JsonObject(sourceAsString); // <5>
504-
return json.mapTo(Fruit.class);
505-
}
506-
return null;
507-
}
508-
509-
public List<Fruit> searchByColor(String color) throws IOException {
510-
return search("color", color);
511-
}
512-
513-
public List<Fruit> searchByName(String name) throws IOException {
514-
return search("name", name);
515-
}
516-
517-
private List<Fruit> search(String term, String match) throws IOException {
518-
SearchRequest searchRequest = new SearchRequest("fruits");
519-
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
520-
searchSourceBuilder.query(QueryBuilders.matchQuery(term, match));
521-
searchRequest.source(searchSourceBuilder);
522-
523-
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
524-
SearchHits hits = searchResponse.getHits();
525-
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.
543444

544445
== Hibernate Search Elasticsearch
545446

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.
547448

548449
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.
549450

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].
551452

552453
== Cluster Health Check
553454

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
555456
to validate the health of the cluster.
556457

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.
558459
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.
559460

560461
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
573474

574475
== Conclusion
575476

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.
577478

578479
== Configuration Reference
579480

0 commit comments

Comments
 (0)