# HS 6 Custom Bridges

**URL:** <https://discourse.hibernate.org/t/hs-6-custom-bridges/4577>\
**Category:** Hibernate Search\
**Created:** [September 16, 2020, 9:53am UTC](https://discourse.hibernate.org/t/hs-6-custom-bridges/4577 "2020-09-16T09:53:57Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Swarnkar](https://avatars.discourse-cdn.com/v4/letter/s/f4b2a3/32.png) [@Swarnkar](https://discourse.hibernate.org/u/Swarnkar)\
**Post date:** [September 16, 2020, 9:53am UTC](https://discourse.hibernate.org/t/hs-6-custom-bridges/4577/1 "2020-09-16T09:53:57Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [September 16, 2020, 9:56am UTC](https://discourse.hibernate.org/t/hs-6-custom-bridges/4577/2 "2020-09-16T09:56:46Z")

</div>

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)`.

---

<div class="post-metadata">

**Author:** ![Swarnkar](https://avatars.discourse-cdn.com/v4/letter/s/f4b2a3/32.png) [@Swarnkar](https://discourse.hibernate.org/u/Swarnkar)\
**Post date:** [September 16, 2020, 9:59am UTC](https://discourse.hibernate.org/t/hs-6-custom-bridges/4577/3 "2020-09-16T09:59:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [September 16, 2020, 10:04am UTC](https://discourse.hibernate.org/t/hs-6-custom-bridges/4577/4 "2020-09-16T10:04:40Z")

</div>

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](https://docs.jboss.org/hibernate/search/6.0/reference/en-US/html_single/#mapper-orm-mapping-inspect)

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

```

---

<div class="post-metadata">

**Author:** ![Swarnkar](https://avatars.discourse-cdn.com/v4/letter/s/f4b2a3/32.png) [@Swarnkar](https://discourse.hibernate.org/u/Swarnkar)\
**Post date:** [September 16, 2020, 10:05am UTC](https://discourse.hibernate.org/t/hs-6-custom-bridges/4577/5 "2020-09-16T10:05:59Z")

</div>

> [@yrodiere](#):
>
> If you target an integer field, you must pass integer values to the DSL. It’s as simple as that.

in HS 5  
 ![image](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/2/2f18495ea3ecd1fc4dc4a0f6d60d2cc70bed0345.png)

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

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/9/9eed0c2b1666f17ab1cbe79b3006b020712f2005.png)

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [September 16, 2020, 10:06am UTC](https://discourse.hibernate.org/t/hs-6-custom-bridges/4577/6 "2020-09-16T10:06:21Z")

</div>

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, …

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [September 16, 2020, 10:10am UTC](https://discourse.hibernate.org/t/hs-6-custom-bridges/4577/7 "2020-09-16T10:10:18Z")

</div>

> [@Swarnkar](#):
>
> in HS 5  
> ![image](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/2/2f18495ea3ecd1fc4dc4a0f6d60d2cc70bed0345.png)
> 
> and in runtime we can just ignore bridge at runtime like this
> 
> ![image](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/9/9eed0c2b1666f17ab1cbe79b3006b020712f2005.png)

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.

---

<div class="post-metadata">

**Author:** ![Swarnkar](https://avatars.discourse-cdn.com/v4/letter/s/f4b2a3/32.png) [@Swarnkar](https://discourse.hibernate.org/u/Swarnkar)\
**Post date:** [September 16, 2020, 12:55pm UTC](https://discourse.hibernate.org/t/hs-6-custom-bridges/4577/8 "2020-09-16T12:55:19Z")

</div>

> [@yrodiere](#):
>
> 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, …

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](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/9/93d372851ac8ef955354bf06108ec111dfb29c16.png)

**2: predicate**  
 ![image](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/5/5bd347cc1ec5c71e2c4873265fa943402f343024.png)  
3: saving sales amount  
 ![image](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/0/0fab0896b3962658dddcd61d43b11d857caf67bd.png)  
**4: TestCase**  
**non -working**

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/8/893929359b70de5396e081a7939f4257f063ab4e.png)  
**working**  
 ![image](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/b/beaedb316d0f0ccf91b01f69e4a1e3f1da9f69b8.png)  
@yrodiere any guess whats wrong here ?  
what annotation I should put to indicate its a numeric field rather generic field?

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [September 16, 2020, 1:32pm UTC](https://discourse.hibernate.org/t/hs-6-custom-bridges/4577/9 "2020-09-16T13:32:30Z")

</div>

> [@Swarnkar](#):
>
> 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.

> [@Swarnkar](#):
>
> @yrodiere any guess whats wrong here ?

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](https://github.com/yrodiere/hibernate-test-case-templates/tree/long-range/search/hibernate-search-6/orm-lucene).

---

<div class="post-metadata">

**Author:** ![Swarnkar](https://avatars.discourse-cdn.com/v4/letter/s/f4b2a3/32.png) [@Swarnkar](https://discourse.hibernate.org/u/Swarnkar)\
**Post date:** [September 16, 2020, 2:52pm UTC](https://discourse.hibernate.org/t/hs-6-custom-bridges/4577/10 "2020-09-16T14:52:39Z")

</div>

🙏 thanks alot for ur time
