第4章 組み込みキャッシュのクエリー
データグリッドをライブラリーとしてカスタムアプリケーションに追加する場合は、組み込みクエリーを使用します。
組み込みクエリーでは、Protobuf マッピングは必要ありません。インデックス作成とクエリーは、どちらも Java オブジェクト上で実行されます。
4.1. 組み込みキャッシュのクエリー リンクのコピーリンクがクリップボードにコピーされました!
このセクションでは、インデックス化された Book インスタンスを保存する "books" という名前のサンプルキャッシュを使用して組み込みキャッシュをクエリーする方法を説明します。
この例では、各 Book インスタンスはインデックス化されるプロパティーを定義し、以下のように Hibernate Search アノテーションを使用して詳細なインデックスオプションを指定します。
Book.java
package org.infinispan.sample;
import java.time.LocalDate;
import java.util.HashSet;
import java.util.Set;
import org.infinispan.api.annotations.indexing.*;
// Annotate values with @Indexed to add them to indexes
// Annotate each field according to how you want to index it
@Indexed
public class Book {
@Keyword
String title;
@Text
String description;
@Keyword
String isbn;
@Basic
LocalDate publicationDate;
@Embedded
Set<Author> authors = new HashSet<Author>();
}
Author.java
package org.infinispan.sample;
import org.infinispan.api.annotations.indexing.Text;
public class Author {
@Text
String name;
@Text
String surname;
}
手順
"books" キャッシュをインデックス化するように Data Grid を設定し、
org.infinispan.sample.Bookをインデックスのエンティティーとして指定します。<distributed-cache name="books"> <indexing path="${user.home}/index"> <indexed-entities> <indexed-entity>org.infinispan.sample.Book</indexed-entity> </indexed-entities> </indexing> </distributed-cache>キャッシュを取得します。
import org.infinispan.Cache; import org.infinispan.manager.DefaultCacheManager; import org.infinispan.manager.EmbeddedCacheManager; EmbeddedCacheManager manager = new DefaultCacheManager("infinispan.xml"); Cache<String, Book> cache = manager.getCache("books");以下の例のように、Data Grid キャッシュに保存されている
Bookインスタンスでフィールドのクエリーを実行します。// Create an Ickle query that performs a full-text search using the ':' operator on the 'title' and 'authors.name' fields // You can perform full-text search only on indexed caches Query<Book> fullTextQuery = cache.query("FROM org.infinispan.sample.Book b WHERE b.title:'infinispan' AND b.authors.name:'sanne'"); // Use the '=' operator to query fields in caches that are indexed or not // Non full-text operators apply only to fields that are not analyzed Query<Book> exactMatchQuery= cache.query("FROM org.infinispan.sample.Book b WHERE b.isbn = '12345678' AND b.authors.name : 'sanne'"); // You can use full-text and non-full text operators in the same query Query<Book> query= cache.query("FROM org.infinispan.sample.Book b where b.authors.name : 'Stephen' and b.description : (+'dark' -'tower')"); // Get the results List<Book> found=query.execute().list();