我们在 springboot 中使用 hibernate-search 已经有一段时间了。
有一个关于嵌套谓词用于更长的嵌套层次的问题...
直到 6.1.5 我们使用下面的语法来查询嵌套对象链:
List<Address> hits = searchSession
.search(Address.class)
.where(f ->
f
.nested().objectField("customer").nest(
f
.bool()
.must(
f
.nested().objectField("customer.wishLists").nest(
f.bool().must(
f.nested().objectField("customer.wishLists.products").nest(
f.match().field("customer.wishLists.products.title").matching("Nestle"))
)
)
)
)
)
.fetchHits(page.getPageNumber() * page.getPageSize(), page.getPageSize());
通过 6.2 的嵌套谓词改进,我们可以一步嵌套所有链:
例子:
List<Address> hits = searchSession
.search(Address.class)
.where((f, root) -> {
root.add(f.matchAll());
f.nested("customer.wishLists.products")
.add(
f.match().field("customer.wishLists.products.title").matching("Nestle")
);
})
.fetchHits(page.getPageNumber() * page.getPageSize(), page.getPageSize());
或者我们是否需要嵌套单个对象,例如:
List<Address> hits = searchSession
.search(Address.class)
.where((f, root) -> {
root.add(f.matchAll());
f.nested("customer")
.add(
f.nested("customer.wishLists")
.add(
f.nested("customer.wishLists.products")
.add(
f.match().field("customer.wishLists.products.title").matching("Nestle")
)
));
})
.fetchHits(page.getPageNumber() * page.getPageSize(), page.getPageSize());
在问题中解释