Hibernate Search 中文操作指南
9. Main API Entry Points
本部分详细介绍了在运行时访问 Hibernate Search API 的主要入口点,即用于索引、搜索、查找元数据等的 API。
This section details the main entry points to Hibernate Search APIs at runtime, i.e. APIs to index, search, look up metadata, …
9.1. SearchMapping
9.1.1. Basics
_SearchMapping_是 Hibernate Search API 的最顶层入口:它表示从实体到索引的完整映射。
The SearchMapping is the top-most entrypoint to Hibernate Search APIs: it represents the whole mapping from entities to indexes.
SearchMapping_是线程安全的:可从多个线程安全并行使用。然而,这并不意味着它返回的对象(_SearchWorkspace,…)都是线程安全的。
The SearchMapping is thread-safe: it can safely be used concurrently from multiple threads. However, that does not mean the objects it returns (SearchWorkspace, …) are themselves thread-safe.
Hibernate Search 中的 SearchMapping 等同于 JPA/Hibernate ORM 中的 EntityManagerFactory/SessionFactory。 |
The SearchMapping in Hibernate Search is the equivalent of the EntityManagerFactory/SessionFactory in JPA/Hibernate ORM. |
一些框架(如 Quarkus )允许您简单地将 @Inject the SearchMapping 注入到您的 CDI bean 中。 |
Some frameworks, such as Quarkus, allow you to simply @Inject the SearchMapping into your CDI beans. |
9.1.2. Retrieving the SearchMapping with the Hibernate ORM integration
使用 Hibernate ORM integration,SearchMapping 将 created automatically when Hibernate ORM starts。
With the Hibernate ORM integration, the SearchMapping is created automatically when Hibernate ORM starts.
要检索_SearchMapping_,请调用_Search.mapping(…)并传递_EntityManagerFactory/SessionFactory:
To retrieve the SearchMapping, call Search.mapping(…) and pass the EntityManagerFactory/SessionFactory:
示例 8. 从 Hibernate ORM SessionFactory 检索 SearchMapping
. Example 8. Retrieving the SearchMapping from a Hibernate ORM SessionFactory
SessionFactory sessionFactory = /* ... */ (1)
SearchMapping searchMapping = Search.mapping( sessionFactory ); (2)
仍然使用 Hibernate ORM integration,可以从 JPA EntityManagerFactory 中执行相同操作:
Still with the Hibernate ORM integration, the same can be done from a JPA EntityManagerFactory:
示例 9. 从 JPA EntityManagerFactory 检索 SearchMapping
. Example 9. Retrieving the SearchMapping from a JPA EntityManagerFactory
EntityManagerFactory entityManagerFactory = /* ... */ (1)
SearchMapping searchMapping = Search.mapping( entityManagerFactory ); (2)
9.1.3. Retrieving the SearchMapping with the Standalone POJO Mapper
使用 Standalone POJO Mapper,SearchMapping 是启动 Hibernate Search 的结果。
With the Standalone POJO Mapper, the SearchMapping is the result of starting Hibernate Search.
请参阅 this section 以获取有关使用独立 POJO 映射器启动 Hibernate Search 的更多信息。
See this section for more information about starting Hibernate Search with the Standalone POJO Mapper.
9.2. SearchSession
9.2.1. Basics
_SearchSession_代表执行一系列相关操作的上下文。通常应该在很短的时间内使用它,例如处理单个 Web 请求。
The SearchSession represents the context in which a sequence of related operations are executed. It should generally be used for a very short time, for example to process a single web request.
SearchSession 不是线程安全的:它不能从多个线程同时使用。
The SearchSession is not thread-safe: it must not be used concurrently from multiple threads.
Hibernate Search 中的 SearchSession 等同于 JPA/Hibernate ORM 中的 EntityManager/Session。 |
The SearchSession in Hibernate Search is the equivalent of the EntityManager/Session in JPA/Hibernate ORM. |
一些框架(如 Quarkus )允许您简单地将 @Inject the SearchSession 注入到您的 CDI bean 中。 |
Some frameworks, such as Quarkus, allow you to simply @Inject the SearchSession into your CDI beans. |
9.2.2. Retrieving the SearchSession with the Hibernate ORM integration
要使用 Hibernate ORM integration 检索 SearchSession,请调用 Search.session(…) 并传递 EntityManager/Session:
To retrieve the SearchSession with the Hibernate ORM integration, call Search.session(…) and pass the EntityManager/Session:
示例 10. 从 Hibernate ORM Session 检索 SearchSession
. Example 10. Retrieving the SearchSession from a Hibernate ORM Session
Session session = /* ... */ (1)
SearchSession searchSession = Search.session( session ); (2)
仍然使用 Hibernate ORM integration,可以从 JPA EntityManager 中执行相同操作:
Still with the Hibernate ORM integration, the same can be done from a JPA EntityManager:
示例 11. 从 JPA EntityManager 检索 SearchSession
. Example 11. Retrieving the SearchSession from a JPA EntityManager
EntityManager entityManager = /* ... */ (1)
SearchSession searchSession = Search.session( entityManager ); (2)
9.2.3. Retrieving the SearchSession with the Standalone POJO Mapper
使用 Standalone POJO Mapper,SearchSession 应明确创建并关闭:
With the Standalone POJO Mapper, the SearchSession should be created and closed explicitly:
示例 12. 创建 SearchSession
. Example 12. Creating the SearchSession
SearchMapping searchMapping = /* ... */ (1)
try ( SearchSession searchSession = searchMapping.createSession() ) { (2)
// ...
}
忘记关闭 SearchSession 会导致不执行索引操作,甚至可能造成内存泄漏。
Forgetting to close the SearchSession will lead to indexing not being executed, and may even cause memory leaks.
SearchSession 也可以用几个选项进行配置:
The SearchSession can also be configured with a few options:
示例 13. 使用选项创建 SearchSession
. Example 13. Creating the SearchSession with options
SearchMapping searchMapping = /* ... */ (1)
Object tenantId = "myTenant";
try ( SearchSession searchSession = searchMapping.createSessionWithOptions() (2)
.indexingPlanSynchronizationStrategy( IndexingPlanSynchronizationStrategy.sync() )(3)
.tenantId( tenantId )
.build() ) { (4)
// ...
}
9.3. SearchScope
_SearchScope_代表一组已编制索引的实体及其索引。
The SearchScope represents a set of indexed entities and their indexes.
SearchScope_是线程安全的:可从多个线程安全并行使用。然而,这并不意味着它返回的对象(_SearchWorkspace,…)都是线程安全的。
The SearchScope is thread-safe: it can safely be used concurrently from multiple threads. However, that does not mean the objects it returns (SearchWorkspace, …) are themselves thread-safe.
可以从 SearchMapping 和 SearchSession 中检索 SearchScope 。
A SearchScope can be retrieved from a SearchMapping as well as from a SearchSession.
示例 14. 从 SearchMapping 检索 SearchScope
. Example 14. Retrieving a SearchScope from a SearchMapping
SearchMapping searchMapping = /* ... */ (1)
SearchScope<Book> bookScope = searchMapping.scope( Book.class ); (2)
SearchScope<Person> associateAndManagerScope = searchMapping.scope( Arrays.asList( Associate.class, Manager.class ) ); (3)
SearchScope<Person> personScope = searchMapping.scope( Person.class ); (4)
SearchScope<Person> personSubTypesScope = searchMapping.scope( Person.class,
Arrays.asList( "Manager", "Associate" ) ); (5)
SearchScope<Object> allScope = searchMapping.scope( Object.class ); (6)
示例 15. 从 SearchSession 检索 SearchScope
. Example 15. Retrieving a SearchScope from a SearchSession
SearchSession searchSession = /* ... */ (1)
SearchScope<Book> bookScope = searchSession.scope( Book.class ); (2)
SearchScope<Person> associateAndManagerScope =
searchSession.scope( Arrays.asList( Associate.class, Manager.class ) ); (3)
SearchScope<Person> personScope = searchSession.scope( Person.class ); (4)
SearchScope<Person> personSubTypesScope = searchSession.scope( Person.class,
Arrays.asList( "Manager", "Associate" ) ); (5)
SearchScope<Object> allScope = searchSession.scope( Object.class ); (6)