Skip to content

Commit

Permalink
Move the index layout strategy to the Search runtime config
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta committed Sep 18, 2024
1 parent 8e6e43d commit affaf3e
Show file tree
Hide file tree
Showing 16 changed files with 407 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ interface ElasticsearchBackendBuildTimeConfig {
*/
Optional<ElasticsearchVersion> version();

/**
* Configuration for the index layout.
*/
LayoutConfig layout();

/**
* The default configuration for the Elasticsearch indexes.
*/
Expand Down Expand Up @@ -178,45 +173,6 @@ interface AnalysisConfig {
Optional<List<String>> configurer();
}

@ConfigGroup
interface LayoutConfig {
/**
* A xref:hibernate-search-orm-elasticsearch.adoc#bean-reference-note-anchor[bean reference] to the component
* used to configure the Elasticsearch layout: index names, index aliases, ...
*
* The referenced bean must implement `IndexLayoutStrategy`.
*
* Available built-in implementations:
*
* `simple`::
* The default, future-proof strategy: if the index name in Hibernate Search is `myIndex`,
* this strategy will create an index named `myindex-000001`, an alias for write operations named `myindex-write`,
* and an alias for read operations named `myindex-read`.
* `no-alias`::
* A strategy without index aliases, mostly useful on legacy clusters:
* if the index name in Hibernate Search is `myIndex`,
* this strategy will create an index named `myindex`, and will not use any alias.
*
* See
* link:{hibernate-search-docs-url}#backend-elasticsearch-indexlayout[this section of the reference documentation]
* for more information.
*
* [NOTE]
* ====
* Instead of setting this configuration property,
* you can simply annotate your custom `IndexLayoutStrategy` implementation with `@SearchExtension`
* and leave the configuration property unset: Hibernate Search will use the annotated implementation automatically.
* See xref:hibernate-search-orm-elasticsearch.adoc#plugging-in-custom-components[this section]
* for more information.
*
* If this configuration property is set, it takes precedence over any `@SearchExtension` annotation.
* ====
*
* @asciidoclet
*/
Optional<String> strategy();
}

@ConfigGroup
interface CoordinationConfig {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,6 @@ private void contributeBackendBuildTimeProperties(BiConsumer<String, Object> pro
elasticsearchBackendConfig.version());
}

// Settings that may default to a @SearchExtension-annotated-bean
addBackendConfig(propertyCollector, backendName,
ElasticsearchBackendSettings.LAYOUT_STRATEGY,
HibernateSearchBeanUtil.singleExtensionBeanReferenceFor(
elasticsearchBackendConfig == null ? Optional.empty()
: elasticsearchBackendConfig.layout().strategy(),
IndexLayoutStrategy.class, persistenceUnitName, backendName, null));

// Index defaults at the backend level
contributeBackendIndexBuildTimeProperties(propertyCollector, backendName, null,
elasticsearchBackendConfig == null ? null : elasticsearchBackendConfig.indexDefaults());
Expand Down Expand Up @@ -472,6 +464,12 @@ private void contributeBackendRuntimeProperties(BiConsumer<String, Object> prope
addBackendConfig(propertyCollector, backendName, ElasticsearchBackendSettings.DISCOVERY_REFRESH_INTERVAL,
elasticsearchBackendConfig.discovery().refreshInterval().getSeconds());
}
// Settings that may default to a @SearchExtension-annotated-bean
addBackendConfig(propertyCollector, backendName,
ElasticsearchBackendSettings.LAYOUT_STRATEGY,
HibernateSearchBeanUtil.singleExtensionBeanReferenceFor(
elasticsearchBackendConfig.layout().strategy(),
IndexLayoutStrategy.class, persistenceUnitName, backendName, null));
}

// Settings that may default to a @SearchExtension-annotated-bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ interface ElasticsearchBackendRuntimeConfig {
@ConfigDocSection
@ConfigDocMapKey("index-name")
Map<String, ElasticsearchIndexRuntimeConfig> indexes();

/**
* Configuration for the index layout.
*/
LayoutConfig layout();
}

