mirror of https://github.com/halo-dev/halo
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
parent
de28811549
commit
1f75ba09f8
|
@ -544,6 +544,8 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
||||||
* @param postQuery post query must not be null
|
* @param postQuery post query must not be null
|
||||||
* @return a post specification
|
* @return a post specification
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// CS304 issue: https://github.com/halo-dev/halo/issues/1842
|
||||||
@NonNull
|
@NonNull
|
||||||
private Specification<Post> buildSpecByQuery(@NonNull PostQuery postQuery) {
|
private Specification<Post> buildSpecByQuery(@NonNull PostQuery postQuery) {
|
||||||
Assert.notNull(postQuery, "Post query must not be null");
|
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) {
|
if (postQuery.getKeyword() != null) {
|
||||||
|
|
||||||
// Format like condition
|
// Format like condition
|
||||||
String likeCondition = String
|
String likeCondition = String
|
||||||
.format("%%%s%%", StringUtils.strip(postQuery.getKeyword()));
|
.format("%%%s%%", StringUtils.strip(postQuery.getKeyword()));
|
||||||
|
|
||||||
// Build like predicate
|
// Build like predicate
|
||||||
Predicate titleLike = criteriaBuilder.like(root.get("title"), likeCondition);
|
Subquery<Post> postSubquery = query.subquery(Post.class);
|
||||||
Predicate originalContentLike = criteriaBuilder
|
Root<Content> contentRoot = postSubquery.from(Content.class);
|
||||||
.like(root.get("originalContent"), likeCondition);
|
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();
|
return query.where(predicates.toArray(new Predicate[0])).getRestriction();
|
||||||
|
|
Loading…
Reference in New Issue