lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Markus Jelsma <>
Subject Range query on date field
Date Thu, 24 Nov 2016 13:12:35 GMT
Hi - i seem to be having trouble correctly executing a range query on a date field.

The following Solr document is indexed via a unit test followed by a commit:

    <str name="type">view</str>
    <str name="key">test_key</str>
    <date name="time">2013-01-09T17:11:40Z</date>

I can retrieve the document simply wrapping term queries in a boolean query like this:

      BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();

      Query typeQuery = new TermQuery(new Term("type", "view"));
      queryBuilder.add(typeQuery, Occur.MUST);
      long count = searcher.get().count(;

This gets me exactly 1 in variable count. This is all fine. But i also need to restrict the
query to a date, so i add a simple (or so i thought) range query!

      TermRangeQuery timeQuery = TermRangeQuery.newStringRange("time", date + "T00:00:00Z",
date + "T23:59:59Z", true, true);
      queryBuilder.add(timeQuery, Occur.MUST);

But no, it doesn't work. No matter what i do, i don't get any results! Thinking there is something
wrong with my range query, i even tried StandardQueryParser, nothing can go wrong if Lucene
builds the query for me right?

      StandardQueryParser parser = new StandardQueryParser();
      Query q = parser.parse(type:view AND time:[" + date + "T00:00:00Z TO " + date +
"T23:59:59Z]", "query");

In both cases, toString of the final query yields similar results, only the order is different.
The letters T and Z are somehow lowercased by the query parser.

I feel incredible stupid so many thanks in advance!

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message