Trying to migrate to Hibernate Search 6.
The idea is to have enum
class indexed to be able to filter out by enum
name and also to create additional Integer
field in Lucene to have sort ability.
The goal is to get 2 fields in Lucene index:
-
importance
(Text
, searchable, containingenum
item name (“LOW”, “HIGH”…) -
importance_numeric
(Numeric
, sortablem, containing score fromenum
item)?
Which type of bridge shoud I use?
What type of @*Field to put on Importance
property inside Article
?
@AllArgsConstructor @Getter
enum Importance {
FIRST(3),
TOP(2),
HIGH(1),
NORMAL(0),
LOW(-1),
BOTTOM(-2),
LAST(-3);
private final int score;
}
Enum is used inside Article
class:
@Entity @Indexed
class Article {
// ...
@Enumerated(EnumType.STRING)
@Field(
index = Index.YES,
analyze = Analyze.NO,
store = Store.NO,
bridge = @FieldBridge(impl = ImportanceBridge.class)
)
@SortableField
private Importance importance = Importance.NORMAL;
// ...
}
Bridge from Hibernate Search 5 was pretty simple:
class ImportanceBridge implements MetadataProvidingFieldBridge {
@Override
public void configureFieldMetadata(
String name,
FieldMetadataBuilder builder
) {
builder.field(name + "_numeric", FieldType.INTEGER).sortable(true);
}
@Override
public void set(
String name, Object object, Document document,
LuceneOptions luceneOptions
) {
if (!(object instanceof Importance)) {
return;
}
Importance i = (Importance) object;
int score = i.getScore();
luceneOptions.addNumericFieldToDocument(
name + "_numeric", Integer.valueOf(score), document
);
document.add(new NumericDocValuesField(name + "_numeric", score));
}
}
Tried to create this as PropertyBinder
but I got lost.
class ImportanceBridge implements PropertyBinder {
private static final String PROPERTY_NAME = "importance";
private static final String SUFFIX = "_numeric";
@Override
public void bind(PropertyBindingContext context) {
context.dependencies().use(PROPERTY_NAME);
IndexSchemaObjectField importanceNumericField =
context.indexSchemaElement().objectField(PROPERTY_NAME + SUFFIX);
// -- don't know what to do with this :(
// IndexFieldType<Integer> importanceNumericFieldType =
// context.typeFactory().asInteger().toIndexFieldType();
context.bridge(Importance.class, new ImportancePropertyBridge(
importanceNumericField.toReference()
));
}
private static class ImportancePropertyBridge
implements PropertyBridge<Importance> {
private final IndexObjectFieldReference importanceFieldReference;
private ImportancePropertyBridge(
IndexObjectFieldReference importanceFieldReference
) {
this.importanceFieldReference = importanceFieldReference;
}
@Override
public void write(
DocumentElement target, Importance bridgedElement,
PropertyBridgeWriteContext context
) {
Integer intValue = Integer.valueOf(bridgedElement.getScore());
target.addObject( this.importanceFieldReference );
// ????? what next?
}
}
}