HS 6 Custom Bridges

Hi tried looking for the custom bridges in docs but find really tough to integrate in system.

Looking for Bridges for
1: int and Integer class Bridge
2: long and Long Class Bridge
3: boolean and Boolean Class Bridge
Also for some custom Enum type Bridges.

It would be great if please provide the live example for case where i have to do a search on integer property as below error I got if I wont be able to cast this value from sting to int and at run time.

Servlet.service() for servlet [dispatcherServlet] in context with path [/api] threw exception [Request processing failed; nested exception is org.hibernate.search.util.common.SearchException: HSEARCH400532: Unable to convert DSL parameter: Cannot cast java.lang.String to java.lang.Integer
Context: field ‘salesOrder.items.destinationStationId’] with root cause
java.lang.ClassCastException: Cannot cast java.lang.String to java.lang.Integer
at java.lang.Class.cast(Class.java:3369)
at org.hibernate.search.engine.backend.types.converter.spi.DslConverter.convertUnknown(DslConverter.java:72)
at org.hibernate.search.backend.elasticsearch.types.predicate.impl.ElasticsearchStandardMatchPredicate$Builder.value(ElasticsearchStandardMatchPredicate.java:104)
at org.hibernate.search.engine.search.predicate.dsl.impl.MatchPredicateFieldMoreStepImpl$CommonState.matching(MatchPredicateFieldMoreStepImpl.java:87)
at org.hibernate.search.engine.search.predicate.dsl.impl.MatchPredicateFieldMoreStepImpl.matching(MatchPredicateFieldMoreStepImpl.java:61)
at org.hibernate.search.engine.search.predicate.dsl.MatchPredicateMatchingStep.matching(MatchPredicateMatchingStep.java:33)

If you target an integer field, you must pass integer values to the DSL. It’s as simple as that.

If all you have is a string, well, parse it using Integer.parseInt(myString).

Yes that’s correct but the problem I am facing is I don’t know the type only at runtime it will be provided.

Hibernate Search needs the actual type and can’t work on a string.

I suppose you can leverage Hibernate Search’s metamodel to discover the type of a field: https://docs.jboss.org/hibernate/search/6.0/reference/en-US/html_single/#mapper-orm-mapping-inspect

SearchMapping mapping = Search.mapping( entityManagerFactory ); 
SearchIndexedEntity bookEntity = mapping.indexedEntity( Book.class ); 
IndexDescriptor indexDescriptor = bookEntity.indexManager().descriptor(); 
Class<?> expectedType = indexDescriptor.field( "myField" ).toValueField().type().dslArgumentClass();

in HS 5
image

and in runtime we can just ignore bridge at runtime like this

Alternatively, you can define a custom bridge to put your numeric data in string fields, with all the downsides this imply: performance, weird results for range queries and sorts, …

Yes, this will compile and run, but you won’t get any match because the field is numeric and you’re trying to execute a string query. The only change here, really, is that Hibernate Search 6 tells you something is wrong instead of ignoring the problem.

I got what you said here instead of making a long or integer range it will do like string range (I faced this issue earlier and now also explaining below)
i.e. if I have amount 430 and if i make a query like 1==>400 it will return result
if i make a query like 1==>1000 it will return null result set.
Now this unexpected behavior is still in the application
Below is details

1: property
image

2: predicate
image
3: saving sales amount
image
4: TestCase
non -working


working

@yrodiere any guess whats wrong here ?
what annotation I should put to indicate its a numeric field rather generic field?

Generic fields of type long are always numeric, there’s nothing more to do.

Honestly, I couldn’t say. There’s simply too many missing pieces of information. Your application is doing tons of things that I don’t see.

This should go without saying, but we have tests for such a simple use cases, and they pass. If you don’t believe me, see this attempt at creating a reproducer.

1 Like

:pray: thanks alot for ur time