简体   繁体   English

如何在 Hibernate 搜索查询中使用 QueryDSL BooleanExpression?

[英]How do I use a QueryDSL BooleanExpression in a Hibernate Search query?

I have a complex, reusable method that returns a BooleanExpression to filter a Q object QPost .我有一个复杂的、可重用的方法,它返回一个 BooleanExpression 来过滤 Q 对象QPost

How do I use this BooleanExpression in a Hibernate Search query like this:我如何在这样的 Hibernate 搜索查询中使用此 BooleanExpression:

Search.session(entityManager).search(Post.class)
                .where(f -> {
                    var boolStep = f.bool()
                            .must(f.match().fields("title", "text", "author.name").matching(searchText))
});

You mean an expression built using the QueryDSL project ?您的意思是使用QueryDSL 项目构建的表达式?

That project still uses Hibernate Search 5 , not 6, so currently you cannot use it with Hibernate Search 6.该项目仍然使用 Hibernate Search 5 ,而不是 6,因此目前您不能将它与 Hibernate Search 6 一起使用。

Perhaps you should create an issue on that project, and if you can, contribute the upgrade yourself?也许你应该在那个项目上创建一个问题,如果可以的话,自己贡献升级?

To use the BooleanExpression in the Hibernate Search query, you can pass it as an argument to the must or mustNot methods of the BoolFieldContext object.要在 Hibernate Search 查询中使用 BooleanExpression,您可以将其作为参数传递给 BoolFieldContext 对象的 must 或 mustNot 方法。

Here's an example of how you can do this:以下是如何执行此操作的示例:

Search.session(entityManager).search(Post.class)
            .where(f -> {
                var boolStep = f.bool()
                        .must(booleanExpression)
                        .must(f.match().fields("title", "text", "author.name").matching(searchText))

}); }); In this example, the booleanExpression is a BooleanExpression that you want to use to filter the query results.在此示例中,booleanExpression 是您要用于筛选查询结果的 BooleanExpression。 The must method adds a "must" clause to the Boolean query, meaning that the documents returned by the query must match the clause. must 方法向布尔查询添加一个“must”子句,意味着查询返回的文档必须与该子句匹配。 If you want to exclude documents that match the BooleanExpression, you can use the mustNot method instead.如果要排除与 BooleanExpression 匹配的文档,可以改用 mustNot 方法。

You can also combine multiple BooleanExpressions using the should method, which adds a "should" clause to the Boolean query.您还可以使用 should 方法组合多个 BooleanExpressions,这会向布尔查询添加一个“should”子句。 This means that the documents returned by the query may or may not match the clause, depending on the minimum number of "should" clauses that must match as specified by the minimumShouldMatch method.这意味着查询返回的文档可能匹配也可能不匹配子句,具体取决于 minimumShouldMatch 方法指定的必须匹配的“应该”子句的最小数量。

For example:例如:

Copy code复制代码

Search.session(entityManager).search(Post.class)
                .where(f -> {
                    var boolStep = f.bool()
                            .must(booleanExpression1)
                            .should(booleanExpression2)
                            .mustNot(booleanExpression3)
                            .minimumShouldMatch(1)
                            .must(f.match().fields("title", "text", "author.name").matching(searchText))
});

In this example, the query will return documents that match booleanExpression1 and booleanExpression3, and may also match booleanExpression2, as long as at least one of the "should" clauses matches.在此示例中,查询将返回匹配 booleanExpression1 和 booleanExpression3 的文档,并且也可能匹配 booleanExpression2,只要至少有一个“should”子句匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM