Manual indexing without listeners

Hello,

is there a way to do manual indexing with the option autoregister_listeners set to false at Spring startup?

Here my conditionnal configuration

// start checkup
if (!profileService.hasProfileElasticSearch() || !isElasticSearchClusterUp()) {
	LOGGER.info("disabling HibernateSearch listeners...");
	hibernateProperties.put("hibernate.search.autoregister_listeners", "false");
}

And my manual indexing code

SearchSession searchSession = Search.session((EntityManager) sessionFactory.getCurrentSession());
SearchSessionWritePlan searchWritePlan = searchSession.writePlan();
searchWritePlan.addOrUpdate(rootRepository.get(getClassByName(type), id));
searchWritePlan.process();
searchWritePlan.execute();

But when i call the searchWritePlan i get a beautiful HSEARCH800001: Hibernate Search was not initialized.

My use case for that example is :

  • i start my spring instance with a dead elasticsearch cluster
  • i fix my cluster but i can’t restart my spring instance yet
  • i would like a scheduling task to manual index entities while the situation is degraded

Thanks :slight_smile:

Hello,

hibernate.search.autoregister_listeners was renamed to just hibernate.search.enabled in recent versions, because, well… that’s what it does. Setting it to false will completely disable Hibernate Search.

If you just want to disable automatic indexing, you can set hibernate.search.automatic_indexing.strategy to none, as explained here.

Out of curiosity, why do you disable automatic indexing in that case? Is it just for performance reasons? If it is to avoid exceptions being thrown when entities are changed, you could simply use the async synchronization strategy (it was named queued before 6.0.0.Beta5), and then any failure to index will just result in logs: it won’t throw any exception. You will still be able to reindex manually when the Elasticsearch cluster is back up, and you may not have to restart your application.

Hello,

it’s because if i don’t set autoregister_listeners to false with a shutdown cluster the startup is not working : org.hibernate.search.util.common.SearchException: HSEARCH400007: Elasticsearch request failed: java.net.ConnectException: Connection refused: no further information so i was trying to make it more resilient with conditional configuration

But … after another reading of my configuration the call made to elasticsearch is maybe triggered because of this :

hibernateProperties.put("hibernate.search.backends.signature.index_defaults.lifecycle.minimal_required_status", "yellow");

Edit

if(!isElasticSearchClusterUp()) {
	hibernateProperties.put("hibernate.search.automatic_indexing.strategy", "none");
} else {	 
 	hibernateProperties.put("hibernate.search.backends.xxxx.index_defaults.lifecycle.minimal_required_status", "yellow");
}

i tried to remove the status check when elasticsearch is dead, but still get the error at the startup

HSEARCH400080: Failed to detect the Elasticsearch version running on the cluster: HSEARCH400007: Elasticsearch request failed: java.net.ConnectException: Connection refused: no further information

In the meantime i check the documentation and i saw this :

Alternatively, you can tell Hibernate Search the Elasticsearch version to target. Hibernate Search will still query the Elasticsearch cluster to check that the configured version matches the actual version, but only after most of the metadata has been validated. This can be helpful when developing, in particular.

So i guess, it will not be possible to start with hibernate search enabled without elasticsearch up for now

Yes, that’s right. I suppose we could add a configuration option to disable version checking… However, that means you will have to provide the full Elasticsearch version (major and minor).
I created HSEARCH-3841 to address this eventually, because it’s actually something we’d need for applications started in docker-compose (the boot order of services in docker-compose is undefined).

Don’t hesitate to drop a comment if you have an idea about how to expose this feature, or if you want to contribute a patch.

i was able to do what i want with this : https://github.com/hibernate/hibernate-search/pull/2220

what do you think about it @yrodiere ?

I merged it; thank you again for your contribution.

This will be part of the next release, in a few weeks.

In the meantime, you should be able to work with the snapshots that we publish to the JBoss Maven Repository.

Just change the version of Hibernate Search to 6.0.0-SNAPSHOT, and add this to your POM:

<repositories>
    <!--
        Enable the JBoss Snapshots repository.
     -->
    <repository>
        <id>jboss-public-repository-group</id>
        <name>JBoss Public Maven Repository Group</name>
        <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
        <layout>default</layout>
        <releases>
            <enabled>false</enabled>
            <updatePolicy>never</updatePolicy>
        </releases>
        <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
        </snapshots>
    </repository>
</repositories>
1 Like

Thanks to you !
Do not hesitate if you need some help :wink: