You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by lu...@nitwit.de on 2004/04/02 17:03:49 UTC

Simple date/range question

Hi!

I do have some problems with date and the QueryParser range syntax.

code:

java.sql.Timestamp time = row.getTimestamp("timestamp");
if (time != null) doc.add(Field.Keyword("date", new Date(time.getTime())));

query:
date:[20030101 TO 20030202]
date:20030101

The first query does throw a ParserException, the second doesn't return any 
hits.

Hmm...there must be something simple I misunderstood :) BTW what about custom 
date format in QueryParser (...and are the last two digits actually the day 
or month)?

TIA
Timo

---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-user-help@jakarta.apache.org


Re: Simple date/range question

Posted by lu...@nitwit.de.
On Saturday 03 April 2004 11:53, Erik Hatcher wrote:
> I didn't catch in your first message that it was throwing a
> ParseException.... this is odd.  Are you certain that "date:[20030101
> TO 20030202]" is the complete string your passing to QueryParser?  Did

Yes.

> you subclass QueryParser?  If so, what is that code?  (what is the

No.

I use a MultiFieldQueryParser:

Query qQuery = MultiFieldQueryParser.parse(query, new String[] { "id", 
"title", "summary", "contents", "date" }, GERMAN_ANALYZER);	
Hits hits = searcher.search(qQuery);

> complete stack trace?)

     [java] 12:38:03,109 ERROR [view.SearchAction] 
org.apache.lucene.queryParser.ParseException: Encountered "20030404" at line 
1, column 18.
     [java] Was expecting:
     [java] "]" ...
     [java] org.apache.lucene.queryParser.ParseException: Encountered 
"20030404" at line 1, column 18.
     [java] Was expecting:
     [java] "]" ...
     [java] at 
org.apache.lucene.queryParser.QueryParser.generateParseException(QueryParser.java:994)
     [java] at 
org.apache.lucene.queryParser.QueryParser.jj_consume_token(QueryParser.java:874)
     [java] at 
org.apache.lucene.queryParser.QueryParser.Term(QueryParser.java:657)
     [java] at 
org.apache.lucene.queryParser.QueryParser.Clause(QueryParser.java:521)
     [java] at 
org.apache.lucene.queryParser.QueryParser.Query(QueryParser.java:464)
     [java] at 
org.apache.lucene.queryParser.QueryParser.parse(QueryParser.java:108)
     [java] at 
org.apache.lucene.queryParser.QueryParser.parse(QueryParser.java:87)
     [java] at 
org.apache.lucene.queryParser.MultiFieldQueryParser.parse(MultiFieldQueryParser.java:115)

---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-user-help@jakarta.apache.org


Re: Simple date/range question

Posted by Erik Hatcher <er...@ehatchersolutions.com>.
On Apr 3, 2004, at 4:07 AM, lucene@nitwit.de wrote:
> On Friday 02 April 2004 17:03, lucene@nitwit.de wrote:
>> date:[20030101 TO 20030202]
>
>      [java] 11:05:53,735 ERROR [view.SearchAction]
> org.apache.lucene.queryParser.ParseException: Encountered "20030202" 
> at line
> 1, column 18.
>      [java] Was expecting:
>      [java] "]" ...
>
> Why is this?

I didn't catch in your first message that it was throwing a 
ParseException.... this is odd.  Are you certain that "date:[20030101 
TO 20030202]" is the complete string your passing to QueryParser?  Did 
you subclass QueryParser?  If so, what is that code?  (what is the 
complete stack trace?)

	Erik


---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-user-help@jakarta.apache.org


Re: Simple date/range question

Posted by lu...@nitwit.de.
On Friday 02 April 2004 17:03, lucene@nitwit.de wrote:
> date:[20030101 TO 20030202]

     [java] 11:05:53,735 ERROR [view.SearchAction] 
org.apache.lucene.queryParser.ParseException: Encountered "20030202" at line 
1, column 18.
     [java] Was expecting:
     [java] "]" ...

Why is this?

---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-user-help@jakarta.apache.org


Re: Simple date/range question

Posted by lu...@nitwit.de.
On Friday 02 April 2004 17:03, lucene@nitwit.de wrote:
> java.sql.Timestamp time = row.getTimestamp("timestamp");
> if (time != null) doc.add(Field.Keyword("date", new Date(time.getTime())));
>
> date:20030101

