【问题标题】:index unrelated entities in same index with hibernate search使用休眠搜索在同一索引中索引不相关的实体
【发布时间】:2013-08-13 09:12:24
【问题描述】:

在我们的领域模型中,我们非常积极地使用聚合,即我们不通过 jpa 关系连接所有类,而是使用我们可以查询相关对象的服务。因此,假设我们有两个示例类,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 自己对索引执行的所有维护?

还有其他我们应该考虑的解决方案吗?

【问题讨论】:

    标签: hibernate lucene domain-driven-design hibernate-search


    【解决方案1】:

    使用 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;
    }
    

    然后您可以使用此索引来查找匹配的人员和/或 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();
    

    【讨论】:

    • 我试过这个,它有效。但问题是结果没有正确排序。 All Persion 列表,然后列出所有 Workplaces。
    猜你喜欢
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多