假设我有以下类(class)。 (只包括最重要的东西)
public class Client {
/* Some Properties */
}
public class ClientDocumentAssociation {
@ManyToOne
private Client client;
/* Some Properties */
}
@Indexed
public class Document {
@OneToOne
private ClientDocumentAssociation clientAssociation;
@Field(name = "text")
private String text;
/* Some Properties */
}
我的基本文档搜索是这样的:
public List<AbstractDocument> searchDocuments(String text) {
if (text == null) {
return newArrayList();
}
FullTextEntityManager ftem = Search.getFullTextEntityManager(entityManagerProvider.get());
MultiFieldQueryParser parser = new MultiFieldQueryParser(DOCUMENT_FIELDS, new StandardAnalyzer());
parser.setDefaultOperator(Operator.AND);
FullTextQuery ftq;
try {
Query q = parser.parse(text + "*");
ftq = ftem.createFullTextQuery(q, Document.class);
ftq.setMaxResults(20);
List<AbstractDocument> results = ftq.getResultList();
return results;
} catch (ParseException e) {
e.printStackTrace();
}
return newArrayList();
}
现在,我希望能够搜索文档,但不是在整个索引范围内,而是只查找属于给定客户端的文档。我唯一想到的是将关联添加到索引并将客户端 ID 添加到搜索中的适当字段。但这似乎不对。必须有另一种选择,这就是我所要求的。
最佳答案
为什么你最初的想法似乎是错误的?事实上,为搜索所需的所有数据建立索引是推荐的做法。这就是 @IndexedEmbedded 的用途。索引数据还可以让您更灵活地更改查询和/或新查询。
关于java - Hibernate Search - 在给定范围内搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2295715/