[hibernate-issues] [JIRA] (HSEARCH-3889) Exception Predicate targets unexpected fields when use nested() in sort filter

Yoann Rodière (JIRA) jira at hibernate.atlassian.net
Tue Apr 14 03:05:05 EDT 2020


Yoann Rodière ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%3A58fa1ced-171a-4c00-97e8-5d70d442cc4b ) *commented* on HSEARCH-3889 ( https://hibernate.atlassian.net/browse/HSEARCH-3889?atlOrigin=eyJpIjoiYWU4Nzk5NGMxZmU4NDgxYmI5MDY5NDcyZjkzZWI1ZDMiLCJwIjoiaiJ9 )

Re: Exception Predicate targets unexpected fields when use nested() in sort filter ( https://hibernate.atlassian.net/browse/HSEARCH-3889?atlOrigin=eyJpIjoiYWU4Nzk5NGMxZmU4NDgxYmI5MDY5NDcyZjkzZWI1ZDMiLCJwIjoiaiJ9 )

This exception is on purpose. The predicate passed as sort filter should only be referring to fields in the same nested document as the sort field, or a more deeply nested document; otherwise the filter just cannot work. See HSEARCH-3885 ( https://hibernate.atlassian.net/browse/HSEARCH-3885 ) Resolved.

This code would work just fine:

            PermissionQuery allowQuery = new PermissionQuery( "prices.permissions" , operation, user, true );
           PermissionQuery dannyQuery = new PermissionQuery( "prices.permissions" , operation, user, false );

           BooleanQuery query = new BooleanQuery.Builder()
                   .add(allowQuery, BooleanClause.Occur.MUST)
                   .add(dannyQuery, BooleanClause.Occur.MUST_NOT)
                   .build();

           filter = predicate.extension(LuceneExtension.get()).fromLuceneQuery(query)
                   .toPredicate();

           sort.add(sorter.field( "prices.bruttoPrice_sort" )
                   .asc()
                   .mode(SortMode.MIN)
                   .filter(filter));

           SearchQuery<Assortment> query = session.search(scope)
                   .where((f) -> f.bool().must(select)
                   .filter(f.nested().objectField( "prices" ).nest(filter))
                   .sort(sort.toSort())
                   .toQuery();

Note that's a bit complicated, but only because of the native query. Without it, we wouldn't have to use an explicit nested predicate to begin with.

As to your patch, I'm a bit concerned it will allow "nesting" multiple nested predicates on the same object field, which I do not think will work correctly. Something like this:

           SearchQuery<Assortment> query = session.search(scope)
                   .where(f -> f.nested().objectField( "prices" ).nest(
                                       f.nested().objectField( "prices" ).nest(
                                                            f.nested().objectField( "prices" ).nest(
                                                                                 f.match().field( "prices.thing" ).matching( "foo" )
                                                            )
                                       )
                   ))
                   .toQuery();

I will close this ticket for now, as I provided a solution to your problem. If that's not an acceptable solution, please feel free to re-open this ticket.

( https://hibernate.atlassian.net/browse/HSEARCH-3889#add-comment?atlOrigin=eyJpIjoiYWU4Nzk5NGMxZmU4NDgxYmI5MDY5NDcyZjkzZWI1ZDMiLCJwIjoiaiJ9 ) Add Comment ( https://hibernate.atlassian.net/browse/HSEARCH-3889#add-comment?atlOrigin=eyJpIjoiYWU4Nzk5NGMxZmU4NDgxYmI5MDY5NDcyZjkzZWI1ZDMiLCJwIjoiaiJ9 )

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.core&referrer=utm_source%3DNotificationLink%26utm_medium%3DEmail ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailNotificationLink&mt=8 ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100124- sha1:185bad4 )
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/hibernate-issues/attachments/20200414/1cfb8a8a/attachment.html 


More information about the hibernate-issues mailing list