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 Shai Erera <se...@gmail.com> on 2011/04/05 16:06:26 UTC

Retrieving the first document in a range

Hi

We have a date field which is indexed as NumericField<Long> and we'd like to
get the first docid that is since 3 weeks ago. Currently we're doing
something like this:

{code}
Query q = NumericRangeQuery.newLongRange("date", timeBefore3Weeks,
System.currentTimeMillis(), true, true);
Scorer s = q.weight(searcher).scorer(reader, true, false);
int doc = s.nextDoc();
if (doc == DISI.NO_MORE_DOCS) {
  // no docs since last 3 weeks
} else {
  // do something
}
{code}

Is there a more efficient way to achieve this?

Can we use TermEnum to skip to the first term 'after 3 weeks'? If so, we can
pull the first doc that appears in the TermDocs of that Term (if it's a
valid term). I'm not sure though that we can use the lexicon with numeric
fields this way ...

Thanks
Shai

Re: Retrieving the first document in a range

Posted by Yonik Seeley <yo...@lucidimagination.com>.
On Tue, Apr 5, 2011 at 10:06 AM, Shai Erera <se...@gmail.com> wrote:
> Can we use TermEnum to skip to the first term 'after 3 weeks'? If so, we can
> pull the first doc that appears in the TermDocs of that Term (if it's a
> valid term).

Yep.  Try this to get the term you want to use to seek:
        BytesRef term = new BytesRef();
        NumericUtils.longToPrefixCoded(longval, 0, term);


-Yonik
http://www.lucenerevolution.org -- Lucene/Solr User Conference, May
25-26, San Francisco

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