Tutorial :How to index BigDecimal values in Lucene 3.0.1


I have some BigDecimal values which should be indexed for searching. Lucene has NumericField but it has setters only for long, double, float and int. I could store it as a String but then I would not benefit from NumericRangeQuery.

How you have stored your BigDecimals? Any best practices to share?


Steven Rowe provides interesting ideas in this post:

He says that his scheme could probably be used to represent all BigDecimal values. It seems easier to implement if you don't need negative values. Like mindas suggested, you could extend AbstractField to implement this.

There is also Yonik Seeley who says he has started some work in Solr for that with the class BCDUtils:


If everything else fails, considering extending AbstractField (similar to how NumericField extends it), TokenStream (similar to how NumericTokenStream extends it) and MultitermQuery (similar to how NumericRangeQuery extends it). All of three Numeric* classes are unfortunately final so they cannot be extended on their own :( The good news is that logic in these classes is fairly trivial and it should be easy to retrofit this for BigDecimals.

The storing of data is trivial as even NumericField stores it in a String. From the javadoc:

NOTE: This class is only used during indexing. When retrieving the stored field value from a Document instance after search, you will get a conventional Fieldable instance where the numeric values are returned as Strings (according to toString(value) of the used data type).

If you'll go that route, try sending patch to Lucene developers or at least fill a JIRA request. Lucene devs are generally nice and open people so this could benefit others, too.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »