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