Skip to content

Commit a2a2fb6

Browse files
committed
HSEARCH-5464 Elasticsearch-java client
1 parent dcb28d6 commit a2a2fb6

26 files changed

+391
-47
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
SPDX-License-Identifier: Apache-2.0
4+
Copyright Red Hat Inc. and Hibernate Authors
5+
-->
6+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
8+
<modelVersion>4.0.0</modelVersion>
9+
<parent>
10+
<groupId>org.hibernate.search</groupId>
11+
<artifactId>hibernate-search-parent-public</artifactId>
12+
<version>8.2.0-SNAPSHOT</version>
13+
<relativePath>../../../build/parents/public</relativePath>
14+
</parent>
15+
<artifactId>hibernate-search-backend-elasticsearch-client-elasticsearch-java</artifactId>
16+
17+
<name>Hibernate Search Backend - Elasticsearch client based on the low-level Elasticsearch java client</name>
18+
<description>Hibernate Search Elasticsearch client based on the low-level Elasticsearch java client</description>
19+
20+
<properties>
21+
<!-- This is a publicly distributed module that should be published: -->
22+
<deploy.skip>false</deploy.skip>
23+
<java.module.name>org.hibernate.search.backend.elasticsearch.client.elasticsearch.restclient</java.module.name>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.hibernate.search</groupId>
29+
<artifactId>hibernate-search-engine</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.hibernate.search</groupId>
33+
<artifactId>hibernate-search-backend-elasticsearch-client-common</artifactId>
34+
</dependency>
35+
<dependency>
36+
<groupId>co.elastic.clients</groupId>
37+
<artifactId>elasticsearch-java</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.jboss.logging</groupId>
41+
<artifactId>jboss-logging</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.jboss.logging</groupId>
45+
<artifactId>jboss-logging-annotations</artifactId>
46+
</dependency>
47+
<dependency>
48+
<groupId>com.google.code.gson</groupId>
49+
<artifactId>gson</artifactId>
50+
</dependency>
51+
52+
<!-- We only depend on Jackson indirectly through the Elasticsearch client sniffer,
53+
but Dependabot apparently ignores dependency management,
54+
so we need this explicit dependency to have Dependabot update versions. -->
55+
<dependency>
56+
<groupId>com.fasterxml.jackson.core</groupId>
57+
<artifactId>jackson-core</artifactId>
58+
</dependency>
59+
60+
<!-- Test -->
61+
<dependency>
62+
<groupId>org.hibernate.search</groupId>
63+
<artifactId>hibernate-search-util-internal-test-common</artifactId>
64+
<scope>test</scope>
65+
</dependency>
66+
</dependencies>
67+
68+
<build>
69+
<plugins>
70+
<plugin>
71+
<groupId>org.moditect</groupId>
72+
<artifactId>moditect-maven-plugin</artifactId>
73+
</plugin>
74+
</plugins>
75+
</build>
76+
</project>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* SPDX-License-Identifier: Apache-2.0
33
* Copyright Red Hat Inc. and Hibernate Authors
44
*/
5-
package org.hibernate.search.backend.elasticsearch.client.elasticsearch.lowlevel;
5+
package org.hibernate.search.backend.elasticsearch.client.elasticsearch.restclient;
66

77

