Search code examples
hibernate-searchhibernate-search-6

How can I reuse a field in several bridges in Hibernate Search 6?


In Hibernate Search 5 I had several custom bridges that populated the same field. That was handy so that I can perform a query on just one field. Now if I try to do it I receive this error:

HSEARCH600034: Duplicate index field definition: 'attributes'. Index field names must be unique. Look for two property mappings with the same field name, or two indexed-embeddeds with prefixes that lead to conflicting index field names, or two custom bridges declaring index fields with the same name.

I have not found a way to get an existing field from the PropertyBinding Context when implementing the PropertyBinder, only the documented way to add new fields:

    IndexFieldReference<String> attributesField = schemaElement
            .field("attributes", f -> f.asString())
            .toReference();

Am I missing something or is no longer possible and I need to add new fields?


Solution

  • How can I reuse a field in several bridges in Hibernate Search 6?

    At the moment, you cannot.

    This limitation is a side effect from the (many) sanity checks that Hibernate Search 6 performs on startup, which prevent common mistakes and indirectly allow a more intuitive behavior in the Search DSL.

    Your options are pretty much this:

    • Either you refactor your bridges to regroup all code that contributes to the same field in a single bridge (either a TypeBridge, or a PropertyBridge applied to a non-persisted getter that returns an aggregated list of all values you're interested in).
    • Or you change your bridges to each contribute to its own field, and change your search code to target all these fields at once; most (if not all) predicates allow targeting multiple fields in the same predicate.

    The second solution is also the recommended way of indexing, since it produces more accurate relevance scores.

    EDIT: If you go for solution 2, this (not yet implemented) feature might be of interest to you: https://hibernate.atlassian.net/browse/HSEARCH-3926