如何在 Lucene 3.0.1 中索引 BigDecimal 值
我有一些 BigDecimal 值,应该对其进行索引以进行搜索。 Lucene 有 NumericField 但它只有 long、double、float 和 int 的 setter。我可以将其存储为字符串,但这样我就不会从 NumericRangeQuery 中受益。
您如何存储 BigDecimals?有什么最佳实践可以分享吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果其他一切都失败,请考虑扩展
AbstractField
(类似于NumericField
扩展它的方式)、TokenStream
(类似于NumericTokenStream
的方式> 扩展它)和MultitermQuery
(类似于NumericRangeQuery
扩展它的方式)。遗憾的是,所有三个 Numeric* 类都是最终类,因此它们无法自行扩展:( 好消息是这些类中的逻辑相当简单,对于 BigDecimals 来说应该很容易对其进行改造。数据的存储很简单,甚至
NumericField
将其存储在 String 中 来自 javadoc:如果您要走这条路,请尝试向 Lucene 开发人员发送补丁,或者至少填写 JIRA 请求。 Lucene 开发人员通常都是友善且开放的人,因此这也可以使其他人受益。
If everything else fails, considering extending
AbstractField
(similar to howNumericField
extends it),TokenStream
(similar to howNumericTokenStream
extends it) andMultitermQuery
(similar to howNumericRangeQuery
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: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.
Steven Rowe 在这篇文章中提供了有趣的想法:
http://www.lucidimagination.com/search/document/ad648772f8825a28/bigdecimal_values# 2502f96055839c3d
他说他的方案可能可以用来表示所有 BigDecimal 值。如果不需要负值,似乎更容易实现。就像 Mindas 建议的那样,您可以扩展 AbstractField 来实现这一点。
还有 Yonik Seeley 说他已经在 Solr 中开始了 BCDUtils 类的一些工作:
http://www.lucidimagination.com/search/document/ad648772f8825a28/bigdecimal_values# cef1d0e25af063ef
Steven Rowe provides interesting ideas in this post:
http://www.lucidimagination.com/search/document/ad648772f8825a28/bigdecimal_values#2502f96055839c3d
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:
http://www.lucidimagination.com/search/document/ad648772f8825a28/bigdecimal_values#cef1d0e25af063ef