88
import org.hibernate.search.engine.cfg.ConfigurationPropertySource;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.search.backend.elasticsearch.client.elasticsearch.restclient;
6+
7+
/**
8+
* An extension point allowing fine tuning of the Apache HTTP Client used by the Elasticsearch integration.
9+
* <p>
10+
* This enables in particular connecting to cloud services that require a particular authentication method,
11+
* such as request signing on Amazon Web Services.
12+
* <p>
13+
* The ElasticsearchHttpClientConfigurer implementation will be given access to the HTTP client builder
14+
* on startup.
15+
* <p>
16+
* Note that you don't have to configure the client unless you have specific needs:
17+
* the default configuration should work just fine for an on-premise Elasticsearch server.
18+
*/
19+
public interface ElasticsearchHttpClientConfigurer {
20+
21+
/**
22+
* Configure the HTTP Client.
23+
* <p>
24+
* This method is called once for every configurer, each time an Elasticsearch client is set up.
25+
* <p>
26+
* Implementors should take care of only applying configuration if relevant:
27+
* there may be multiple, conflicting configurers in the path, so implementors should first check
28+
* (through a configuration property) whether they are needed or not before applying any modification.
29+
* For example an authentication configurer could decide not to do anything if no username is provided,
30+
* or if the configuration property {@code my.configurer.enabled} is {@code false}.
31+
*
32+
* @param context A configuration context giving access to the Apache HTTP client builder
33+
* and configuration properties in particular.
34+
*/
35+
void configure(ElasticsearchHttpClientConfigurationContext context);
36+
37+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.search.backend.elasticsearch.client.elasticsearch.restclient.cfg;
6+
7+
import org.hibernate.search.backend.elasticsearch.client.common.cfg.ElasticsearchBackendClientCommonSettings;
8+
import org.hibernate.search.backend.elasticsearch.client.elasticsearch.restclient.ElasticsearchHttpClientConfigurer;
9+
10+
/**
11+
* Specific configuration properties for the Elasticsearch backend's rest client based on the Elasticsearch's low-level rest client.
12+
* <p>
13+
* Constants in this class are to be appended to a prefix to form a property key;
14+
* see {@link org.hibernate.search.engine.cfg.BackendSettings} for details.
15+
*
16+
* @author Gunnar Morling
17+
*/
18+
public final class ElasticsearchBackendClientSettings {
19+
20+
private ElasticsearchBackendClientSettings() {
21+
}
22+
23+
/**
24+
* A {@link ElasticsearchHttpClientConfigurer} that defines custom HTTP client configuration.
25+
* <p>
26+
* It can be used for example to tune the SSL context to accept self-signed certificates.
27+
* It allows overriding other HTTP client settings, such as {@link ElasticsearchBackendClientCommonSettings#USERNAME} or {@link ElasticsearchBackendClientCommonSettings#MAX_CONNECTIONS_PER_ROUTE}.
28+
* <p>
29+
* Expects a reference to a bean of type {@link ElasticsearchHttpClientConfigurer}.
30+
* <p>
31+
* Defaults to no value.
32+
*/
33+
public static final String CLIENT_CONFIGURER = "client.configurer";
34+
35+
/**
36+
* Default values for the different settings if no values are given.
37+
*/
38+
public static final class Defaults {
39+
40+
private Defaults() {
41+
}
42+
}
43+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.search.backend.elasticsearch.client.elasticsearch.restclient.cfg.spi;
6+
7+
import org.hibernate.search.engine.cfg.EngineSettings;
8+
9+
/**
10+
* Configuration properties for the Elasticsearch backend that are considered SPI (and not API).
11+
*/
12+
public final class ElasticsearchBackendClientSpiSettings {
13+
14+
/**
15+
* The prefix expected for the key of every Hibernate Search configuration property.
16+
*/
17+
public static final String PREFIX = EngineSettings.PREFIX + "backend.";
18+
19+
/**
20+
* An external Elasticsearch client instance that Hibernate Search should use for all requests to Elasticsearch.
21+
* <p>
22+
* If this is set, Hibernate Search will not attempt to create its own Elasticsearch,
23+
* and all other client-related configuration properties
24+
* (hosts/uris, authentication, discovery, timeouts, max connections, configurer, ...)
25+
* will be ignored.
26+
* <p>
27+
* Expects a reference to a bean of type {@link org.elasticsearch.client.RestClient}.
28+
* <p>
29+
* Defaults to nothing: if no client instance is provided, Hibernate Search will create its own.
30+
* <p>
31+
* <strong>WARNING - Incubating API:</strong> the underlying client class may change without prior notice.
32+
*
33+
* @see org.hibernate.search.engine.cfg The core documentation of configuration properties,
34+
* which includes a description of the "bean reference" properties and accepted values.
35+
*/
36+
public static final String CLIENT_INSTANCE = "client.instance";
37+
38+
private ElasticsearchBackendClientSpiSettings() {
39+
}
40+
41+
/**
42+
* Configuration property keys without the {@link #PREFIX prefix}.
43+
*/
44+
public static class Radicals {
45+
46+
private Radicals() {
47+
}
48+
}
49+
50+
public static final class Defaults {
51+
52+
private Defaults() {
53+
}
54+
}
55+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* SPDX-License-Identifier: Apache-2.0
33
* Copyright Red Hat Inc. and Hibernate Authors
44
*/
5-
package org.hibernate.search.backend.elasticsearch.client.elasticsearch.lowlevel.impl;
5+
package org.hibernate.search.backend.elasticsearch.client.elasticsearch.restclient.impl;
66

77
import java.io.IOException;
88
import java.nio.ByteBuffer;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.search.backend.elasticsearch.client.elasticsearch.restclient.impl;
6+
7+
import java.io.FilterOutputStream;
8+
import java.io.IOException;
9+
import java.io.OutputStream;
10+
11+
final class CountingOutputStream extends FilterOutputStream {
12+
13+
private long bytesWritten = 0L;
14+
15+
public CountingOutputStream(OutputStream out) {
16+
super( out );
17+
}
18+
19+
@Override
20+
public void write(int b) throws IOException {
21+
out.write( b );
22+
count( 1 );
23+
}
24+
25+
@Override
26+
public void write(byte[] b) throws IOException {
27+
write( b, 0, b.length );
28+
}
29+
30+
@Override
31+
public void write(byte[] b, int off, int len) throws IOException {
32+
out.write( b, off, len );
33+
count( len );
34+
}
35+
36+
void count(int written) {
37+
if ( written > 0 ) {
38+
bytesWritten += written;
39+
}
40+
}
41+
42+
public long getBytesWritten() {
43+
return bytesWritten;
44+
}
45+
46+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* SPDX-License-Identifier: Apache-2.0
33
* Copyright Red Hat Inc. and Hibernate Authors
44
*/
5-
package org.hibernate.search.backend.elasticsearch.client.elasticsearch.lowlevel.impl;
5+
package org.hibernate.search.backend.elasticsearch.client.elasticsearch.restclient.impl;
66

77

88
import java.util.concurrent.TimeUnit;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.search.backend.elasticsearch.client.elasticsearch.restclient.impl;
6+
7+
import org.hibernate.search.backend.elasticsearch.client.common.spi.ElasticsearchClientFactory;
8+
import org.hibernate.search.engine.environment.bean.BeanHolder;
9+
import org.hibernate.search.engine.environment.bean.spi.BeanConfigurationContext;
10+
import org.hibernate.search.engine.environment.bean.spi.BeanConfigurer;
11+
12+
public class ElasticsearchClientBeanConfigurer implements BeanConfigurer {
13+
@Override
14+
public void configure(BeanConfigurationContext context) {
15+
context.define(
16+
ElasticsearchClientFactory.class, "elasticsearch-java",
17+
beanResolver -> BeanHolder.of( new ElasticsearchClientFactoryImpl() )
18+
);
19+
}
20+
}

0 commit comments

Comments
 (0)