Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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);
        }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading