Creating two seperate indexes

In our project we want to use Hibernate Search 6 and local Lucene indices. Data is stored in two data sources with identical database schemas but not necessarily storing the same data. Therefore we want to create two seperate indices. After doing the inital setup I realized that I cannot declare multiple indices for one entity and I’m not sure if this approach is feasible at all.

My questions:

  • First of all: Can we do this the way we intent to do it using Hibernate Search?
  • How would we create two seperate indices for one entity?

The setup is actually not worth mentioning:
A basic entity

@Entity
@Indexed
public class Person{

@Id
@GeneratedValue
private int id;

@FullTextField
String s;

...
}

… and an entity manager to the corresponding data source

@Transactional()
@Override
public void initializeHibernateSearch(EntityManager entityManager) {
  
  SearchSession searchSession = Search.session(entityManager);
  
  MassIndexer indexer = searchSession.massIndexer();
  
  try {
      indexer.startAndWait();
  } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  }
}

If that’s the case, do you have two separate entity manager factories / session factories / persistence units? Because you should. And if you do, then you can simply configure index storage differently in each entity manager factory by setting hibernate.search.backend.directory.root to a different filesystem path.

Thank you for your reply. I set individual root paths (using commented out part) and achieved basic functionality of Hibernate Search in my project. I’m now struggling to set the path via application.properties file. I tried setting it via

xxx.hibernate.search.backend.directory.root = C:/...

and

xxx.jpa.properties.hibernate.search.backend.directory.root = C:/...

but no success. I have feeling this might not necessarily be Hibernate Search related but maybe you could give some insight anyway?

    @Bean(name = ENTITYMANAGER_BEAN_NAME)
    @ConfigurationProperties(prefix = "xxx")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
            @Qualifier(DATASOURCE_BEAN_NAME) DataSource dataSource, JpaProperties jpaProperties, HibernateProperties hibernateProperties) {

        HibernateSettings hibernateSettings = new HibernateSettings();
        LocalContainerEntityManagerFactoryBean emfb = builder.dataSource(dataSource).packages(ENTITY_SCAN_BASEPACKAGE).persistenceUnit(PERSISTENCE_UNIT_NAME)
                .properties(hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), hibernateSettings)).build();

//        Properties p = new Properties();
//        p.put("hibernate.search.backend.directory.root", "C:/yyy");
//        emfb.setJpaProperties(p);

        return emfb;
    }

I’m sorry but no, I don’t know how that part of Spring (@ConfigurationProperties(prefix = ...)) works. You’ll probably have better luck asking the Spring community.

I ended up creating a custom property entity with

@ConfigurationProperties(prefix = "hibernatesearchpropertiesxxx")
public class HibernateSearchPropertiesXXX extends HibernateSearchProperties

and used it to set the property in the EntityManagerFactory

Properties p = new Properties();
p.put("hibernate.search.backend.directory.root", hibernateSearchPropertiesXXX.getIndexRoot());
emfb.setJpaProperties(p);
1 Like

Thanks for the update!