Can we create Lucene indexes only once at for init

2019-09-14 21:02发布

问题:

I am an newbie in Hibernate Search. According to Documentation

By default, every time an object is inserted, updated or deleted through Hibernate, Hibernate Search updates the according Lucene index.

As I learn till now I come to know that we can build the lucene index through programmatically like this :( correct me if I am wrong)

FullTextSession fullTextSession = Search.getFullTextSession(session);
fullTextSession.createIndexer().startAndWait();

But what makes me surprise if is there is any way (and possible) to create indexes at initial set up only for once only.

回答1:

If you start your application with an empty database (initially) there is no need to build the index. When you restart the application, the index is (by default) stored on filesystem so there is no need to rebuild the index.

This operation is meant to be used only when you lost the index (disk crash?), when you update the Hibernate Search or Lucene versions, when you restore a different database backup, or when you change the mapping of your entities: it can't detect automatically when it should run, so you're supposed to invoke the method explicitly.

The MassIndexer is also available as a JMX operation, which makes it very easy to invoke by an administrator without writing code, or in some cases I've simply created an "admin panel" in my web applications for people to operate when needed.