Hi, In the documentation (https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#numeric-field-annotation) we have the example: Example 27. Defining a custom NumericFieldBridge for BigDecimal
public class BigDecimalNumericFieldBridge extends NumericFieldBridge {
private static final BigDecimal storeFactor = BigDecimal.valueOf(100);
@Override
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
if ( value != null ) {
BigDecimal decimalValue = (BigDecimal) value;
long tmpLong = decimalValue.multiply( storeFactor ).longValue();
Long indexedValue = Long.valueOf( tmpLong );
luceneOptions.addNumericFieldToDocument( name, indexedValue, document );
}
}
@Override
public Object get(String name, Document document) {
String fromLucene = document.get( name );
BigDecimal storedBigDecimal = new BigDecimal( fromLucene );
return storedBigDecimal.divide( storeFactor );
}
}
By checking the class (org.hibernate.search.bridge.builtin.NumericFieldBridge), I realized that it is now an 'enum' and the constructor is private respectively. I believe the documentation is incorrect, or a refactoring made it impossible to extend the class. Particularly, I'm using with Elasticsearch 5.6 like this:
public class BigDecimalFieldBridge implements MetadataProvidingFieldBridge, TwoWayFieldBridge {
@Override
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
if (value != null) {
Double indexedValue = ((BigDecimal) value).doubleValue();
luceneOptions.addNumericFieldToDocument(name, indexedValue, document);
}
}
@Override
public Object get(String name, Document document) {
String fromLucene = document.get(name);
return StringHelper.isEmpty(fromLucene) ? null : new BigDecimal(fromLucene);
}
@Override
public String objectToString(Object object) {
return object.toString();
}
@Override
public void configureFieldMetadata(String name, FieldMetadataBuilder builder) {
builder.field(name, FieldType.DOUBLE);
}
}
If it's just the documentation, I can update it. We can even add more examples, such as a custom FieldBridge to a complex enum too. |