Lucene & Hibernate & 连接表

lucene & hibernate & joining tables

我们将 Lucene 与 HibernateSearch 结合使用,目前我们只查询一个实体的属性。我们想要的是能够查询相关实体的属性。
例如:

用户

id
姓名
... group_id

群组

id
姓名
类型

所以也可以输入组的type/name,这样就可以找到属于该组的用户。
我找到了以下页面,但是 (http://blog.mikemccandless.com/2012/01/searching-relational-content-with.html) 它没有使用 Hibernate,我在某处读到无法在 Lucene 中使用 Hibernate 加入实体。

能否请您告诉我如何使用 Hibernate Search 在 Lucene 中实现这一点?

----------------------------更新------------ ------------------
我忘了提到我们不使用注释,而是使用 .hbm.xml 文件。我们还使用 IndexedMapping class 添加要索引的实体的属性。例如
indexedMapping.property(field.getName(), ElementType.FIELD);

如果我没理解错的话,您正在寻找一种方法来索引组实体的属性以及用户实体的属性。

也许 @IndexedEmbedded 就是您要找的:https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/?v=5.5#search-mapping-associated

您必须在 group 字段中添加注释:

@Entity
@Indexed
public class User {
    @Id
    @GeneratedValue
    private Long id;

    @Field
    private String name;

    @ManyToOne
    @IndexedEmbedded
    private Group group;
    ....
}

然后组的所有索引字段将被添加到用户文档中,前缀为"group."。例如 "group.name" 将匹配用户组的名称。


EDIT :如果使用程序化 API,则必须改为调用 .indexedEmbedded()

indexedMapping.property(field.getName(), ElementType.FIELD)
    .indexedEmbedded();

https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/?v=5.5#_programmatically_defining_embedded_entities