Java 如何索引已使用lucene hibernate搜索创建的数据库

Java 如何索引已使用lucene hibernate搜索创建的数据库,java,hibernate,lucene,hibernate-search,Java,Hibernate,Lucene,Hibernate Search,我有一个包含现有数据的数据库,我想使用Lucene Hibernate对其进行索引。当我创建新数据时,Hibernate会对其进行索引,但问题是:如何对数据库中的所有旧数据进行索引 这是我的persistence.xml文件: <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"

我有一个包含现有数据的数据库,我想使用Lucene Hibernate对其进行索引。当我创建新数据时,Hibernate会对其进行索引,但问题是:如何对数据库中的所有旧数据进行索引

这是我的
persistence.xml
文件:

<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>

org.hibernate.jpa.HibernatePersistenceProvider

本手册的目的。它实际上没有太多关于底层索引的内容,并且您不能使用此文件创建DB索引。要创建索引,必须以管理员身份登录数据库服务器,并使用适当的命令创建索引

会议的目的。它实际上没有太多关于底层索引的内容,并且您不能使用此文件创建DB索引。要创建索引,必须以管理员身份登录数据库服务器,并使用适当的命令创建索引

简单的回答是索引是自动的:每次通过Hibernate ORM持久化、更新或删除每个实体时,Hibernate搜索都会透明地对其进行索引。它的任务是保持索引和数据库同步,让您忘记这个问题

但是,在现有应用程序中引入Hibernate搜索时,必须为数据库中已有的数据创建初始Lucene索引

添加上述属性和注释后,如果数据库中存在数据,则需要触发书籍的初始批索引。这将重建索引,以确保索引和数据库同步。您可以通过使用以下代码段之一实现这一点(另请参见重建整个索引):

使用Hibernate会话重建索引

FullTextSession fullTextSession = Search.getFullTextSession(session);
fullTextSession.createIndexer().startAndWait();
FullTextEntityManager fullTextEntityManager = 
Search.getFullTextEntityManager(entityManager);
fullTextEntityManager.createIndexer().startAndWait();
使用EntityManager(JPA)重建索引

FullTextSession fullTextSession = Search.getFullTextSession(session);
fullTextSession.createIndexer().startAndWait();
FullTextEntityManager fullTextEntityManager = 
Search.getFullTextEntityManager(entityManager);
fullTextEntityManager.createIndexer().startAndWait();
执行上述代码后,您应该能够在
/var/Lucene/index/example.Book
下看到Lucene索引

存储路径的根目录取决于配置步骤中指定的配置属性hibernate.search.default.indexBase


你现在可以和卢克一起检查这个索引了。它将帮助您了解Hibernate搜索的工作原理:Luke允许您检查索引内容和结构,与使用SQL控制台检查Hibernate ORM在关系数据库上的工作方式类似。

简单的回答是索引是自动的:每次通过Hibernate ORM对每个实体进行持久化、更新或删除时,Hibernate搜索都会透明地对其进行索引。它的任务是保持索引和数据库同步,让您忘记这个问题

但是,在现有应用程序中引入Hibernate搜索时,必须为数据库中已有的数据创建初始Lucene索引

添加上述属性和注释后,如果数据库中存在数据,则需要触发书籍的初始批索引。这将重建索引,以确保索引和数据库同步。您可以通过使用以下代码段之一实现这一点(另请参见重建整个索引):

使用Hibernate会话重建索引

FullTextSession fullTextSession = Search.getFullTextSession(session);
fullTextSession.createIndexer().startAndWait();
FullTextEntityManager fullTextEntityManager = 
Search.getFullTextEntityManager(entityManager);
fullTextEntityManager.createIndexer().startAndWait();
使用EntityManager(JPA)重建索引

FullTextSession fullTextSession = Search.getFullTextSession(session);
fullTextSession.createIndexer().startAndWait();
FullTextEntityManager fullTextEntityManager = 
Search.getFullTextEntityManager(entityManager);
fullTextEntityManager.createIndexer().startAndWait();
执行上述代码后,您应该能够在
/var/Lucene/index/example.Book
下看到Lucene索引

存储路径的根目录取决于配置步骤中指定的配置属性hibernate.search.default.indexBase


你现在可以和卢克一起检查这个索引了。它将帮助您了解Hibernate搜索的工作原理:Luke允许您检查索引内容和结构,类似于使用SQL控制台检查Hibernate ORM在关系数据库上的工作方式。

固定语法和代码格式固定语法和代码格式