Skip to content

Commit 0f88242

Browse files
authored
Merge pull request #39416 from yrodiere/backend-elasticsearch-shared
Shared extension for the Hibernate Search Elasticsearch backend
2 parents 89bc558 + 1c43f90 commit 0f88242

File tree

36 files changed

+1321
-1633
lines changed

36 files changed

+1321
-1633
lines changed

Diff for: bom/application/pom.xml

+10
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,16 @@
13781378
<artifactId>quarkus-mongodb-panache-common-deployment</artifactId>
13791379
<version>${project.version}</version>
13801380
</dependency>
1381+
<dependency>
1382+
<groupId>io.quarkus</groupId>
1383+
<artifactId>quarkus-hibernate-search-backend-elasticsearch-common</artifactId>
1384+
<version>${project.version}</version>
1385+
</dependency>
1386+
<dependency>
1387+
<groupId>io.quarkus</groupId>
1388+
<artifactId>quarkus-hibernate-search-backend-elasticsearch-common-deployment</artifactId>
1389+
<version>${project.version}</version>
1390+
</dependency>
13811391
<dependency>
13821392
<groupId>io.quarkus</groupId>
13831393
<artifactId>quarkus-hibernate-search-orm-elasticsearch</artifactId>

Diff for: devtools/bom-descriptor-json/pom.xml

+13
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,19 @@
954954
</exclusion>
955955
</exclusions>
956956
</dependency>
957+
<dependency>
958+
<groupId>io.quarkus</groupId>
959+
<artifactId>quarkus-hibernate-search-backend-elasticsearch-common</artifactId>
960+
<version>${project.version}</version>
961+
<type>pom</type>
962+
<scope>test</scope>
963+
<exclusions>
964+
<exclusion>
965+
<groupId>*</groupId>
966+
<artifactId>*</artifactId>
967+
</exclusion>
968+
</exclusions>
969+
</dependency>
957970
<dependency>
958971
<groupId>io.quarkus</groupId>
959972
<artifactId>quarkus-hibernate-search-orm-elasticsearch</artifactId>

Diff for: docs/pom.xml

