【发布时间】:2017-11-03 12:38:51
【问题描述】:
我使用 Lucene 6.1.0 来索引具有名称和值的元素。
例如
<documents>
<Document>
<field name="NAME" value="Long_-1"/>
<field name="VALUE" value="-1"/>
</Document>
<Document>
<field name="NAME" value="Double_-1.0"/>
<field name="VALUE" value="-1.0"/>
</Document>
<Document>
<field name="NAME" value="Double_-0.5"/>
<field name="VALUE" value="-0.5"/>
</Document>
<Document>
<field name="NAME" value="Long_0"/>
<field name="VALUE" value="0"/>
</Document>
<Document>
<field name="NAME" value="Double_0.0"/>
<field name="VALUE" value="0.0"/>
</Document>
<Document>
<field name="NAME" value="Double_0.5"/>
<field name="VALUE" value="0.5"/>
</Document>
<Document>
<field name="NAME" value="Long_1"/>
<field name="VALUE" value="1"/>
</Document>
<Document>
<field name="NAME" value="Double_1.0"/>
<field name="VALUE" value="1.0"/>
</Document>
<Document>
<field name="NAME" value="Double_1.5"/>
<field name="VALUE" value="1.5"/>
</Document>
<Document>
<field name="NAME" value="Long_2"/>
<field name="VALUE" value="2"/>
</Document>
</documents>
根据文档,我使用 LongPoint 和 DoublePoint 来构建索引。
public static void addLongField(String name, long value, Document doc) {
doc.add(new LongPoint(name, value));
// since Lucene6.x a second field is required to store the values.
doc.add(new StoredField(name, value));
}
public static void addDoubleField(String name, double value, Document doc) {
doc.add(new DoublePoint(name, value));
// since Lucene6.x a second field is required to store the values.
doc.add(new StoredField(name, value));
}
由于我对 long 和 double 值使用相同的字段,如果最小值和最大值有不同的符号,我的 RangeQuery 会得到奇怪的结果。
LongPoint.newRangeQuery(field, minValue, maxValue);
DoublePoint.newRangeQuery(field, minValue, maxValue);
这个例子是正确的:
VALUE:[1 TO 1] VALUE:[0.5 TO 1.0]
结果:
0.5 Double_0.5
1 Long_1
1.0 Double_1.0
这个例子是错误的
VALUE:[0 TO 1] VALUE:[-0.5 TO 1.0]
结果:
0 Long_0
0.0 Double_0.0
1 Long_1
-1 Long_-1
-0.5 Double_-0.5
0.5 Double_0.5
1.0 Double_1.0 em>
2 Long_2
除了正确的结果外,所有长值都会返回。
有人知道为什么吗?
不能在同一个字段中存储 long 和 double 值吗?
非常感谢。
BR托比亚斯
【问题讨论】:
标签: lucene double long-integer