Java 无法在lucene中搜索实体

Java 无法在lucene中搜索实体,java,hibernate,date,lucene,hibernate-search,Java,Hibernate,Date,Lucene,Hibernate Search,我正在尝试为我的搜索方法添加时间限制,这意味着按日期搜索。我知道lucene只能处理字符串,但我先将日期转换为字符串。但它仍然不起作用,而且由于代码库的复杂性,我不太清楚它为什么不起作用。以下是一个简单的版本: @Indexed public class SomeDocument extends Document{ } public abstract class Document extends BaseEntity{ } @Indexed public class BaseEntity {

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

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

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


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

要生成日期字符串,应使用Lucene的
Date.toString
以“yyyy-mm-dd”格式生成日期,而Lucene的
DateTools
以“yyyyymmddhhmmssss”格式格式化日期,这更适合于使用典型分析器进行有效查询。比如:

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);

我相信默认的日期分辨率是
分辨率。毫秒
。这可以通过。

啊,对不起,我输入了一个小错误,我从来没有在RangeQuery中调用currentTime。所以我将RangeQuery改为Query dateRangeQuery=NumericRangeQuery.newLongRange(“LastUpdateDate”,dateTwo.getTime(),dateFour.getTime(),true,true);它仍然没有返回任何结果,因此我在lastUpdateDate中添加了@DateBridge(分辨率=分辨率.millis秒),并设置了TermRangeQuery以使用DateTools创建的字符串,但仍然没有返回任何结果。lastUpdateDate上的注释DateBridge返回“Mon Nov 04…”,而DateTools返回“20131104…”,您提供的代码中没有
lastUpdateDate
。DateBridge应该在索引中存储“20131106110300000”,而不是“Mon-Nov 04”。很抱歉,您是对的,我更新了代码,因此现在应该更有意义。但是,DateBridge仍然不是您显示的日期。它存储为2013-11-07 12:11:04.073