Fixed the bug of not being able to query according to the content (#1873)

The content table is associated at query time now.
pull/1907/head
yhc 2022-04-22 11:46:11 +08:00 committed by GitHub
parent de28811549
commit 1f75ba09f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 4 deletions

View File

@ -544,6 +544,8 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
* @param postQuery post query must not be null
* @return a post specification
*/
// CS304 issue: https://github.com/halo-dev/halo/issues/1842
@NonNull
private Specification<Post> buildSpecByQuery(@NonNull PostQuery postQuery) {
Assert.notNull(postQuery, "Post query must not be null");
@ -572,16 +574,21 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
}
if (postQuery.getKeyword() != null) {
// Format like condition
String likeCondition = String
.format("%%%s%%", StringUtils.strip(postQuery.getKeyword()));
// Build like predicate
Predicate titleLike = criteriaBuilder.like(root.get("title"), likeCondition);
Predicate originalContentLike = criteriaBuilder
.like(root.get("originalContent"), likeCondition);
Subquery<Post> postSubquery = query.subquery(Post.class);
Root<Content> contentRoot = postSubquery.from(Content.class);
postSubquery.select(contentRoot.get("id"))
.where(criteriaBuilder.like(contentRoot.get("originalContent"), likeCondition));
predicates.add(criteriaBuilder.or(titleLike, originalContentLike));
Predicate titleLike = criteriaBuilder.like(root.get("title"), likeCondition);
predicates.add(
criteriaBuilder.or(titleLike, criteriaBuilder.in(root).value(postSubquery)));
}
return query.where(predicates.toArray(new Predicate[0])).getRestriction();