如何索引已使用lucene hibernate search 创建的数据库
how to index a database already created using 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"
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>
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
命令的目的。
简短的回答是索引是自动的:每次通过 Hibernate ORM 持久化、更新或删除实体时,Hibernate Search 都会透明地索引每个实体。它的任务是让索引和你的数据库保持同步,让你忘记这个问题。
但是,在现有应用程序中引入 Hibernate Search 时,您必须为数据库中已存在的数据创建初始 Lucene 索引。
添加上述属性和注释后,如果数据库中已有数据,则需要触发图书的初始批量索引。这将重建您的索引以确保您的索引和数据库同步。您可以使用以下代码片段之一实现此目的(另请参阅重建整个索引):
使用 Hibernate Session 重建索引
FullTextSession fullTextSession = Search.getFullTextSession(session);
fullTextSession.createIndexer().startAndWait();
使用 EntityManager (JPA) 重建索引
FullTextEntityManager fullTextEntityManager =
Search.getFullTextEntityManager(entityManager);
fullTextEntityManager.createIndexer().startAndWait();
执行上面的代码后,应该可以在/var/lucene/indexes/example.Book
下看到一个Lucene索引。
存储路径的根取决于我们在配置步骤中指定的配置属性 hibernate.search.default.indexBase。
您现在可以和 Luke 一起检查这个索引了。它将帮助您了解 Hibernate Search 的工作原理:Luke 允许您检查索引内容和结构,类似于您使用 SQL 控制台检查 Hibernate ORM 在关系数据库上的工作方式。
我有一个包含现有数据的数据库,我想使用 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>
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
命令的目的。
简短的回答是索引是自动的:每次通过 Hibernate ORM 持久化、更新或删除实体时,Hibernate Search 都会透明地索引每个实体。它的任务是让索引和你的数据库保持同步,让你忘记这个问题。
但是,在现有应用程序中引入 Hibernate Search 时,您必须为数据库中已存在的数据创建初始 Lucene 索引。
添加上述属性和注释后,如果数据库中已有数据,则需要触发图书的初始批量索引。这将重建您的索引以确保您的索引和数据库同步。您可以使用以下代码片段之一实现此目的(另请参阅重建整个索引):
使用 Hibernate Session 重建索引
FullTextSession fullTextSession = Search.getFullTextSession(session);
fullTextSession.createIndexer().startAndWait();
使用 EntityManager (JPA) 重建索引
FullTextEntityManager fullTextEntityManager =
Search.getFullTextEntityManager(entityManager);
fullTextEntityManager.createIndexer().startAndWait();
执行上面的代码后,应该可以在/var/lucene/indexes/example.Book
下看到一个Lucene索引。
存储路径的根取决于我们在配置步骤中指定的配置属性 hibernate.search.default.indexBase。
您现在可以和 Luke 一起检查这个索引了。它将帮助您了解 Hibernate Search 的工作原理:Luke 允许您检查索引内容和结构,类似于您使用 SQL 控制台检查 Hibernate ORM 在关系数据库上的工作方式。