I have the following field defined on my entity class:
@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
My BigDecimalNumericFieldBridge class uses the implementation described in the docs: https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#example-custom-numericfieldbridge and in the forums here: https://discourse.hibernate.org/t/sorting-on-bigdecimal-field/2339
I'm using a custom query parser to convert the query to a numeric range query like so:
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)
}
All of the queries I do on this field return zero results even if I do a range from 0 to 999999999999 which I know includes all values. If I leave it as a string search I get the following error: "contains a string based sub query which targets the numeric field". Sorting on the field works. What am I missing? Thank you in advance for the help.
Edit Adding the field bridge logic:
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()
}
}
Adding both fields in the set
function of the field bridge logic fixes the problem:
if (value != null && (name == "test" || name == "test_sort")) {
Now my searches work. I guess that makes me wonder if I need the two separate field definitions. I think I could get away with just the one @Field
annotation and then I wouldn't need to store both test
and test_sort
.