我在我的实体类上定义了以下字段:
@Column
@FieldBridge(impl = BigDecimalNumericFieldBridge.class)
@Fields(
@Field(),
@Field(name = "test_sort", analyze = Analyze.NO, store = Store.NO, index = Index.NO))
@NumericField
@SortableField(forField = "test_sort")
val test: BigDecimal
我的BigDecimalNumericFieldBridge类使用文档中描述的实现:https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#example-custom-numericfieldbridge和以下论坛中:https://discourse.hibernate.org/t/sorting-on-bigdecimal-field/2339
我使用自定义查询解析器将查询转换为数字范围查询,如下所示:
override fun newRangeQuery(field: String, part1: String, part2: String, startInclusive: Boolean, endInclusive: Boolean) {
if ("test" == field) {
val convertedPart1 = BigDecimal(part1).multiply(storeFactor).longValueExact()
val convertedPart2 = BigDecimal(part2).multiply(storeFactor).longValueExact()
return NumericRangeQuery.newLongRange(field, convertedPart1, convertedPart2, startInclusive, endInclusive)
}
return super.newRangeQuery(field, part1, part2, startInclusive, endInclusive)
}
我对这个字段所做的所有查询都返回零个结果,即使我在0到999999999999的范围内进行查询,我知道该范围包含所有值。如果我将其保留为字符串搜索,则会出现以下错误:““包含基于字符串得子查询,该子查询得目的是数值字段”“.对该字段进行排序有效.我遗漏了什么?提前感谢您得帮助.
编辑添加场桥逻辑:
class BigDecimalNumericFieldBridge : TwoWayFieldBridge {
override fun get(name: String, document: Document): Any? {
val fromLucene: String = document.get(name) ?: ""
if (fromLucene.isNotBlank()) {
return fromLucene.toBigDecimal().divide(LUCENE_BIG_DECIMAL_STORE_FACTOR)
}
return null
}
override fun set(name: String, value: Any?, document: Document, options: LuceneOptions) {
if (value != null && name == "test_sort") {
val decimalValue: BigDecimal = value as BigDecimal
val indexedValue: Long = decimalValue
.multiply(LUCENE_BIG_DECIMAL_STORE_FACTOR)
.longValueExact()
options.addNumericFieldToDocument(name, indexedValue, document)
options.addNumericDocValuesFieldToDocument(name, indexedValue, document)
}
}
override fun objectToString(obj: Any?): String {
return obj.toString()
}
}
2条答案
按热度按时间7hiiyaii1#
在字段桥逻辑的
set
函数中添加两个字段可解决该问题:现在我的搜索工作了。我想这让我怀疑我是否需要两个独立的字段定义。我想我可以只使用一个
@Field
注解,这样我就不需要同时存储test
和test_sort
了。