如何在日期之间搜索(Hibernate Search)?

我想知道如何使用Range-Query在Hibernate Search中按日期搜索,或者我是否需要实现任何filter。以下是我在Record Entity中的字段

/** * When the analysis started. */ @Temporal(TemporalType.TIMESTAMP) @Field(index = Index.UN_TOKENIZED) @DateBridge(resolution = Resolution.MILLISECOND) private Date startTS; 

我的要求是找到两个日期之间分析的记录,例如。 11/11/2011至11/11/2012。我很困惑如何做到这一点。

您应该使用fromto来使用范围查询。

 query = monthQb .range() .onField( "startTS" ).ignoreFieldBridge() .from( DateTools.dateToString( from, DateTools.Resolution.MILLISECOND ) ) .to( DateTools.dateToString( to, DateTools.Resolution.MILLISECOND ) ).excludeLimit() .createQuery(); 

由于您使用DateTools自己创建基于字符串的搜索字符串,因此需要ignoreFieldBridge

征服你有以下实体:

 @Entity @Indexed public class Myth { @Field(analyze = Analyze.NO) @DateBridge(resolution = Resolution.YEAR) public Date getCreationDate() { return creationDate; } public Date setCreationDate(Date creationDate) { this.creationDate = creationDate; } private Date creationDate; ... } Date birthdate = ...; Query luceneQuery = mythQb.keyword().onField("creationDate").matching(birthdate).createQuery(); 

进行范围查询的方法是:

 //look for myths strictly BC Date beforeChrist = ...; Query luceneQuery = mythQB .range() .onField("creationDate") .below(beforeChrist).excludeLimit() .createQuery(); 

有关详细信息,请查看此参考