gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:31:53 24 4
gpt4 key购买 nike

我有一个包含现有数据的数据库,我想使用 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>

最佳答案

简短的回答是索引是自动的:每次通过 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 在关系数据库上的工作情况。

关于java - 如何索引已使用 lucene hibernate 搜索创建的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46960118/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com