1

I am trying to add time constraints to my search method which means searching by date. I know lucene can only handle strings, but I am converting the dates to string first. But it's still not working and due to the complexity of the code base I'm not quite sure why it's not working. Here's a simple version:

@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

Does anyone see a place that is implemented incorrectly? Thank you!

Sanne
  • 6,027
  • 19
  • 34
Kat
  • 475
  • 1
  • 6
  • 21

2 Answers2

1

From the code snippet you have given I'd guess the currentTime.toString() and dateTwo.toString() are in different formats, the first is the number of milliseconds since epoch and the second is in the "dow mon dd hh:mm:ss zzz yyyy" format which probably makes no sense in a Lucene range query.

As for the numbers, Lucene can index them just fine. See LongField and NumericRangeQuery.

ArtemGr
  • 11,684
  • 3
  • 52
  • 85
  • Ah, sorry I put in a slight typo, I never actually call currentTime in the TermRangeQuery. – Kat Nov 05 '13 at 14:38
  • So I changed the TermRangeQuery to Query dateRangeQuery = NumericRangeQuery.newLongRange("lastUpdatedDate", dateTwo.getTime(), dateFour.getTime(), true, true); and it's still not returning any results – Kat Nov 05 '13 at 19:40
1

Instead of using Date.toString(), to generate the date string, you should use Lucene's DateTools.DateToString. Date.toString generates a date in " yyyy-mm-dd" format, while Lucene's DateTools formats dates in "yyyyMMddHHmmssSSS" format, which is more suited to querying effectively with typical analyzers. Something like:

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

I believe the default date resolution is Resolution.MILLISECOND. This can be changed with a @DateBridge annotation.

femtoRgon
  • 32,893
  • 7
  • 60
  • 87
  • So I added @DateBridge(resolution = Resolution.MILLISECOND) to lastUpdatedDate and set up the TermRangeQuery to use strings created by DateTools and still nothing. – Kat Nov 05 '13 at 19:48
  • The annotation DateBridge on lastUpdateDate is return "Mon Nov 04..." while DateTools is returning "20131104.." – Kat Nov 05 '13 at 20:08
  • Kat, there's no `lastUpdateDate` in the code you provided. DateBridge should store "20131106110300000" in the index, not "Mon Nov 04". – ArtemGr Nov 06 '13 at 07:05
  • Sorry, you are correct, I updated the code so it should make more sense now. – Kat Nov 06 '13 at 21:59
  • However, DateBridge still isn't the date as you showed.It is stored as 2013-11-07 12:11:04.073 – Kat Nov 07 '13 at 17:12
  • From your updated code I'd think you forgot to use the `@DateBridge`: should be `@IndexedEmbedded @DateBridge(resolution = Resolution.MILLISECOND) private Date lastUpdatedDate;`, not just `@IndexedEmbedded private Date lastUpdatedDate;`. – ArtemGr Nov 09 '13 at 10:17
  • @ArtemGr So I do have both annotations but it is still not working. However, for the annotation I had to use hibernate.search.annotations.Resolution, not DateTools.Resolution because it was throwing errors. I'm guessing this makes a difference? – Kat Nov 12 '13 at 17:18
  • I dunno, I think it shouldn't make a difference. You should find a way to debug your program. With Hibernate logging, for example (http://stackoverflow.com/q/1710476/257568). – ArtemGr Nov 12 '13 at 17:50