英文:
How to index float/double values in Lucene
问题
I am using Lucene 8.11 because of Java 8 and need to index float/double values. But I can find only NumericDocValuesField
class which accepts only integer/long types.
英文:
I am using Lucene 8.11 because of Java 8 and need to index float/double values. But I can find only NumericDocValuesField
class which accepts only integer/long types.
答案1
得分: 1
Lucene不支持直接索引浮点或双精度值,因为它们的不精确性,一种常见的方法是通过缩放将浮点/双精度值转换为长整型或整数:
double originalValue = 123.456;
long scaledValue = (long) (originalValue * 1000);
document.add(new NumericDocValuesField("field", scaledValue));
因此,您可以决定关心的精度级别。
英文:
Lucene doesn't support direct indexing of float or double values because of their imprecise nature, one common approach is to convert your float/double values to long or int by scaling them:
double originalValue = 123.456;
long scaledValue = (long) (originalValue * 1000);
document.add(new NumericDocValuesField("field", scaledValue));
So you decide on a precision level that you care about
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论