How to configure index names for multiple Environments?

I’m working with Hibernate Search version: 6.1.0.Final with a single Elasticsearch Cluster that will serve Multiple environments Such as DEV,REC OR PROD.

My configuration file looks like:
spring:
jpa:
properties:
hibernate:
search:
backend:
analysis:
configurer: MyLuceneAnalysisConfigurer
elasticsearch:
index_management_strategy: create
hosts: localhost:9200
protocol: https
username:
password:

My class looks like :

@Indexed
public class Article {

@Id
private Long id;


}

When I use a default hibernate serach configuration indexing works well with the wanted result:

{
“_index”: “article-000001”,
“_type”: “_doc”,
“_id”: “689”,
}

My question is when i try to deliver the application in second then third enironments : How To configure index name for performing indexing and create index for a such environment like to have something something like ?

For DEV environnement:
{
“_index”: “Dev-article-000001”,
“_type”: “_doc”,
“_id”: “689”,
}

For REC environnement:
{
“_index”: “Rec-article-000001”,
“_type”: “_doc”,
“_id”: “689”,
}

For PROD environnement:
{
“_index”: “Prod-article-000001”,
“_type”: “_doc”,
“_id”: “689”,
}

You could use an index layout strategy?

(note the strategy can be a Spring bean, so you can use whatever means necessary to determine in which environment your application is running)

1 Like

Thank you. that is even easier as good solution.

Hello yrodiere,

Custom strategy woks well and it meets the need.

There is still only one small problem related to load externalized properties from configuration file.
I used hibernate search/ORm v 6.1.0 for configuration.

When i run the application using spring boot 2.0, CustomLayoutStrategyBean can’t load application properties, “environment” comes with null value.
it is loaded after spring context is been initialized inside postConstruct method not inside createWriteAlias.

It seems like Hibernate context get loaded before spring context.

@Component
public class CustomLayoutStrategyBean implements IndexLayoutStrategy
{

@Value("${application.env}")
public String environment;

@Override
public String createInitialElasticsearchIndexName(String hibernateSearchIndexName) {
    return hibernateSearchIndexName + "-" + environment;
}

@Override
public String createWriteAlias(String hibernateSearchIndexName) {
    return environment + "-" + hibernateSearchIndexName + "-write";
}

@Override
public String createReadAlias(String hibernateSearchIndexName) {
    return environment + "-" + hibernateSearchIndexName;
}

@Override
public String extractUniqueKeyFromHibernateSearchIndexName(
        String hibernateSearchIndexName) {
    return hibernateSearchIndexName;
}

@PostConstruct
public void postConstruct() {
    LOGGER.info("MyComponent in postConstruct: [{}]", value); // (1) displays: REC
}

}

configuration file looks like :slight_smile:
spring:
jpa:
properties:
hibernate:
search:
backend:layout:strategy: CustomLayoutStrategyBean
analysis:configurer: MyLuceneAnalysisConfigurer
elasticsearch:
index_management_strategy: create
hosts: localhost:9200
protocol: https
username:
password:

It seems like Hibernate context get loaded before spring context.

Is there any wahy to read properties files just before Hibernate load the bean to apply costum strategy ?

The solution will probably lie in changing something on the Spring config/setup. I’m afraid it’s been a long time since I worked on a serious (non-demo) project using Spring, so I couldn’t say… other than you don’t seem to define application.env in your configuration file, so that looks suspicious.

Maybe this can help: java - How do you get current active/default Environment profile programmatically in Spring? - Stack Overflow

Otherwise, maybe ask the Spring community? It’s quite active on stackoverflow.

It’s done, thanks a lot yrodiere. :slight_smile: :crossed_fingers: