Search code examples
spring-boothibernatehibernate-search

Hibernate Search 6 search multiple fields with multiple keywords


I want to search through these 2 different variables :

@Enumerated(EnumType.STRING)
@Column(length = 20)
@Convert(converter = WorksEnumConverter.class)
@GenericField(valueBridge = @ValueBridgeRef(type = WorksValueBridge.class))
private WorksEnum works;

@Convert(converter = AcquisitionTypeConverter.class)
@Enumerated(EnumType.STRING)
@Column(length = 10)
@GenericField(valueBridge = @ValueBridgeRef(type = AcquisitionTypeBridge.class))
private AcquisitionTypeEnum acquisitionType;

As you can see there are 2 types of Enum , and i want to use hibernate search to search via multiple keywords, I used bridge and converter and i always get the error of cannot convert string to enum This is my code for hibernate search implementation :

  if (requestSearchCustomer.getKeyword() != null) {
            final String[] keywords = requestSearchCustomer.getKeyword().split(",");
            final SearchPredicate keywordPredicate = getSearchScope().predicate().terms()
                    .fields(RequestDB_.WORKS, RequestDB_.ACQUISITION_TYPE)
                    .matchingAny(keywords).toPredicate();
            predicate.must(keywordPredicate);
        }

Solution

  • If you want to pass strings, and not the enum type that Hibernate Search expects, you will need to disable Hibernate Search's automatic conversion:

      if (requestSearchCustomer.getKeyword() != null) {
                final String[] keywords = requestSearchCustomer.getKeyword().split(",");
                final SearchPredicate keywordPredicate = getSearchScope().predicate().terms()
                        .fields(RequestDB_.WORKS, RequestDB_.ACQUISITION_TYPE)
                        .matchingAny(Arrays.asList(keywords), ValueConvert.NO).toPredicate();
                predicate.must(keywordPredicate);
            }
    

    See https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#search-dsl-argument-type