Sorting on dynamic fields

#1

I’m current using Hibernate 5.4.2 with search 5.11.1 (after upgrading from 5.0.11 and 5.4.0 respectively) and have a searchable entity with the following field definition:

    @OrderBy("id ASC")
    @IndexColumn(
            name = "custom_field_order")
    @ElementCollection(
            fetch = FetchType.EAGER)
    @CollectionTable(
            name = "USER_ATTRIBUTES")
    @IndexedEmbedded
    @Fetch(FetchMode.SUBSELECT)
    @Field(
            index = Index.YES,
            store = Store.NO)
    @FieldBridge(impl = CustomFieldBridge.class)
    private List<CustomField> customField;

With CustomField having the following fields:

@Column(
            name = "USER_ATTRIBUTE_ID")
    @Field(
            index = Index.YES,
                store = Store.NO)
    private String id;

    @Column(
            name = "VALUE",
                length = 1024)
    @Field(
            index = Index.YES,
                store = Store.NO)
    private String value;

And the bridge having the following definition:

public class CustomFieldBridge implements FieldBridge
{

    @Override
    public void set(String name, Object value, Document document, LuceneOptions luceneOptions)
    {
        if (value != null)
        {
            List<CustomField> fields = (List) value;
            for (CustomField field: fields)
            {
                String fieldName = field.getId();
                String fieldValue = field.getValue();
                luceneOptions.addFieldToDocument(fieldName, fieldValue, document);
            }
        }
    }
}

This allows searching of any of the defined custom fields which is great, but we had issues when it comes to sorting. After reading the docs, it looked like the solution was to implement the MetadataProvidingFieldBridge interface rather than FieldBridge, providing the field names as part of the configureFieldMetadata() method call. As our field names are derived from the contents of the List object of the entity being indexed and aren’t related in anyway to the name passed into configureFieldMetadata(), this didn’t work for us. As a result, we’re having to resort to turning on “index_uninverting_allowed” to allow these fields to be sorted without an exception being thrown.

Am I right in thinking what we’re looking to achieve isn’t possible without using the “index_uninverting_allowed” parameter? It would be nice if we could avoid logging the warning on query as we might have to live with it but you can’t have everything!

0 Likes

#2

You’re right, in your case, if you want to use Hibernate Search to query the index, you need to turn on “index_uninverting_allowed” and you will get warnings.

If you knew in advance the list of potential field names, you would be able to use MetadataProvidingFieldBridge#configureFieldMetadata, but I suppose you don’t.

On the bright side, we’re working on better support for dynamic fields. It’s not done yet, but due to the new approach, in Search 6 you should be able to get dynamic fields that are sortable without the index uninverting.

0 Likes

#3

Ah ok, thanks for confirming this.

I’ll follow Search 6’s development with great interest, thanks!

0 Likes