이 콘텐츠는 선택한 언어로 제공되지 않습니다.
Chapter 4. Querying embedded caches
Use embedded queries when you add Data Grid as a library to custom applications.
Protobuf mapping is not required with embedded queries. Indexing and querying are both done on top of Java objects.
4.1. Querying embedded caches 링크 복사링크가 클립보드에 복사되었습니다!
This section explains how to query an embedded cache using an example cache named "books" that stores indexed Book
instances.
In this example, each Book
instance defines which properties are indexed and specifies some advanced indexing options with Hibernate Search annotations as follows:
Book.java
Author.java
Procedure
Configure Data Grid to index the "books" cache and specify
org.infinispan.sample.Book
as the entity to index.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Obtain the cache.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Perform queries for fields in the
Book
instances that are stored in the Data Grid cache, as in the following example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.2. Entity mapping annotations 링크 복사링크가 클립보드에 복사되었습니다!
Add annotations to your Java classes to map your entities to indexes.
Hibernate Search API
Data Grid uses the Hibernate Search API to define fine grained configuration for indexing at entity level. This configuration includes which fields are annotated, which analyzers should be used, how to map nested objects, and so on.
The following sections provide information that applies to entity mapping annotations for use with Data Grid.
For complete detail about these annotations, you should refer to the Hibernate Search manual.
@DocumentId
Unlike Hibernate Search, using @DocumentId
to mark a field as identifier does not apply to Data Grid values; in Data Grid the identifier for all @Indexed
objects is the key used to store the value. You can still customize how the key is indexed using a combination of @Transformable
, custom types and custom FieldBridge
implementations.
@Transformable keys
The key for each value needs to be indexed as well, and the key instance must be transformed in a String
. Data Grid includes some default transformation routines to encode common primitives, but to use a custom key you must provide an implementation of org.infinispan.query.Transformer
.
Registering a key Transformer via annotations
You can annotate your key class with org.infinispan.query.Transformable
and your custom transformer implementation will be picked up automatically:
Registering a key Transformer via the cache indexing configuration
Use the key-transformers
xml element in both embedded and server config:
Alternatively, use the Java configuration API (embedded mode):
ConfigurationBuilder builder = ... builder.indexing().enable() .addKeyTransformer(CustomKey.class, CustomTransformer.class);
ConfigurationBuilder builder = ...
builder.indexing().enable()
.addKeyTransformer(CustomKey.class, CustomTransformer.class);