Querying for a date range using Lucene.Net.Query

1,455 views
Skip to first unread message

Andreas Wimmer

unread,
Sep 4, 2013, 10:30:24 AM9/4/13
to rav...@googlegroups.com
Hi

When I need to build my own Lucene.Net.Query as a parameter to query RavenDB for a date range, something like:
var ravenQuery = session.Advanced.LuceneQuery<DocumentType>("IndexName")
 .SelectFields<int>("SomeField")
 .Where(luceneQuery);

How would you do this? Use a TermRangeQuery with your DateTools to create the date as string? As you save dates in some ISO8601-like format I can't take NumericRangeQuery. Or use a QueryParser and pass a string like "CreationDate:[* TO 20130904]"? Something else? What would you consider to be best practice? Primarily regarding performance/costs.

Thanks
Andy

Matt Johnson

unread,
Sep 4, 2013, 10:42:05 AM9/4/13
to rav...@googlegroups.com
Try not to build Lucene queries yourself unless you absolutely have to.  You can do something like this:

    .WhereLessThanOrEqual(x => x.YourDateTime, new DateTime(2013, 09, 04));

Do understand thought that the DateTime in this case would have an Unspecified kind.  If your dates are stored as UTC, then you would do this instead:

    .WhereLessThanOrEqual(x => x.DateTime, new DateTime(2013, 09, 04, 0, 0, 0, DateTimeKind.Utc));

See also:  http://ravendb.net/kb/61/working-with-date-and-time-in-ravendb

Andreas Wimmer

unread,
Sep 4, 2013, 10:57:03 AM9/4/13
to rav...@googlegroups.com
I'm afraid, I think I have no other choice.
We provide our customers sort of graphical query builder and save that in an expression tree. So the query is very dynamic and I'm using BooleanQuery wrapped around other type of queries.

So how can I query for a date range? Or is there any alternative?

Oren Eini (Ayende Rahien)

unread,
Sep 4, 2013, 11:01:00 AM9/4/13
to ravendb
You need to get the actual text and use that, htne.


--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Andreas Wimmer

unread,
Sep 4, 2013, 11:21:40 AM9/4/13
to rav...@googlegroups.com
Thanks for your answer Oren, unfortunately it didn't help me much. "Actual text"...of the date? And what does "htne" mean?
Could you provide a short example?

Oren Eini (Ayende Rahien)

unread,
Sep 4, 2013, 2:33:36 PM9/4/13
to ravendb
The actual text of the query, and use that, "then".
If you are constructing a query dynamically, you have the Query object, then call ToString on that.

Matt Johnson

unread,
Sep 4, 2013, 3:14:12 PM9/4/13
to rav...@googlegroups.com
The underlying query syntax would be something like this:

CreationDate:[* TO 2013-09-04T00:00:00.0000000]

or if you use UTC dates in your data (you should), then it is this:

CreationDate:[* TO 2013-09-04T00:00:00.0000000Z]

Note that when you build a query with session.Query or session.Advanced.LuceneQuery, you can dump the lucene query to a string with just a ToString() on the query.  I do this all the time like this:

var q = session.Query<Foo>()...
Debug.WriteLine(q);
var results = q.ToList();

Andreas Wimmer

unread,
Sep 5, 2013, 3:15:36 AM9/5/13
to rav...@googlegroups.com
Oren, I first thought it might be some weird abbreviation ;)

OK, then as I proposed, I work with a QueryParser and pass to it a string in Lucene syntax but "inspired" by the text I get from an "ordinary" LINQ to RavenDB query.
I use DateTimeOffset so I add the offset first to get UTC.

Thank you both

Matt Johnson

unread,
Sep 5, 2013, 2:09:00 PM9/5/13
to rav...@googlegroups.com
Yes.  You need to adjust from DateTimeOffset to UTC DateTime before building your query, and pass the Z at the end.  Basically:   yourDateTimeOffset.UtcDateTime.ToString("o")

Raven will do this for you if you use build the query through the API instead of manually.
Reply all
Reply to author
Forward
0 new messages