Search code examples
javaspring-boothibernatelucenehibernate-search

How do I reload the index before searching in Hibernate Lucene


Let's say, I have one Lucene Index that is being used by two applications. For simplicity I will call them "ReadApp" and "WriteApp". Only WriteApp can write changes to the Lucene-Index.

My problem is that while both applications run, the ReadApp can't see the changes made to the Index by the WriteApp only after a restart.

Is there a way to configure the ReadApp to reload the Index before every read?

Let's not take in consideration the performance of such an endeavor :)

I am using the Lucene Backend (https://hibernate.org/search/releases/7.0/)

Configuration of ReadApp:

spring:
  jpa:
    properties:
      hibernate:
        search:
          indexing:
            plan.synchronization.strategy: sync
            listeners.enabled: true
          backend:
            directory.locking.strategy: none

Solution

  • First, this is dangerous, since Hibernate Search has no read-only mode. Any accidental write will potential corrupt your indexes. You should consider working with the Hibernate team on adding such read-only mode. I think someone asked about that recently, but I can't get my hands on that conversation... Anyway, you're not alone wanting such a feature.

    If you're extra sure your second app won't write to the indexes...

    Is there a way to configure the ReadApp to reload the Index before every read?

    What you're after is a refresh. Refreshing before every search is actually what Hibernate Search does by default... except it assumes it's the only one writing to the index, and if it didn't write to the index, it just skips the refresh.

    (Now that's where a read-only mode in Hibernate Search would come in handy: Hibernate Search could infer that if it's not writing, someone else is, and thus this optimization should be disabled)

    One way to work around that limitation would be to use the "debug" IO strategy:

    hibernate.search.backend.io.strategy=debug
    

    Or with Spring:

    spring:
      jpa:
        properties:
          hibernate:
            search:
              backend:
                io.strategy: debug
    

    I won't talk about performance since you don't want to, but... We should be talking about performance :) Don't use that strategy on your writing node, or you'll regret it.