+13
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,19 @@
965965
</exclusion>
966966
</exclusions>
967967
</dependency>
968+
<dependency>
969+
<groupId>io.quarkus</groupId>
970+
<artifactId>quarkus-hibernate-search-backend-elasticsearch-common-deployment</artifactId>
971+
<version>${project.version}</version>
972+
<type>pom</type>
973+
<scope>test</scope>
974+
<exclusions>
975+
<exclusion>
976+
<groupId>*</groupId>
977+
<artifactId>*</artifactId>
978+
</exclusion>
979+
</exclusions>
980+
</dependency>
968981
<dependency>
969982
<groupId>io.quarkus</groupId>
970983
<artifactId>quarkus-hibernate-search-orm-elasticsearch-deployment</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>quarkus-hibernate-search-backend-elasticsearch-common-parent</artifactId>
7+
<groupId>io.quarkus</groupId>
8+
<version>999-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>quarkus-hibernate-search-backend-elasticsearch-common-deployment</artifactId>
13+
<name>Quarkus - Hibernate Search - Elasticsearch - Deployment</name>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>io.quarkus</groupId>
18+
<artifactId>quarkus-core-deployment</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>io.quarkus</groupId>
22+
<artifactId>quarkus-elasticsearch-rest-client-common-deployment</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>io.quarkus</groupId>
26+
<artifactId>quarkus-hibernate-search-backend-elasticsearch-common</artifactId>
27+
</dependency>
28+
</dependencies>
29+
30+
<build>
31+
<plugins>
32+
<plugin>
33+
<artifactId>maven-compiler-plugin</artifactId>
34+
<configuration>
35+
<annotationProcessorPaths>
36+
<path>
37+
<groupId>io.quarkus</groupId>
38+
<artifactId>quarkus-extension-processor</artifactId>
39+
<version>${project.version}</version>
40+
</path>
41+
</annotationProcessorPaths>
42+
</configuration>
43+
</plugin>
44+
<plugin>
45+
<artifactId>maven-surefire-plugin</artifactId>
46+
<configuration>
47+
<skip>true</skip>
48+
</configuration>
49+
</plugin>
50+
</plugins>
51+
</build>
52+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.quarkus.hibernate.search.backend.elasticsearch.common.deployment;
2+
3+
import java.util.Map;
4+
5+
import io.quarkus.builder.item.MultiBuildItem;
6+
import io.quarkus.hibernate.search.backend.elasticsearch.common.runtime.HibernateSearchBackendElasticsearchBuildTimeConfig;
7+
import io.quarkus.hibernate.search.backend.elasticsearch.common.runtime.MapperContext;
8+
9+
public final class HibernateSearchBackendElasticsearchEnabledBuildItem extends MultiBuildItem {
10+
11+
private final MapperContext mapperContext;
12+
private final Map<String, HibernateSearchBackendElasticsearchBuildTimeConfig> buildTimeConfig;
13+
14+
public HibernateSearchBackendElasticsearchEnabledBuildItem(MapperContext mapperContext,
15+
Map<String, HibernateSearchBackendElasticsearchBuildTimeConfig> buildTimeConfig) {
16+
this.mapperContext = mapperContext;
17+
this.buildTimeConfig = buildTimeConfig;
18+
}
19+
20+
public MapperContext getMapperContext() {
21+
return mapperContext;
22+
}
23+
24+
public Map<String, HibernateSearchBackendElasticsearchBuildTimeConfig> getBuildTimeConfig() {
25+
return buildTimeConfig;
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package io.quarkus.hibernate.search.backend.elasticsearch.common.deployment;
2+
3+
import java.nio.file.Files;
4+
import java.nio.file.Path;
5+
import java.util.LinkedHashSet;
6+
import java.util.List;
7+
import java.util.Map.Entry;
8+
import java.util.Optional;
9+
import java.util.Set;
10+
11+
import org.hibernate.search.backend.elasticsearch.analysis.ElasticsearchAnalysisConfigurer;
12+
import org.hibernate.search.backend.elasticsearch.gson.spi.GsonClasses;
13+
import org.hibernate.search.backend.elasticsearch.index.layout.IndexLayoutStrategy;
14+
15+
import io.quarkus.arc.deployment.UnremovableBeanBuildItem;
16+
import io.quarkus.deployment.annotations.BuildProducer;
17+
import io.quarkus.deployment.annotations.BuildStep;
18+
import io.quarkus.deployment.annotations.BuildSteps;
19+
import io.quarkus.deployment.builditem.ApplicationArchivesBuildItem;
20+
import io.quarkus.deployment.builditem.HotDeploymentWatchedFileBuildItem;
21+
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
22+
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
23+
import io.quarkus.hibernate.search.backend.elasticsearch.common.runtime.HibernateSearchBackendElasticsearchBuildTimeConfig;
24+
import io.quarkus.hibernate.search.backend.elasticsearch.common.runtime.MapperContext;
25+
import io.quarkus.runtime.configuration.ConfigurationException;
26+
27+
@BuildSteps
28+
class HibernateSearchBackendElasticsearchProcessor {
29+
30+
@BuildStep
31+
void registerBeans(List<HibernateSearchBackendElasticsearchEnabledBuildItem> enabled,
32+
BuildProducer<UnremovableBeanBuildItem> unremovableBean) {
33+
if (enabled.isEmpty()) {
34+
return;
35+
}
36+
// Some user-injectable beans are retrieved programmatically and shouldn't be removed
37+
unremovableBean.produce(UnremovableBeanBuildItem.beanTypes(ElasticsearchAnalysisConfigurer.class,
38+
IndexLayoutStrategy.class));
39+
}
40+
41+
@BuildStep
42+
void registerReflectionForGson(List<HibernateSearchBackendElasticsearchEnabledBuildItem> enabled,
43+
BuildProducer<ReflectiveClassBuildItem> reflectiveClass) {
44+
if (enabled.isEmpty()) {
45+
return;
46+
}
47+
String[] reflectiveClasses = GsonClasses.typesRequiringReflection().toArray(String[]::new);
48+
reflectiveClass.produce(ReflectiveClassBuildItem.builder(reflectiveClasses)
49+
.reason(getClass().getName())
50+
.methods().fields().build());
51+
}
52+
53+
@BuildStep
54+
void processBuildTimeConfig(List<HibernateSearchBackendElasticsearchEnabledBuildItem> enabled,
55+
ApplicationArchivesBuildItem applicationArchivesBuildItem,
56+
BuildProducer<NativeImageResourceBuildItem> nativeImageResources,
57+
BuildProducer<HotDeploymentWatchedFileBuildItem> hotDeploymentWatchedFiles) {
58+
if (enabled.isEmpty()) {
59+
return;
60+
}
61+
for (HibernateSearchBackendElasticsearchEnabledBuildItem enabledItem : enabled) {
62+
processBuildTimeConfig(enabledItem, applicationArchivesBuildItem, nativeImageResources,
63+
hotDeploymentWatchedFiles);
64+
}
65+
}
66+
67+
private void processBuildTimeConfig(HibernateSearchBackendElasticsearchEnabledBuildItem enabled,
68+
ApplicationArchivesBuildItem applicationArchivesBuildItem,
69+
BuildProducer<NativeImageResourceBuildItem> nativeImageResources,
70+
BuildProducer<HotDeploymentWatchedFileBuildItem> hotDeploymentWatchedFiles) {
71+
Set<String> propertyKeysWithNoVersion = new LinkedHashSet<>();
72+
var buildTimeConfig = enabled.getBuildTimeConfig();
73+
74+
var mapperContext = enabled.getMapperContext();
75+
Set<String> allBackendNames = new LinkedHashSet<>(mapperContext.getBackendNamesForIndexedEntities());
76+
allBackendNames.addAll(buildTimeConfig.keySet());
77+
// For all backends referenced either through @Indexed(backend = ...) or configuration...
78+
for (String backendName : allBackendNames) {
79+
HibernateSearchBackendElasticsearchBuildTimeConfig backendConfig = buildTimeConfig.get(backendName);
80+
// ... we validate that the backend is configured and the version is present
81+
if (backendConfig == null || backendConfig.version().isEmpty()) {
82+
propertyKeysWithNoVersion.add(mapperContext.backendPropertyKey(backendName, null, "version"));
83+
}
84+
if (backendConfig == null) {
85+
continue;
86+
}
87+
// ... we register files referenced from backends configuration
88+
registerClasspathFilesFromBackendConfig(mapperContext, backendName, backendConfig,
89+
applicationArchivesBuildItem, nativeImageResources, hotDeploymentWatchedFiles);
90+
}
91+
if (!propertyKeysWithNoVersion.isEmpty()) {
92+
throw new ConfigurationException(
93+
"The Elasticsearch version needs to be defined via properties: "
94+
+ String.join(", ", propertyKeysWithNoVersion) + ".",
95+
propertyKeysWithNoVersion);
96+
}
97+
}
98+
99+
private static void registerClasspathFilesFromBackendConfig(MapperContext mapperContext, String backendName,
100+
HibernateSearchBackendElasticsearchBuildTimeConfig backendConfig,
101+
ApplicationArchivesBuildItem applicationArchivesBuildItem,
102+
BuildProducer<NativeImageResourceBuildItem> nativeImageResources,
103+
BuildProducer<HotDeploymentWatchedFileBuildItem> hotDeploymentWatchedFiles) {
104+
registerClasspathFilesFromIndexConfig(mapperContext, backendName, null, backendConfig.indexDefaults(),
105+
applicationArchivesBuildItem, nativeImageResources, hotDeploymentWatchedFiles);
106+
for (Entry<String, HibernateSearchBackendElasticsearchBuildTimeConfig.IndexConfig> entry : backendConfig.indexes()
107+
.entrySet()) {
108+
String indexName = entry.getKey();
109+
HibernateSearchBackendElasticsearchBuildTimeConfig.IndexConfig indexConfig = entry.getValue();
110+
registerClasspathFilesFromIndexConfig(mapperContext, backendName, indexName, indexConfig,
111+
applicationArchivesBuildItem, nativeImageResources, hotDeploymentWatchedFiles);
112+
}
113+
}
114+
115+
private static void registerClasspathFilesFromIndexConfig(MapperContext mapperContext, String backendName, String indexName,
116+
HibernateSearchBackendElasticsearchBuildTimeConfig.IndexConfig indexConfig,
117+
ApplicationArchivesBuildItem applicationArchivesBuildItem,
118+
BuildProducer<NativeImageResourceBuildItem> nativeImageResources,
119+
BuildProducer<HotDeploymentWatchedFileBuildItem> hotDeploymentWatchedFiles) {
120+
registerClasspathFileFromConfig(mapperContext, backendName, indexName, "schema-management.settings-file",
121+
indexConfig.schemaManagement().settingsFile(),
122+
applicationArchivesBuildItem, nativeImageResources, hotDeploymentWatchedFiles);
123+
registerClasspathFileFromConfig(mapperContext, backendName, indexName, "schema-management.mapping-file",
124+
indexConfig.schemaManagement().mappingFile(),
125+
applicationArchivesBuildItem, nativeImageResources, hotDeploymentWatchedFiles);
126+
}
127+
128+
private static void registerClasspathFileFromConfig(MapperContext mapperContext, String backendName, String indexName,
129+
String propertyKeyRadical,
130+
Optional<String> classpathFileOptional,
131+
ApplicationArchivesBuildItem applicationArchivesBuildItem,
132+
BuildProducer<NativeImageResourceBuildItem> nativeImageResources,
133+
BuildProducer<HotDeploymentWatchedFileBuildItem> hotDeploymentWatchedFiles) {
134+
if (!classpathFileOptional.isPresent()) {
135+
return;
136+
}
137+
String classpathFile = classpathFileOptional.get();
138+
139+
Path existingPath = applicationArchivesBuildItem.getRootArchive().getChildPath(classpathFile);
140+
141+
if (existingPath == null || Files.isDirectory(existingPath)) {
142+
//raise exception if explicit file is not present (i.e. not the default)
143+
throw new ConfigurationException(
144+
"Unable to find file referenced in '"
145+
+ mapperContext.backendPropertyKey(backendName, indexName, propertyKeyRadical) + "="
146+
+ classpathFile
147+
+ "'. Remove property or add file to your path.");
148+
}
149+
nativeImageResources.produce(new NativeImageResourceBuildItem(classpathFile));
150+
hotDeploymentWatchedFiles.produce(new HotDeploymentWatchedFileBuildItem(classpathFile));
151+
}
152+
153+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>quarkus-extensions-parent</artifactId>
7+
<groupId>io.quarkus</groupId>
8+
<version>999-SNAPSHOT</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>quarkus-hibernate-search-backend-elasticsearch-common-parent</artifactId>
14+
<name>Quarkus - Hibernate Search - Elasticsearch</name>
15+
<packaging>pom</packaging>
16+
<modules>
17+
<module>deployment</module>
18+
<module>runtime</module>
19+
</modules>
20+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>quarkus-hibernate-search-backend-elasticsearch-common-parent</artifactId>
7+
<groupId>io.quarkus</groupId>
8+
<version>999-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>quarkus-hibernate-search-backend-elasticsearch-common</artifactId>
13+
<name>Quarkus - Hibernate Search - Elasticsearch - Runtime</name>
14+
<description>Elasticsearch/OpenSearch backend for use in other Hibernate Search extensions</description>
15+
<dependencies>
16+
<dependency>
17+
<groupId>io.quarkus</groupId>
18+
<artifactId>quarkus-core</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>io.quarkus</groupId>
22+
<artifactId>quarkus-elasticsearch-rest-client-common</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.hibernate.search</groupId>
26+
<artifactId>hibernate-search-backend-elasticsearch</artifactId>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.graalvm.sdk</groupId>
30+
<artifactId>graal-sdk</artifactId>
31+
<scope>provided</scope>
32+
</dependency>
33+
</dependencies>
34+
35+
<build>
36+
<plugins>
37+
<plugin>
38+
<groupId>io.quarkus</groupId>
39+
<artifactId>quarkus-extension-maven-plugin</artifactId>
40+
</plugin>
41+
<plugin>
42+
<artifactId>maven-compiler-plugin</artifactId>
43+
<configuration>
44+
<annotationProcessorPaths>
45+
<path>
46+
<groupId>io.quarkus</groupId>
47+
<artifactId>quarkus-extension-processor</artifactId>
48+
<version>${project.version}</version>
49+
</path>
50+
</annotationProcessorPaths>
51+
</configuration>
52+
</plugin>
53+
</plugins>
54+
</build>
55+
</project>
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.quarkus.hibernate.search.orm.elasticsearch.runtime;
1+
package io.quarkus.hibernate.search.backend.elasticsearch.common.runtime;
22

33
import org.hibernate.search.backend.elasticsearch.ElasticsearchVersion;
44

0 commit comments

Comments
 (0)