无法在lucene中搜索实体

时间:2013-11-05 13:14:54

标签: java hibernate date lucene hibernate-search

我正在尝试为搜索方法添加时间限制,这意味着按日期搜索。我知道lucene只能处理字符串,但我首先将日期转换为字符串。但它仍然没有工作,由于代码库的复杂性,我不太清楚为什么它不起作用。这是一个简单的版本:

@Indexed
public class SomeDocument extends Document{
}

public abstract class Document extends BaseEntity{
}

@Indexed
public class BaseEntity {

@IndexedEmbedded
private Date lastUpdatedDate;
}

//snippet of search method
BooleanQuery bq = new BooleanQuery();    
long oneDay = 1000L * 60 * 60 * 24;
long currentTime = System.currentTimeMillis();
Date dateOne = new Date(currentTime);
Date dateTwo = new Date(currentTime - (oneDay * 7)); // 1 week ago ago    
TermRangeQuery dateQuery = new TermRangeQuery("lastUpdatedDate", dateTwo.toString(),    dateOne.toString(), true, true);
bq.add(new BooleanClause(dateQuery, BooleanClause.Occur.MUST));

//more is added to boolean query, I create a full text query, and use the list() method

有没有人看到错误实施的地方?谢谢!

2 个答案:

答案 0 :(得分:1)

从您给出的代码片段中我猜测currentTime.toString()和dateTwo.toString()的格式不同,第一个是epoch以来的毫秒数,第二个是“{{3 “在Lucene范围查询中可能没有意义的格式。

至于数字,Lucene可以将它们编入索引。请参阅dow mon dd hh:mm:ss zzz yyyyLongField

答案 1 :(得分:1)

您应该使用Lucene的DateTools.DateToString,而不是使用Date.toString()来生成日期字符串。 Date.toString在"中生成日期YYYY-MM-DD"格式,而Lucene的DateTools格式日期为" yyyyMMddHHmmssSSS"格式,更适合用典型分析仪有效查询。类似的东西:

String dateOneString = DateTools.dateToString(dateOne, DateTools.Resolution.MILLISECOND);
String dateTwoString = DateTools.dateToString(dateTwo, DateTools.Resolution.MILLISECOND);
TermRangeQuery dateQuery = new TermRangeQuery("lastUpdateDate", dateTwoString,  dateOneString, true, true);

我认为默认日期解析为Resolution.MILLISECOND。可以使用@DateBridge annotation更改此内容。