Search code examples
elasticsearchhibernate-search

Hibernate Search fallback when Elastic search not available


Is there a fallback mechanism in Hibernate Search in order to be able to perform a search even when Elastic search cluster is not available? I know that the indexes are stored in Elastic search but can Hibernate search in this case just ignore the indexes and perform a default search? Is there a way to set this up?


Solution

  • No. If Hibernate Search uses an Elasticsearch backend, it uses only that backend. If Elasticsearch is down, Hibernate Search is down.

    If you want a "default search" (whatever that means), you will need to implement it in your own code, e.g.:

    if ( elasticsearch available ) {
       // execute a query using Hibernate Search
    }
    else {
       // implement some "default" search, whatever that means
    }
    

    As to how you determine whether Elasticsearch is available, you could send a request to Elasticsearch using the underlying Rest client, or alternatively just run schema validation (which will necessarily fail if Elasticsearch is not available).

    Similar (but not identical) question: https://discourse.hibernate.org/t/how-to-know-if-index-already-exist-in-elastic-server/6207

    EDIT: Perhaps a better approach would be to try a normal search, catch exceptions and then fall back, e.g.:

    try {
    
    }
    catch (SearchException e) {
       if ( e.getMessage().contains("connect") ) {
           // implement some "default" search, whatever that means
       }
       else {
          throw e;
       }
    }