Hibernate 搜索:Children 的存储列表并通过投影检索

Hibernate Search: Store List of Children and retrieve via projection

我有以下 2 个 类 带有休眠和休眠搜索注释:

Parent Class:

@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
}

Child Class:

@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;
}

我为上述场景创建了一个索引。现在我试图通过搜索给定的 parent 名称来获取所有 child 名称。

我在场上使用投影如下:

Query searchQuery = queryBuilder.keyword().onField("name").matching("test").createQuery();
FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(searchQuery, Parent.class);
fullTextQuery.setProjection("childList.name");

现在,当我尝试 运行 查询来检索搜索结果时,我只得到 parent 模型的第一个 child 的名称。

当我使用 Luke 查看索引时,我能够看到文档中的所有值。

如何获取索引中存储的所有 child 个名称的列表?

在此示例中,您有一个模型需要 "turning around"。

您的目标是查询列表 child 姓名,因此您需要搜索 Child 实体。

您仍然可以在每个 child 中包含 "parent's name",并限制您的查询。

@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");