1

我正在尝试向我的搜索方法添加时间限制,这意味着按日期搜索。我知道 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

有没有人看到一个实施不正确的地方?谢谢!

4

2 回答 2

1

而不是使用Date.toString(), 来生成日期字符串,您应该使用 Lucene 的DateTools.DateToString. 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 注释进行更改。

于 2013-11-05T16:58:37.620 回答
1

从您给出的代码片段中,我猜 currentTime.toString() 和 dateTwo.toString() 的格式不同,第一个是自纪元以来的毫秒数,第二个是“ dow mon dd hh:mm :ss zzz yyyy " 格式在 Lucene 范围查询中可能没有意义。

至于数字,Lucene 可以很好地索引它们。请参阅LongFieldNumericRangeQuery

于 2013-11-05T13:25:48.433 回答