我有一些 BigDecimal 值,应该为搜索编制索引。 Lucene 有 NumericField,但它只有 long、double、float 和 int 的 setter。我可以将它存储为字符串,但那样我就无法从 NumericRangeQuery 中受益。
您如何存储 BigDecimals?有什么最佳做法可以分享吗?
最佳答案
如果一切都失败了,考虑扩展 AbstractField
(类似于 NumericField
扩展它的方式),TokenStream
(类似于 NumericTokenStream 的方式
扩展它)和 MultitermQuery
(类似于 NumericRangeQuery
扩展它的方式)。不幸的是,所有三个 Numeric* 类都是最终类,因此它们不能自行扩展 :( 好消息是这些类中的逻辑相当简单,为 BigDecimals 改造它应该很容易。
数据的存储很简单,因为甚至 NumericField
都将其存储在字符串中。来自 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).
如果您要走那条路,请尝试向 Lucene 开发人员发送补丁或至少填写一个 JIRA 请求。 Lucene 开发人员通常都是友善和开放的人,因此这也可以使其他人受益。
关于java - 如何在 Lucene 3.0.1 中索引 BigDecimal 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2730200/