2 Answers
2

UPDATE: Like @basZero said in his comment, starting with Lucene 2.9, you can add numeric fields to your documents. Just remember to use NumericRangeQuery instead of RangeQuery when you search.

Original answer

Lucene treats numbers as words, so their order is alphabetic:

0
1
12
123
2
22

That means that for Lucene, 12 is between 0 and 2. If you want to do a proper numerical range, you need to index the numbers zero-padded, then do a range search of [0000 TO 0002]. (The amount of padding you need depends on the expected range of values).

If you have negative numbers, just add another zero for non-negative numbers. (EDIT: WRONG WRONG WRONG. See update)

If your numbers include a fraction part, leave it as is, and zero-pad the integer part only.

Example:

-00002.12
-00001

000000
000001
000003.1415
000022

UPDATE: Negative numbers are a bit tricky, since -1 comes before -2 alphabetically. This article gives a complete explanation about dealing with negative numbers and numbers in general in Lucene. Basically, you have to "encode" numbers into something that makes the order of the items make sense.

could you please let me know how do i use rangequery for decimal numbers?thanks!
–
user40907Apr 3 '09 at 1:09

For decimals (I assume you mean decimals with a fractional component) you need to scale them up, eg. by multiplying by a million, and removing any remainder: 1.2 -> 1200000. The amount you multiply by depends on how many decimal places you need to be accurate to.
–
RichieHindleMay 2 '09 at 18:22

1

...and of course you still need to zero-pad them, as itsadok says. I should have said 1.2 -> 0001200000
–
RichieHindleMay 2 '09 at 20:38

1

Use Lucene 2.9.x and you can add numbers to the index.
–
basZeroApr 29 '11 at 18:09