enum ElasticsearchClientProtocol {
Expand Down Expand Up @@ -628,4 +633,43 @@ interface MultiTenancyConfig {
Optional<List<String>> tenantIds();

}

@ConfigGroup
interface LayoutConfig {
/**
* A xref:hibernate-search-orm-elasticsearch.adoc#bean-reference-note-anchor[bean reference] to the component
* used to configure the Elasticsearch layout: index names, index aliases, ...
*
* The referenced bean must implement `IndexLayoutStrategy`.
*
* Available built-in implementations:
*
* `simple`::
* The default, future-proof strategy: if the index name in Hibernate Search is `myIndex`,
* this strategy will create an index named `myindex-000001`, an alias for write operations named `myindex-write`,
* and an alias for read operations named `myindex-read`.
* `no-alias`::
* A strategy without index aliases, mostly useful on legacy clusters:
* if the index name in Hibernate Search is `myIndex`,
* this strategy will create an index named `myindex`, and will not use any alias.
*
* See
* link:{hibernate-search-docs-url}#backend-elasticsearch-indexlayout[this section of the reference documentation]
* for more information.
*
* [NOTE]
* ====
* Instead of setting this configuration property,
* you can simply annotate your custom `IndexLayoutStrategy` implementation with `@SearchExtension`
* and leave the configuration property unset: Hibernate Search will use the annotated implementation automatically.
* See xref:hibernate-search-orm-elasticsearch.adoc#plugging-in-custom-components[this section]
* for more information.
*
* If this configuration property is set, it takes precedence over any `@SearchExtension` annotation.
* ====
*
* @asciidoclet
*/
Optional<String> strategy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,6 @@ interface ElasticsearchBackendBuildTimeConfig {
*/
Optional<ElasticsearchVersion> version();

/**
* Configuration for the index layout.
*/
LayoutConfig layout();

/**
* The default configuration for the Elasticsearch indexes.
*/
Expand Down Expand Up @@ -195,45 +190,6 @@ interface AnalysisConfig {
Optional<List<String>> configurer();
}

@ConfigGroup
interface LayoutConfig {
/**
* A xref:hibernate-search-standalone-elasticsearch.adoc#bean-reference-note-anchor[bean reference] to the component
* used to configure the Elasticsearch layout: index names, index aliases, ...
*
* The referenced bean must implement `IndexLayoutStrategy`.
*
* Available built-in implementations:
*
* `simple`::
* The default, future-proof strategy: if the index name in Hibernate Search is `myIndex`,
* this strategy will create an index named `myindex-000001`, an alias for write operations named `myindex-write`,
* and an alias for read operations named `myindex-read`.
* `no-alias`::
* A strategy without index aliases, mostly useful on legacy clusters:
* if the index name in Hibernate Search is `myIndex`,
* this strategy will create an index named `myindex`, and will not use any alias.
*
* See
* link:{hibernate-search-docs-url}#backend-elasticsearch-indexlayout[this section of the reference documentation]
* for more information.
*
* [NOTE]
* ====
* Instead of setting this configuration property,
* you can simply annotate your custom `IndexLayoutStrategy` implementation with `@SearchExtension`
* and leave the configuration property unset: Hibernate Search will use the annotated implementation automatically.
* See xref:hibernate-search-standalone-elasticsearch.adoc#plugging-in-custom-components[this section]
* for more information.
*
* If this configuration property is set, it takes precedence over any `@SearchExtension` annotation.
* ====
*
* @asciidoclet
*/
Optional<String> strategy();
}

@ConfigGroup
interface MappingConfig {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,6 @@ private void contributeBackendBuildTimeProperties(BiConsumer<String, Object> pro
elasticsearchBackendConfig.version());
}

// Settings that may default to a @SearchExtension-annotated-bean
addBackendConfig(propertyCollector, backendName,
ElasticsearchBackendSettings.LAYOUT_STRATEGY,
HibernateSearchBeanUtil.singleExtensionBeanReferenceFor(
elasticsearchBackendConfig == null ? Optional.empty()
: elasticsearchBackendConfig.layout().strategy(),
IndexLayoutStrategy.class, backendName, null));

// Index defaults at the backend level
contributeBackendIndexBuildTimeProperties(propertyCollector, backendName, null,
elasticsearchBackendConfig == null ? null : elasticsearchBackendConfig.indexDefaults());
Expand Down Expand Up @@ -327,6 +319,13 @@ private void contributeBackendRuntimeProperties(BiConsumer<String, Object> prope
addBackendConfig(propertyCollector, backendName, ElasticsearchBackendSettings.DISCOVERY_REFRESH_INTERVAL,
elasticsearchBackendConfig.discovery().refreshInterval().getSeconds());
}

// Settings that may default to a @SearchExtension-annotated-bean
addBackendConfig(propertyCollector, backendName,
ElasticsearchBackendSettings.LAYOUT_STRATEGY,
HibernateSearchBeanUtil.singleExtensionBeanReferenceFor(
elasticsearchBackendConfig.layout().strategy(),
IndexLayoutStrategy.class, backendName, null));
}

// Settings that may default to a @SearchExtension-annotated-bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ interface ElasticsearchBackendRuntimeConfig {
@ConfigDocSection
@ConfigDocMapKey("index-name")
Map<String, ElasticsearchIndexRuntimeConfig> indexes();

/**
* Configuration for the index layout.
*/
LayoutConfig layout();
}

enum ElasticsearchClientProtocol {
Expand Down Expand Up @@ -521,6 +526,45 @@ interface ElasticsearchIndexIndexingConfig {
OptionalInt maxBulkSize();
}

@ConfigGroup
interface LayoutConfig {
/**
* A xref:hibernate-search-standalone-elasticsearch.adoc#bean-reference-note-anchor[bean reference] to the component
* used to configure the Elasticsearch layout: index names, index aliases, ...
*
* The referenced bean must implement `IndexLayoutStrategy`.
*
* Available built-in implementations:
*
* `simple`::
* The default, future-proof strategy: if the index name in Hibernate Search is `myIndex`,
* this strategy will create an index named `myindex-000001`, an alias for write operations named `myindex-write`,
* and an alias for read operations named `myindex-read`.
* `no-alias`::
* A strategy without index aliases, mostly useful on legacy clusters:
* if the index name in Hibernate Search is `myIndex`,
* this strategy will create an index named `myindex`, and will not use any alias.
*
* See
* link:{hibernate-search-docs-url}#backend-elasticsearch-indexlayout[this section of the reference documentation]
* for more information.
*
* [NOTE]
* ====
* Instead of setting this configuration property,
* you can simply annotate your custom `IndexLayoutStrategy` implementation with `@SearchExtension`
* and leave the configuration property unset: Hibernate Search will use the annotated implementation automatically.
* See xref:hibernate-search-standalone-elasticsearch.adoc#plugging-in-custom-components[this section]
* for more information.
*
* If this configuration property is set, it takes precedence over any `@SearchExtension` annotation.
* ====
*
* @asciidoclet
*/
Optional<String> strategy();
}

static String elasticsearchVersionPropertyKey(String backendName) {
return backendPropertyKey(backendName, null, "version");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.quarkus.it.hibernate.search.orm.elasticsearch.layout;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Named;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.hibernate.search.backend.elasticsearch.index.layout.IndexLayoutStrategy;

@ApplicationScoped
@Named("CustomIndexLayoutStrategy")
public class CustomIndexLayoutStrategy implements IndexLayoutStrategy {

@ConfigProperty(name = "test.index-layout.prefix", defaultValue = "-")
String prefix;

@Override
public String createInitialElasticsearchIndexName(String hibernateSearchIndexName) {
return "%s%s-000001".formatted(prefix, hibernateSearchIndexName);
}

@Override
public String createWriteAlias(String hibernateSearchIndexName) {
return "%s%s-write".formatted(prefix, hibernateSearchIndexName);
}

@Override
public String createReadAlias(String hibernateSearchIndexName) {
return "%s%s-read".formatted(prefix, hibernateSearchIndexName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.quarkus.it.hibernate.search.orm.elasticsearch.layout;

import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

import org.hibernate.SessionFactory;
import org.hibernate.search.backend.elasticsearch.index.ElasticsearchIndexManager;
import org.hibernate.search.engine.environment.bean.BeanReference;
import org.hibernate.search.mapper.orm.mapping.SearchMapping;

@Path("/test/layout-strategy")
public class HibernateSearchLayoutTestResource {

@Inject
SessionFactory sessionFactory;

@Inject
SearchMapping searchMapping;

@GET
@Path("/property")
@Transactional
public String layoutStrategy() {
return ((BeanReference<?>) sessionFactory.getProperties().get("hibernate.search.backend.layout.strategy")).toString();
}

@GET
@Path("/index-name")
@Transactional
public String name() {
var descriptor = searchMapping.indexedEntity(LayoutEntity.class)
.indexManager()
.unwrap(ElasticsearchIndexManager.class)
.descriptor();

return "%s - %s - %s".formatted(descriptor.hibernateSearchName(), descriptor.readName(), descriptor.writeName());
}
}
Loading

0 comments on commit affaf3e

Please sign in to comment.