Skip to content

Commit 4a3dd9e

Browse files
committed
HSEARCH-5482 Mention the JDK client in the docs
1 parent da7373e commit 4a3dd9e

File tree

6 files changed

+104
-5
lines changed

6 files changed

+104
-5
lines changed

backend/elasticsearch-client/jdk-rest-client/src/main/java/org/hibernate/search/backend/elasticsearch/client/jdk/cfg/ClientJdkElasticsearchBackendClientSettings.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ public static final class Defaults {
6464
private Defaults() {
6565
}
6666

67-
public static final int READ_TIMEOUT = 30000;
6867
public static final int CONNECTION_TIMEOUT = 1000;
6968
}
7069
}

backend/elasticsearch-client/jdk-rest-client/src/main/java/org/hibernate/search/backend/elasticsearch/client/jdk/impl/ClientJdkHttpRequestInterceptor.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
import java.nio.charset.StandardCharsets;
1212
import java.util.HashMap;
1313
import java.util.List;
14+
import java.util.Locale;
1415
import java.util.Map;
16+
import java.util.Set;
1517

1618
import org.hibernate.search.backend.elasticsearch.client.common.spi.ElasticsearchRequestInterceptor;
1719
import org.hibernate.search.backend.elasticsearch.client.common.spi.ElasticsearchRequestInterceptorContext;
@@ -20,6 +22,11 @@
2022
record ClientJdkHttpRequestInterceptor(ElasticsearchRequestInterceptor elasticsearchRequestInterceptor)
2123
implements HttpRequestInterceptor {
2224

25+
// https://docs.oracle.com/en/java/javase/25/docs/api/java.net.http/module-summary.html
26+
//
27+
// Host header is one of the restricted ^ so we skip it here:
28+
private static final Set<String> HEADERS_TO_IGNORE = Set.of( "host" );
29+
2330
@Override
2431
public void process(HttpRequest.Builder request, HttpRequest.BodyPublisher bodyPublisher,
2532
HttpRequestInterceptorContext context)
@@ -103,6 +110,10 @@ public Map<String, String> queryParameters() {
103110
public void overrideHeaders(Map<String, List<String>> headers) {
104111
for ( Map.Entry<String, List<String>> header : headers.entrySet() ) {
105112
String name = header.getKey();
113+
// To prevent java.lang.IllegalArgumentException: restricted header name: "Header-name"
114+
if ( HEADERS_TO_IGNORE.contains( name.toLowerCase( Locale.ROOT ) ) ) {
115+
continue;
116+
}
106117
boolean first = true;
107118
for ( String value : header.getValue() ) {
108119
if ( first ) {

documentation/src/main/asciidoc/migration/index.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Currently available options are:
6161
* `org.hibernate.search:hibernate-search-backend-elasticsearch-client-rest4` backed by `org.elasticsearch.client:elasticsearch-rest-client`
6262
* `org.hibernate.search:hibernate-search-backend-elasticsearch-client-rest5` backed by `co.elastic.clients:elasticsearch-rest5-client`
6363
* `org.hibernate.search:hibernate-search-backend-elasticsearch-client-opensearch-rest` backed by `org.opensearch.client:opensearch-rest-client`
64+
* `org.hibernate.search:hibernate-search-backend-elasticsearch-client-jdk` backed by `java.net.http.HttpClient`
6465

6566
[[data-format]]
6667
== Data format and schema

documentation/src/main/asciidoc/public/components/elasticsearch-client-compatibility/_all.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@ Configuration properties from this section are applicable to the following clien
1717
|<<backend-elasticsearch-configuration-client-elasticsearch-client-opensearch,OpenSearch low level client>>
1818
^| ✔
1919
20+
|<<backend-elasticsearch-configuration-client-elasticsearch-client-jdk,JDK HttpClient based REST client>>
21+
^| ✔ {jdk-client-warn}
22+
2023
|===
2124
====
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Copyright Red Hat Inc. and Hibernate Authors
3+
[NOTE]
4+
====
5+
Configuration properties from this section are applicable to the following clients:
6+
7+
[cols="3,2",options="header"]
8+
|===
9+
|Client |Configuration property applicability
10+
11+
|<<backend-elasticsearch-configuration-client-elasticsearch-client-rest4,Elasticsearch low level REST client>>
12+
^| ✔
13+
14+
|<<backend-elasticsearch-configuration-client-elasticsearch-client-rest5,Elasticsearch low level REST 5 client>>
15+
^| ✔
16+
17+
|<<backend-elasticsearch-configuration-client-elasticsearch-client-opensearch,OpenSearch low level client>>
18+
^| ✔
19+
20+
|<<backend-elasticsearch-configuration-client-elasticsearch-client-jdk,JDK HttpClient based REST client>>
21+
^| ❌ {jdk-client-warn}
22+
23+
|===
24+
====

documentation/src/main/asciidoc/public/reference/_backend-elasticsearch.adoc

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ which may change in future versions of Hibernate Search.
170170
You should explicitly define the REST client in the application dependencies,
171171
if it matters which one the Elasticsearch backend will pick.
172172

173+
:jdk-client-warn:
173174
=== Available clients
174175

175176
[[backend-elasticsearch-configuration-client-elasticsearch-client-rest4]]
@@ -208,6 +209,23 @@ Underlying HTTP Client::: Apache HTTP Client 5
208209

209210
This Elasticsearch backend REST client is based on the OpenSearch low level client (`org.opensearch.client:opensearch-rest-client`).
210211

212+
[[backend-elasticsearch-configuration-client-elasticsearch-client-jdk]]
213+
==== Simple REST client based on the JDKs `HttpClient`
214+
215+
include::../components/_incubating-warning.adoc[]
216+
217+
Coordinates::
218+
GroupID::: `org.hibernate.search`
219+
ArtifactID::: `hibernate-search-backend-elasticsearch-client-jdk`
220+
Underlying HTTP Client::: JDKs Http Client (`java.net.http.HttpClient`)
221+
222+
This Elasticsearch backend REST client is based on the Http client that is part of the JDKs `java.net` API.
223+
Mostly intended for very basic use cases, and may lack some features,
224+
e.g. automatic node discovery, compared to the other clients.
225+
226+
NOTE: JDKs Http client allows configuration though the system properties.
227+
Consult the list of available properties for your JDK version, e.g. https://docs.oracle.com/en/java/javase/25/docs/api/java.net.http/module-summary.html[JDK 25]
228+
211229
[[backend-elasticsearch-configuration-hosts]]
212230
=== Target hosts
213231

@@ -297,7 +315,7 @@ Expects a boolean value. The default for this property is `false`.
297315
* `discovery.refresh_interval` defines the interval between two executions of the automatic discovery.
298316
Expects a positive integer, in seconds. The default for this property is `10`.
299317

300-
include::../components/elasticsearch-client-compatibility/_all.adoc[]
318+
include::../components/elasticsearch-client-compatibility/_apache_based.adoc[]
301319

302320
[[backend-elasticsearch-authentication-http]]
303321
=== HTTP authentication
@@ -395,6 +413,7 @@ include::../components/elasticsearch-client-compatibility/_all.adoc[]
395413
[[backend-elasticsearch-configuration-connection-tuning]]
396414
=== [[_connection_tuning]] Connection tuning
397415

416+
:jdk-client-warn: (Read timeout is not supported)
398417
Timeouts::
399418
+
400419
[source, properties]
@@ -418,6 +437,7 @@ These properties expect a positive <<configuration-property-types,Integer value>
418437
+
419438
include::../components/elasticsearch-client-compatibility/_all.adoc[]
420439

440+
:jdk-client-warn: (Configurable through JDKs Http client system properties)
421441
Connection pool::
422442
+
423443
[source, properties]
@@ -453,8 +473,9 @@ To prevent that from happening consider having the maximum number of connections
453473
====
454474

455475
+
456-
include::../components/elasticsearch-client-compatibility/_all.adoc[]
476+
include::../components/elasticsearch-client-compatibility/_apache_based.adoc[]
457477

478+
:jdk-client-warn: "(Configurable through JDKs Http client system properties)"
458479
Keep Alive::
459480
+
460481
[source, properties]
@@ -472,7 +493,7 @@ the duration from the `Keep-Alive` header or the value of this property (if set)
472493
If this property is not set, only the `Keep-Alive` header is considered,
473494
and if it's absent, idle connections will be kept forever.
474495
+
475-
include::../components/elasticsearch-client-compatibility/_all.adoc[]
496+
include::../components/elasticsearch-client-compatibility/_apache_based.adoc[]
476497

477498
[[backend-elasticsearch-configuration-http-client]]
478499
=== [[_custom_http_client_configurations]] Custom HTTP client configurations
@@ -485,7 +506,7 @@ See the following sections to learn how each of the clients can be configured.
485506
[[backend-elasticsearch-configuration-http-client-elasticsearch-rest]]
486507
=== Elasticsearch low level client
487508
It is possible to directly configure the underlying Apache HTTP 4 client of the
488-
<<backend-elasticsearch-configuration-client-elasticsearch-client-rest,Elasticsearch low level client>>
509+
<<backend-elasticsearch-configuration-client-elasticsearch-client-rest4,Elasticsearch low level client>>
489510
using an instance of `org.apache.http.impl.nio.client.HttpAsyncClientBuilder`.
490511

491512
With this API you can add interceptors, change the keep alive, the max connections,
@@ -611,6 +632,46 @@ include::{resourcesdir}/configuration/http-client-configurer-opensearch.properti
611632
Any setting defined by a custom http client configurer will override any other setting defined by Hibernate Search.
612633
====
613634

635+
[[backend-elasticsearch-configuration-http-client-elasticsearch-jdk]]
636+
=== JDK `HttpClient` based REST client
637+
It is possible to directly configure the underlying
638+
<<backend-elasticsearch-configuration-client-elasticsearch-client-jdk,`HttpClient`>>
639+
using an instance of `java.net.http.HttpClient.Builder`.
640+
641+
Configuring the HTTP client directly requires two steps:
642+
643+
. Define a class that implements the `org.hibernate.search.backend.elasticsearch.client.jdk.ElasticsearchHttpClientConfigurer` interface.
644+
. Configure Hibernate Search to use that implementation by setting the configuration property
645+
`hibernate.search.backend.client.configurer`
646+
to a <<configuration-bean-reference-parsing,bean reference>> pointing to the implementation,
647+
for example `class:org.hibernate.search.documentation.backend.elasticsearch.client.jdk.HttpClientConfigurer`.
648+
649+
.Implementing and using a `ElasticsearchHttpClientConfigurer`
650+
====
651+
[source, java, indent=0, subs="+callouts"]
652+
----
653+
include::{sourcedir}/org/hibernate/search/documentation/backend/elasticsearch/client/jdk/HttpClientConfigurer.java[tags=include]
654+
----
655+
<1> The class has to implement the `ElasticsearchHttpClientConfigurer` interface.
656+
<2> The `configure` method provides the access to the `ElasticsearchHttpClientConfigurationContext`.
657+
<3> From the context it is possible to get the `HttpClient.Builder`.
658+
<4> Finally, you can use the builder to configure the client with your custom settings.
659+
====
660+
661+
.Define a custom http client configurer in the properties
662+
====
663+
[source, xml, indent=0, subs="+callouts"]
664+
----
665+
include::{resourcesdir}/configuration/http-client-configurer-jdk.properties[tags=include]
666+
----
667+
<1> Specify the HTTP client configurer.
668+
====
669+
670+
[NOTE]
671+
====
672+
Any setting defined by a custom http client configurer will override any other setting defined by Hibernate Search.
673+
====
674+
614675
[[backend-elasticsearch-configuration-version]]
615676
== [[backend-elasticsearch-configuration-dialect]] Version compatibility
616677

0 commit comments

Comments
 (0)