Yoann Rodière (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
) *created* an issue
Hibernate Search (
https://hibernate.atlassian.net/browse/HSEARCH?atlOrigin=eyJpIjoiMDNmM2Fh...
) / Improvement (
https://hibernate.atlassian.net/browse/HSEARCH-4676?atlOrigin=eyJpIjoiMDN...
) HSEARCH-4676 (
https://hibernate.atlassian.net/browse/HSEARCH-4676?atlOrigin=eyJpIjoiMDN...
) Use .and() syntax by default with .where(BiFunction) and .nested() predicate? (
https://hibernate.atlassian.net/browse/HSEARCH-4676?atlOrigin=eyJpIjoiMDN...
)
Issue Type: Improvement Assignee: Unassigned Components: engine Created: 17/Aug/2022 06:45
AM Fix Versions: 6.2-backlog Priority: Major Reporter: Yoann Rodière (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
)
Follows up on
https://hibernate.atlassian.net/browse/HSEARCH-4601 (
https://hibernate.atlassian.net/browse/HSEARCH-4601 )
The .where(BiFunction) syntax uses a bool predicate:
MySearchParameters searchParameters = getSearchParameters();
List<Book> hits = searchSession.search( Book.class )
.where( (f, b) -> {
b.must( f.matchAll() );
if ( searchParameters.getGenreFilter() != null ) {
b.must( f.match().field( "genre" )
.matching( searchParameters.getGenreFilter() ) );
}
if ( searchParameters.getFullTextFilter() != null ) {
b.must( f.match().fields( "title", "description" )
.matching( searchParameters.getFullTextFilter() ) );
}
if ( searchParameters.getPageCountMaxFilter() != null ) {
b.must( f.range().field( "pageCount" )
.atMost( searchParameters.getPageCountMaxFilter() ) );
}
} )
.fetchHits( 20 );
But most of the time, I expect people will want an and , and we just introduced a simpler
syntax for and predicates (
https://hibernate.atlassian.net/browse/HSEARCH-4601 (
https://hibernate.atlassian.net/browse/HSEARCH-4601 ) ).
It would probably be better to use the and syntax there:
MySearchParameters searchParameters = getSearchParameters();
List<Book> hits = searchSession.search( Book.class )
.where( (f, b) -> {
b.add( f.matchAll() );
if ( searchParameters.getGenreFilter() != null ) {
b.add( f.match().field( "genre" )
.matching( searchParameters.getGenreFilter() ) );
}
if ( searchParameters.getFullTextFilter() != null ) {
b.add( f.match().fields( "title", "description" )
.matching( searchParameters.getFullTextFilter() ) );
}
if ( searchParameters.getPageCountMaxFilter() != null ) {
b.add( f.range().field( "pageCount" )
.atMost( searchParameters.getPageCountMaxFilter() ) );
}
} )
.fetchHits( 20 );
Similarly, the current syntax for adding clauses to a nested predicate is that of the bool
predicate:
.where( f -> f.and()
.add( f.range().field( "summary.total" )
.atLeast( new BigDecimal( "20.0" ) ) )
.add( f.range().field( "summary.shipping" )
.atMost( new BigDecimal( "10.0" ) ) )
.add( f.nested( "lineItems" )
.must( f.range().field( "lineItems.amount" )
.between( new BigDecimal( "7.0" ),
new BigDecimal( "9.0" ) ) )
.must( f.match().field( "lineItems.category"
)
.matching( "BOOK" )
) ))
However, most of the time one will simply want an and inside the nested predicate: an or
would be equivalent to not using the nested predicate to begin with, and advanced features
of the bool predicate are… well, for advanced users.
We should probably use an (implicit) and there as well:
.where( f -> f.and()
.add( f.range().field( "summary.total" )
.atLeast( new BigDecimal( "20.0" ) ) )
.add( f.range().field( "summary.shipping" )
.atMost( new BigDecimal( "10.0" ) ) )
.add( f.nested( "lineItems" )
.add( f.range().field( "lineItems.amount" )
.between( new BigDecimal( "7.0" ),
new BigDecimal( "9.0" ) ) )
.add( f.match().field( "lineItems.category"
)
.matching( "BOOK" )
) ))
(
https://hibernate.atlassian.net/browse/HSEARCH-4676#add-comment?atlOrigin...
) Add Comment (
https://hibernate.atlassian.net/browse/HSEARCH-4676#add-comment?atlOrigin...
)
Get Jira notifications on your phone! Download the Jira Cloud app for Android (
https://play.google.com/store/apps/details?id=com.atlassian.android.jira....
) or iOS (
https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=Em...
) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100204- sha1:f005d5a )