lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mindaugas Žakšauskas <>
Subject NumericField API
Date Tue, 01 Jun 2010 11:54:22 GMT

I have recently been in charge of converting code that was using
pre-3.0 API to be compatible with 3.0 API.

There was a piece of code which was storing a date field:

String date = "20091231131415"; // yyyyMMddHHmmss
new Field("creationDate", date, Field.Store.YES, Field.Index.UN_TOKENIZED);

After some documents being indexed, the following query would retrieve
all documents created in 2009:

new ConstantScoreRangeQuery("creationDate", "20090101000000",
"20100101000000", true, true);

Query results would be sortable by simply adding this sort:

boolean descending = ...;   // either true or false
new Sort(new SortField("creationDate", SortField.STRING, descending));

Unfortunately this sequence doesn't work in 3.0.
ConstantScoreRangeQuery, for example, is gone and replaced with
NumericRangeQuery. With this in mind, Field creation should now become
as follows:

long date = 20091231131415L; // same format but different type
NumericField nf = new NumericField("creationDate", Field.Store.YES, true);

And the range query now looks like as:


This does work, but the above sort isn't. Exception says: "there are
more terms than documents in field "creationDate", but it's impossible
to sort on tokenized fields".

In order to get rid of this exception, I had to change one of the following:
- SortField must be changed from SortField.STRING to SortField.LONG
- NumericField constructor must use false for its "index" (last) parameter.

This is a bit weird. So, here are my questions:

1) I thought the difference between SortField.LONG and
SortField.STRING should only be as in numeric sorting VS
lexicographical sorting, right? Why would changing to SortField.LONG
prevent the exception?
2) How does that relate to passing index=true VS index=false in
NumericField constructor? Which of the two is preferred, assuming I
need the data to be stored and indexed as well as being able to run
range queries?
3) NumericField API is marked as experimental and volatile
( Is there
any other "stable" API I can rely on in Lucene 3.0? If not, what would
be possible NumericField replacement I could use now?

Thanks in advance.


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

View raw message