我有以下2个类,包括hibernate和hibernate搜索注释:
家长班:
@Indexed
@Entity
public class Parent {
@Id
@Column(name="parent_id")
private Long id;
@Field(store = Store.YES)
private String name;
@IndexedEmbedded
@OneToMany(fetch = FetchType.LAZY, mappedBy = "parent", targetEntity = Child.class)
private List<Child> childList;
//getters and setters
}
儿童班:
@Entity
public class Child {
@Id
private Long id;
@ManyToOne(targetEntity = Parent.class)
@ContainedIn
@JoinColumn(name = "parent_id")
private Parent parent;
@Field(store = Store.YES)
private String name;
}
我为上面的场景创建了一个索引。现在我试图通过搜索给定的父名来获取所有子名称。
我在场上使用投影如下:
Query searchQuery = queryBuilder.keyword().onField("name").matching("test").createQuery();
FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(searchQuery, Parent.class);
fullTextQuery.setProjection("childList.name");
现在,当我尝试运行查询以检索搜索结果时,我只得到父模型的第一个孩子的名字。
当我使用Luke查看索引时,我能够看到文档中的所有值。
如何获取索引中存储的所有子名称列表?
答案 0 :(得分:1)
在这个例子中,你有一个需要“转身”的模型。
您的目标是查询列表子名称,以便您需要搜索Child
实体。
您仍然可以在每个孩子中加入“父母的姓名”,并限制您的查询。
@Entity @Indexed
public class Child {
...
Query searchQuery = queryBuilder.keyword().onField("parent.name").matching("test").createQuery();
FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(searchQuery, Child.class);
fullTextQuery.setProjection("name");