Copy to ClipboardCopied!Toggle word wrapToggle overflow
每个 Book 将按照以下示例定义;我们必须选择索引哪些属性;对于每个属性,我们可以选择使用 Hibernate Search 项目中定义的注释选择高级索引选项。
Book.java
package org.infinispan.sample;
import java.time.LocalDate;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.*;
// Annotate values with @Indexed to add them to indexes
// Annotate each fields according to how you want to index it
@Indexed
public class Book {
@FullTextField
String title;
@FullTextField
String description;
@KeywordField
String isbn;
@GenericField
LocalDate publicationDate;
@IndexedEmbedded
Set<Author> authors = new HashSet<Author>();
}
package org.infinispan.sample;
import java.time.LocalDate;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.*;
// Annotate values with @Indexed to add them to indexes
// Annotate each fields according to how you want to index it
@Indexed
public class Book {
@FullTextField
String title;
@FullTextField
String description;
@KeywordField
String isbn;
@GenericField
LocalDate publicationDate;
@IndexedEmbedded
Set<Author> authors = new HashSet<Author>();
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Author.java
package org.infinispan.sample;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.FullTextField;
public class Author {
@FullTextField
String name;
@FullTextField
String surname;
}
package org.infinispan.sample;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.FullTextField;
public class Author {
@FullTextField
String name;
@FullTextField
String surname;
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
假设我们在我们的 Data Grid Cache 中存储了多个 Book 实例,我们可以根据以下示例所示搜索任何匹配字段。
QueryExample.java
// Get the query factory from the cache
QueryFactory queryFactory = org.infinispan.query.Search.getQueryFactory(cache);
// 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 = queryFactory.create("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=queryFactory.create("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=queryFactory.create("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();
// Get the query factory from the cache
QueryFactory queryFactory = org.infinispan.query.Search.getQueryFactory(cache);
// 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 = queryFactory.create("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=queryFactory.create("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=queryFactory.create("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();
Copy to ClipboardCopied!Toggle word wrapToggle overflow