BTW Looking at the index using luke I found that date contains something that 
looks like a hash to me....is the  object stored in index or what?!

---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-user-help@jakarta.apache.org


Re: Simple date/range question

Posted by Erik Hatcher <er...@ehatchersolutions.com>.
Let me clarify what Otis meant as well as shed some light on the other 
questions in this thread.

On Apr 2, 2004, at 10:03 AM, lucene@nitwit.de wrote:
> if (time != null) doc.add(Field.Keyword("date", new 
> Date(time.getTime())));
>
> query:
> date:[20030101 TO 20030202]
> date:20030101
>
> The first query does throw a ParserException, the second doesn't 
> return any
> hits.

QueryParser uses DateFormat.SHORT to parse dates using the default 
locale.  20030101 does not parse using that format string.

If you are truly only representing dates (not times) then use, what 
Otis meant, YYYYMMDD *Strings* to represent the date.  If you need 
times as well, first be aware of what RangeQuery does (expand all terms 
in that range!) so that you aren't shocked with performance or a too 
many clauses exception.

When you use Field.Keyword(String, Date), the date is converted into a 
lexicographically ordered value (the ugly thing you saw in Luke).  This 
is why, if you only care about dates, YYYYMMDD is recommended (it must 
sort properly).

> Hmm...there must be something simple I misunderstood :) BTW what about 
> custom
> date format in QueryParser (...and are the last two digits actually 
> the day
> or month)?

Custom date format in QueryParser is quite do-able.  Subclass 
QueryParser and override getRangeQuery and do what you like there.

I do something similar in Lucene in Action.  I'd like users to search 
on ranges like lastmodified:[1/1/04 TO 12/31/04], but internally I 
represent dates as YYYYMMDD.  To make the conversion, I had to have a 
custom QueryParser....

/**
  * From Lucene in Action (Manning Publications)
  */
public class SmartDayQueryParser extends QueryParser {

 public static final DateFormat formatter =
     new SimpleDateFormat("yyyyMMdd");

 public SmartDayQueryParser(String field, Analyzer analyzer) {
   super(field, analyzer);
 }

 protected Query getRangeQuery(String field, Analyzer analyzer,
                               String part1, String part2,
                               boolean inclusive)
     throws ParseException {
   try {
     DateFormat df =
         DateFormat.getDateInstance(DateFormat.SHORT,
             getLocale());
     df.setLenient(true);
     Date d1 = df.parse(part1);
     Date d2 = df.parse(part2);
     part1 = formatter.format(d1);
     part2 = formatter.format(d2);
   } catch (Exception ignored) {
   }

   return new RangeQuery(new Term(field, part1),
       new Term(field, part2),
       inclusive);
 }
}


	Erik


---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-user-help@jakarta.apache.org


Re: Simple date/range question

Posted by lu...@nitwit.de.
On Friday 02 April 2004 18:59, Otis Gospodnetic wrote:
> You Timestamp contains HH mm, and ss, that's likely why your second

My timestamp contains date and time.

> query doesn't match anything.
> Drop everything other than YYYYMMDD from the index, and things should
> work.

What's wrong with new Date(timestamp)?

---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-user-help@jakarta.apache.org


Re: Simple date/range question

Posted by Otis Gospodnetic <ot...@yahoo.com>.
YYYYMMDD is the format
You Timestamp contains HH mm, and ss, that's likely why your second
query doesn't match anything.
Drop everything other than YYYYMMDD from the index, and things should
work.

Otis

--- lucene@nitwit.de wrote:
> Hi!
> 
> I do have some problems with date and the QueryParser range syntax.
> 
> code:
> 
> java.sql.Timestamp time = row.getTimestamp("timestamp");
> if (time != null) doc.add(Field.Keyword("date", new
> Date(time.getTime())));
> 
> query:
> date:[20030101 TO 20030202]
> date:20030101
> 
> The first query does throw a ParserException, the second doesn't
> return any 
> hits.
> 
> Hmm...there must be something simple I misunderstood :) BTW what
> about custom 
> date format in QueryParser (...and are the last two digits actually
> the day 
> or month)?
> 
> TIA
> Timo
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: lucene-user-help@jakarta.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-user-help@jakarta.apache.org