In Hibernate Search 5, BigDecimals are stored as text by default, which will lead to something like this.
There is better support for BigDecimals in Hibernate Search 6, which will automatically convert the BigDecimal in a dedicated format with a configurable scale. So you won’t have this problem in Hibernate Search 6.
But if you didn’t upgrade, you must have a reason.
In Hibernate Search 5, you will have to use a custom bridge to convert your BigDecimals to long before indexing. There is an example of such a bridge in the reference documentation. The example just below shows how to apply the bridge to your field.
You will also need to make the field sortable, and you cannot do this with @SortableField if you’re using a custom bridge. See this example for more information.
Again, you wouldn’t need all this custom code with Hibernate Search 6; this would be enough:
I tried to follow you examples. I first added a custom bridge:
class BigDecimalNumericFieldBridge : TwoWayFieldBridge {
override fun set(name: String, value: Any, document: Document, luceneOptions: LuceneOptions) {
if (value != null) {
val decimalValue = value as BigDecimal
val indexedValue = decimalValue.multiply(storeFactor).toLong()
luceneOptions.addNumericFieldToDocument(name, indexedValue, document)
}
}
override fun get(name: String, document: Document): Any {
val fromLucene = document[name]
val storedBigDecimal = BigDecimal(fromLucene)
return storedBigDecimal.divide(storeFactor)
}
override fun objectToString(`object`: Any): String {
return `object`.toString()
}
companion object {
private val storeFactor = BigDecimal.valueOf(100)
}
}
than I remove the annotation Sortable Field from the class, instead i added FieldBridge
@NotNull
@Field
@FieldBridge(impl = BigDecimalNumericFieldBridge::class)
var salesPrice: BigDecimal = BigDecimal.ZERO
But after rebuilding the index and restarting the application, I now get this error:
org.hibernate.search.exception.SearchException: HSEARCH000301: Requested sort field(s) salesPrice are not configured for entity type com.....persistence.entities.warehouse.Article mapped to index articles_index, thus an uninverting reader must be created.
You should declare the missing sort fields using @SortableField.