将 QueryDSL 与 Hibernate Search 和 Lucene 相结合
Combining QueryDSL with Hibernate Search and Lucene
我使用 Apache Lucene 配置了 Hibernate Search,以便更轻松地通过给定坐标搜索实体。现在我想知道如何向搜索实体添加更多条件。我也在使用 Spring 数据项目和查询 DSL。我正在尝试使用 QueryDSL 中的 Predicate
class 来保持我的应用程序的一致性。有没有可能做到这一点?
我的Place.class
@Getter
@Setter
@Entity
@Indexed
@Spatial
public class Place implements Serializable {
private static final long serialVersionUID = -8379536848917838560L;
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "place_id")
private Long id;
//...
@Longitude
private Double lng;
@Latitude
private Double lat;
//...
}
我的存储库class
@PersistenceContext(type = PersistenceContextType.EXTENDED)
EntityManager em;
@Override
@Transactional
public List findAll(Coordinates coordinates, Predicate predicate, Pageable pageable, int maxDistance) {
FullTextEntityManager fullTextSession = Search.getFullTextEntityManager(em);
QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Place.class).get();
org.apache.lucene.search.Query luceneQuery = builder
.spatial()
.within(maxDistance, Unit.KM)
.ofCoordinates(coordinates)
.createQuery();
FullTextQuery jpaQuery = fullTextSession.createFullTextQuery(luceneQuery, Place.class);
return jpaQuery.getResultList();
}
我假设您不是在询问有关将多个全文查询与布尔查询相结合的问题——我希望文档中有足够的相关示例——但您是在询问有关添加 SQL基于限制。
在实践中,可以通过应用 Criteria
进一步限制 FullTextQuery
,但是虽然这似乎有效,但此功能并不是故意的(这背后的有趣故事!),效果很差经过测试并且根本没有效率:尽可能避免。它没有被删除,因为有些人真的很喜欢它。
您可以在示例“5.12。在查询中指定 FetchMode”中找到示例和有关限制的警告:
- http://docs.jboss.org/hibernate/search/5.6/reference/en-US/html_single/
更好的解决方案是 运行 通过仅限制索引字段来进行全文查询,这样查询就可以完全由 Lucene 解析。
有时它有助于编码索引中的附加字段,不知何故 "pre-tagging" 您要匹配的 Lucene 文档。这是人们需要发挥创意的地方,您可以编写一些自定义 FieldBridge
和 ClassBridge
实现。
然后应用 Full-Text Filter,或将多个查询与布尔查询组合:参见“5.1.2.8. 组合查询”部分。
我使用 Apache Lucene 配置了 Hibernate Search,以便更轻松地通过给定坐标搜索实体。现在我想知道如何向搜索实体添加更多条件。我也在使用 Spring 数据项目和查询 DSL。我正在尝试使用 QueryDSL 中的 Predicate
class 来保持我的应用程序的一致性。有没有可能做到这一点?
我的Place.class
@Getter
@Setter
@Entity
@Indexed
@Spatial
public class Place implements Serializable {
private static final long serialVersionUID = -8379536848917838560L;
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "place_id")
private Long id;
//...
@Longitude
private Double lng;
@Latitude
private Double lat;
//...
}
我的存储库class
@PersistenceContext(type = PersistenceContextType.EXTENDED)
EntityManager em;
@Override
@Transactional
public List findAll(Coordinates coordinates, Predicate predicate, Pageable pageable, int maxDistance) {
FullTextEntityManager fullTextSession = Search.getFullTextEntityManager(em);
QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Place.class).get();
org.apache.lucene.search.Query luceneQuery = builder
.spatial()
.within(maxDistance, Unit.KM)
.ofCoordinates(coordinates)
.createQuery();
FullTextQuery jpaQuery = fullTextSession.createFullTextQuery(luceneQuery, Place.class);
return jpaQuery.getResultList();
}
我假设您不是在询问有关将多个全文查询与布尔查询相结合的问题——我希望文档中有足够的相关示例——但您是在询问有关添加 SQL基于限制。
在实践中,可以通过应用 Criteria
进一步限制 FullTextQuery
,但是虽然这似乎有效,但此功能并不是故意的(这背后的有趣故事!),效果很差经过测试并且根本没有效率:尽可能避免。它没有被删除,因为有些人真的很喜欢它。
您可以在示例“5.12。在查询中指定 FetchMode”中找到示例和有关限制的警告: - http://docs.jboss.org/hibernate/search/5.6/reference/en-US/html_single/
更好的解决方案是 运行 通过仅限制索引字段来进行全文查询,这样查询就可以完全由 Lucene 解析。
有时它有助于编码索引中的附加字段,不知何故 "pre-tagging" 您要匹配的 Lucene 文档。这是人们需要发挥创意的地方,您可以编写一些自定义 FieldBridge
和 ClassBridge
实现。
然后应用 Full-Text Filter,或将多个查询与布尔查询组合:参见“5.1.2.8. 组合查询”部分。