如何在Lucene中索引浮点/双精度数值

huangapple go评论64阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年6月27日 18:09:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定