简体   繁体   中英

how to index a database already created using lucene hibernate search

I have a database with existing data that I want to index using Lucene Hibernate. When I create new data, Hibernate indexes it but the question is: how can I index all the old data in my database?

This is my persistence.xml file:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="persistenceUnit"
      transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <properties>
      <property name="hibernate.dialect" value="com.zodiac.qtp.domain.MySQL5CustomInnoDBDialect"/>
      <!-- value="create" to build a new database on each run; value="update"
      to modify an existing database; value="create-drop" means the same as "create"
      but also drops tables when Hibernate closes; value="validate" makes no changes
      to the database -->
      <property name="hibernate.format_sql" value="true"/>
      <property name="hibernate.hbm2ddl.auto" value="update" />
      <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
      <property name="hibernate.connection.charSet" value="UTF8" />
      <property name="hibernate.connection.characterEncoding" value="UTF8"/>
      <property name="hibernate.show_sql" value="false" />
      <property name="hibernate.cache.provider_class"  value="org.hibernate.cache.EhCacheProvider"/>
      <property name="hibernate.cache.use_second_level_cache" value="true" />
      <property name="hibernate.cache.use_query_cache" value="true" />
      <property name="hibernate.generate_statistics" value="false" />
      <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory" />
      <!-- Uncomment the following two properties for JBoss only -->
      <!-- property name="hibernate.validator.apply_to_ddl" value="false" / -->
      <!-- property name="hibernate.validator.autoregister_listeners" value="false" / -->
      <property name="hibernate.search.default.directory_provider" value="org.hibernate.search.store.impl.FSDirectoryProvider"/>
      <property name="hibernate.search.default.indexBase" value="C:\ZAM_DEV\QTPGenerator-repository\lucene-indexes-v2"/>
    </properties>
  </persistence-unit>
</persistence>

The short answer is that indexing is automatic: Hibernate Search will transparently index every entity each time it's persisted, updated or removed through Hibernate ORM. Its mission is to keep the index and your database in sync, allowing you to forget about this problem.

However, when introducing Hibernate Search in an existing application, you have to create an initial Lucene index for the data already present in your database.

Once you have added the above properties and annotations, if you have existing data in the database you will need to trigger an initial batch index of your books. This will rebuild your index to make sure your index and your database is in synch. You can achieve this by using one of the following code snippets (see also Rebuilding the whole index):

Using an Hibernate Session to rebuild an index

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

Using an EntityManager (JPA) to rebuild an index

FullTextEntityManager fullTextEntityManager = 
Search.getFullTextEntityManager(entityManager);
fullTextEntityManager.createIndexer().startAndWait();

After executing the above code, you should be able to see a Lucene index under /var/lucene/indexes/example.Book .

The root of the storage path depends on the configuration property hibernate.search.default.indexBase we specified in the configuration step.

You could now inspect this index with Luke. It will help you to understand how Hibernate Search works: Luke allows you to inspect the index contents and structure, similarly to how you would use a SQL console to inspect the working of Hibernate ORM on relational databases.

The purpose of the persistence.xml file is to access entities from your DB . It really doesn't say much about the underlying indices, and you can't create DB indices using this file. To create your indices, you must logon to your DB server as an admin and create the indices using the appropriate CREATE INDEX commands.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM