在我们的域模型中,我们非常积极地使用聚合,即我们不通过jpa-relations连接所有类,而是使用我们可以查询相关对象的服务。所以,假设我们有两个示例类,Person和Workplace,它们通过引用而不是直接的对象关系相关联。
public class Person {
int id;
String name;
}
public class Workplace {
int id;
String name;
List<int> personId;
}
现在我们想构建一个Hibernate Search索引,该索引应该从Person和Workplace索引字段。这是否可以通过Hibernate Search实现,还是我们必须自己动手操作我们自己的Lucene-indexer并处理Hibernate Search对索引执行的所有维护?
我们应该考虑其他解决方案吗?
答案 0 :(得分:3)
使用Hibernate Search,您可以轻松地创建包含两者的索引,或者您可以为每个实体创建一个索引,但查询它们只是一个索引。
@Indexed @Entity
public class Person {
int id;
@Field
String name;
}
@Indexed @Entity
public class Workplace {
int id;
@Field
String name;
List<int> personId;
}
然后,您可以使用此索引查找匹配的Persons和/或Workplace实例。
org.apache.lucene.search.Query q = ...[a standard Lucene query]
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( q, Person.class );
List<Person> list = fullTextQuery.list();
或针对这两种类型:
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( q, Person.class, Workspace.class );
List list = fullTextQuery.list();
或只是所有索引类型:
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( q );
List list = fullTextQuery.list();