Yes that’s one solution.
If you want to use only one annotation, you can do this instead:
// NOTE: this is a binder, not a bridge.
class ImportanceBinder implements PropertyBinder {
@Override
public void bind(PropertyBindingContext context) {
// This bridge is applied to properties of enum type, and that type is immutable.
// Thus we don't need to declare precise dependencies.
context.dependencies().useRootOnly();
context.bridge(Importance.class, new ImportancePropertyBridge(
context.indexSchemaElement()
.field(context.bridgedElement().name(), f -> f.asString())
.toReference(),
context.indexSchemaElement()
.field(context.bridgedElement().name() + "_numeric", f -> f.asInteger()
.searchable(Searchable.NO).sortable(Sortable.YES))
.toReference()
));
}
private static class ImportancePropertyBridge
implements PropertyBridge<Importance> {
private final IndexFieldReference<String> stringField;
private final IndexFieldReference<Integer> integerField;
private ImportancePropertyBridge(
IndexFieldReference<String> stringField,
IndexFieldReference<Integer> integerField
) {
this.stringField = stringField;
this.integerField = integerField;
}
@Override
public void write(
DocumentElement target, Importance bridgedElement,
PropertyBridgeWriteContext context
) {
target.addValue( stringField, bridgedElement.name() );
target.addValue( integerField, bridgedElement.getScore() );
}
}
}
@Entity @Indexed
class Article {
// ...
@PropertyBinding(binder = @PropertyBinderRef(type = ImportanceBinder.class))
@Enumerated(EnumType.STRING)
private Importance importance = Importance.NORMAL;
// ...
}
See also PropertyBinder
, Declaring fields, Declaring field types.