问题
I have two entities:
@Indexed
@Entity
@Table(name = "LK_CONTACT_TYPE")
public class ContactTypeEntity {
@Id
@Column(name = "ID")
@DocumentId
Integer id;
@SortableField
@Field(store = Store.YES, bridge = @FieldBridge(impl = ContactTypeComparator.class))
@Column(name = "NAME")
String name;
getter() .. setter()..
}
@Indexed
@Entity
@Table(name = "DIRECTORY")
public class DirectoryEntity {
....
@IndexedEmbedded(prefix = "contactType.", includePaths = {"id", "name"})
@ManyToOne
@JoinColumn(name = "CONTACT_TYPE")
private ContactTypeEntity contactType;
getter() ... setter()...
}
public class ContactTypeComparator implements MetadataProvidingFieldBridge, TwoWayStringBridge {
@Override
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
if ( value != null ) {
int ordinal = getOrdinal(value.toString());
luceneOptions.addNumericFieldToDocument(name, ordinal, document);
}
}
@Override
public void configureFieldMetadata(String name, FieldMetadataBuilder builder) {
builder.field(name, FieldType.INTEGER).sortable(true);
}
private int getOrdinal(ContactType value) {
switch( value ) {
case PBX: return 0;
case TEL: return 1;
case GSM: return 2;
case FAX: return 3;
default: return 4;
}
}
@Override
public Object get(String name, Document document) {
return document.get( name );
}
@Override
public String objectToString(Object object) {
return object.toString();
}
}
and Query part:
...
query.setSort(queryBuilder.sort().byScore().andByField("contactType.name").createSort());
query.setProjection(... , "contactType.name",...);
...
I am getting the following error: java.lang.IllegalStateException: unexpected docvalues type NONE for field 'contactType.name' (expected=NUMERIC). Use UninvertingReader or index with docvalues.
Note: I am using hibernate-search 5.10. I want to show contactType.name name on UI instead of number. For more detail
回答1:
Seems my original suggestion was missing a bit in the set()
method, in order to add the docvalues:
@Override
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
if ( value != null ) {
int ordinal = getOrdinal(value.toString());
luceneOptions.addNumericFieldToDocument(name, ordinal, document);
// ADD THIS
luceneOptions.addNumericDocValuesFieldToDocument(name, ordinal, document);
}
}
On top of that, if you need to use the field for both sort and projection, I would recommend declaring two fields. Otherwise the projection will return integers, which is not what you want.
So, do this:
@Indexed
@Entity
@Table(name = "LK_CONTACT_TYPE")
public class ContactTypeEntity {
@Id
@Column(name = "ID")
@DocumentId
Integer id;
@SortableField
// CHANGE THESE TWO LINES
@Field(store = Store.YES)
@Field(name = "name_sort", bridge = @FieldBridge(impl = ContactTypeComparator.class))
@Column(name = "NAME")
String name;
getter() .. setter()..
}
@Indexed
@Entity
@Table(name = "DIRECTORY")
public class DirectoryEntity {
....
// CHANGE THIS LINE
@IndexedEmbedded(prefix = "contactType.", includePaths = {"id", "name", "name_sort"})
@ManyToOne
@JoinColumn(name = "CONTACT_TYPE")
private ContactTypeEntity contactType;
getter() ... setter()...
}
public class ContactTypeComparator implements MetadataProvidingFieldBridge, TwoWayStringBridge {
@Override
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
if ( value != null ) {
int ordinal = getOrdinal(value.toString());
luceneOptions.addNumericFieldToDocument(name, ordinal, document);
// ADD THIS LINE
luceneOptions.addNumericDocValuesFieldToDocument(name, ordinal, document);
}
}
@Override
public void configureFieldMetadata(String name, FieldMetadataBuilder builder) {
builder.field(name, FieldType.INTEGER).sortable(true);
}
private int getOrdinal(ContactType value) {
switch( value ) {
case PBX: return 0;
case TEL: return 1;
case GSM: return 2;
case FAX: return 3;
default: return 4;
}
}
@Override
public Object get(String name, Document document) {
return document.get( name );
}
@Override
public String objectToString(Object object) {
return object.toString();
}
}
Then query like this:
...
query.setSort(queryBuilder.sort().byScore().andByField("contactType.name_sort").createSort());
query.setProjection(... , "contactType.name",...);
...
来源:https://stackoverflow.com/questions/59752591/error-on-using-a-custom-bridge-of-hibernate-search