diff --git a/src/main/java/cc/ryanc/halo/service/PostService.java b/src/main/java/cc/ryanc/halo/service/PostService.java index 9625d9b9c..a0ab26ae0 100755 --- a/src/main/java/cc/ryanc/halo/service/PostService.java +++ b/src/main/java/cc/ryanc/halo/service/PostService.java @@ -7,6 +7,7 @@ import cc.ryanc.halo.model.dto.Archive; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.lang.NonNull; +import org.springframework.lang.Nullable; import org.springframework.web.bind.annotation.RequestParam; import java.util.Date; @@ -86,7 +87,7 @@ public interface PostService { * @return a page of posts */ @NonNull - Page searchPostsBy(String keyword, String postType, Integer postStatus, @NonNull Pageable pageable); + Page searchPostsBy(@Nullable String keyword, @Nullable String postType, @Nullable Integer postStatus, @NonNull Pageable pageable); /** diff --git a/src/main/java/cc/ryanc/halo/service/impl/PostServiceImpl.java b/src/main/java/cc/ryanc/halo/service/impl/PostServiceImpl.java index 5f0f0558e..e87c4f548 100755 --- a/src/main/java/cc/ryanc/halo/service/impl/PostServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/PostServiceImpl.java @@ -22,14 +22,21 @@ import org.springframework.cache.annotation.Cacheable; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.domain.Specification; +import org.springframework.data.jpa.domain.Specifications; import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; import org.springframework.stereotype.Service; +import org.springframework.util.Assert; import org.springframework.util.StringUtils; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; import java.util.*; +import static org.springframework.data.jpa.domain.Specification.where; + /** *
  *     文章业务逻辑实现类
@@ -575,26 +582,49 @@ public class PostServiceImpl implements PostService {
     }
 
     @NonNull
-    private Specification buildSearchSepcification(@Nullable String keyword, @Nullable String postType, @Nullable Integer postStatus) {
-        return (Specification) (root, criteriaQuery, criteriaBuilder) -> {
-            List predicates = new LinkedList<>();
-
-            if (StringUtils.hasText(keyword)) {
-                predicates.add(criteriaBuilder.like(root.get("postContent"), keyword));
-            }
-
-            if (StringUtils.hasText(postType)) {
-                predicates.add(criteriaBuilder.equal(root.get("postType"), postType));
-                predicates.add(criteriaBuilder.or(criteriaBuilder.like(root.get("postType"), postType)));
-            }
-
-            if (postStatus != null) {
-                predicates.add(criteriaBuilder.equal(root.get("postStatus"), postStatus));
-                predicates.add(criteriaBuilder.or(criteriaBuilder.like(root.get("postStatus"), postType)));
-            }
-
-            return criteriaQuery.where(predicates.toArray(new Predicate[0])).getRestriction();
-        };
+    private Specification buildSearchSepcification(@NonNull String keyword,
+                                                         @NonNull String postType,
+                                                         @NonNull Integer postStatus) {
+        return Specification.where(postTitleLike(keyword)).or(postContentLike(keyword)).and(postTypeEqual(postType)).and(postStatusEqual(postStatus));
+//        return (root, criteriaQuery, criteriaBuilder) -> {
+//            List predicates = new LinkedList<>();
+//
+//            if (StringUtils.hasText(keyword)) {
+//                predicates.add(criteriaBuilder.like(root.get("postContent"), keyword));
+//                predicates.add(criteriaBuilder.or(criteriaBuilder.like(root.get("postTitle"), keyword)));
+//            }
+//
+//            if (StringUtils.hasText(postType)) {
+//                predicates.add(criteriaBuilder.equal(root.get("postType"), postType));
+//            }
+//
+//            if (postStatus != null) {
+//                predicates.add(criteriaBuilder.equal(root.get("postStatus"), postStatus));
+//            }
+//
+//            return criteriaQuery.where(predicates.toArray(new Predicate[0])).getRestriction();
+//        };
     }
 
+    private Specification postContentLike(@NonNull String keyword) {
+        Assert.hasText(keyword, "Keyword must not be blank");
+
+        return (root, criteriaQuery, criteriaBuilder) ->
+                criteriaBuilder.like(criteriaBuilder.lower(root.get("postContent")), "%" + keyword.toLowerCase() + "%");
+    }
+
+    private Specification postTitleLike(@NonNull String keyword) {
+        Assert.hasText(keyword, "Keyword must not be blank");
+
+        return (root, criteriaQuery, criteriaBuilder) ->
+                criteriaBuilder.like(criteriaBuilder.lower(root.get("postTitle")), "%" + keyword.toLowerCase() + "%");
+    }
+
+    private Specification postTypeEqual(@NonNull String postType) {
+        return (root, criteriaQuery, criteriaBuilder) -> criteriaBuilder.equal(root.get("postType"), postType);
+    }
+
+    private Specification postStatusEqual(@NonNull Integer postStatus) {
+        return (root, criteriaQuery, criteriaBuilder) -> criteriaBuilder.equal(root.get("postStatus"), postStatus);
+    }
 }
diff --git a/src/main/java/cc/ryanc/halo/web/controller/admin/AttachmentController.java b/src/main/java/cc/ryanc/halo/web/controller/admin/AttachmentController.java
index 5c1aea970..65f550e81 100755
--- a/src/main/java/cc/ryanc/halo/web/controller/admin/AttachmentController.java
+++ b/src/main/java/cc/ryanc/halo/web/controller/admin/AttachmentController.java
@@ -8,12 +8,10 @@ import cc.ryanc.halo.model.enums.ResultCodeEnum;
 import cc.ryanc.halo.service.AttachmentService;
 import cc.ryanc.halo.service.LogsService;
 import cc.ryanc.halo.utils.LocaleMessageUtil;
-import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.util.StrUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Page;
-import org.springframework.data.domain.PageRequest;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.domain.Sort;
 import org.springframework.data.web.PageableDefault;
diff --git a/src/main/java/cc/ryanc/halo/web/controller/admin/PostController.java b/src/main/java/cc/ryanc/halo/web/controller/admin/PostController.java
index 63df22d47..6dd088705 100755
--- a/src/main/java/cc/ryanc/halo/web/controller/admin/PostController.java
+++ b/src/main/java/cc/ryanc/halo/web/controller/admin/PostController.java
@@ -110,7 +110,8 @@ public class PostController extends BaseController {
                              @RequestParam(value = "keyword") String keyword,
                              @PageableDefault(sort = "postId", direction = DESC) Pageable pageable) {
         try {
-            model.addAttribute("posts", postService.searchPosts(keyword, PostTypeEnum.POST_TYPE_POST.getDesc(), PostStatusEnum.PUBLISHED.getCode(), pageable));
+            Page posts = postService.searchPostsBy(keyword, PostTypeEnum.POST_TYPE_POST.getDesc(), PostStatusEnum.PUBLISHED.getCode(), pageable);
+            model.addAttribute("posts", posts);
         } catch (Exception e) {
             log.error("未知错误:{}", e.getMessage());
         }
diff --git a/src/main/java/cc/ryanc/halo/web/controller/front/FrontSearchController.java b/src/main/java/cc/ryanc/halo/web/controller/front/FrontSearchController.java
index dec652dfc..39908d23f 100644
--- a/src/main/java/cc/ryanc/halo/web/controller/front/FrontSearchController.java
+++ b/src/main/java/cc/ryanc/halo/web/controller/front/FrontSearchController.java
@@ -73,7 +73,10 @@ public class FrontSearchController extends BaseController {
             size = Integer.parseInt(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp()));
         }
         final Pageable pageable = PageRequest.of(page - 1, size, sort);
-        final Page posts = postService.searchPosts(HtmlUtil.escape(keyword), PostTypeEnum.POST_TYPE_POST.getDesc(), PostStatusEnum.PUBLISHED.getCode(), pageable);
+        final Page posts = postService.searchPostsBy(HtmlUtil.escape(keyword), PostTypeEnum.POST_TYPE_POST.getDesc(), PostStatusEnum.PUBLISHED.getCode(), pageable);
+
+        log.debug("Search posts result: [{}]", posts);
+
         final int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);
         model.addAttribute("is_search", true);
         model.addAttribute("keyword", keyword);
diff --git a/src/main/resources/application-dev.yaml b/src/main/resources/application-dev.yaml
index 5c8c0bea7..3f2d6f807 100755
--- a/src/main/resources/application-dev.yaml
+++ b/src/main/resources/application-dev.yaml
@@ -54,8 +54,4 @@ spring:
 
   # 多语言资源文件路径
   messages:
-    basename: i18n/messages
-logging:
-  file: ./logs/log.log
-
-
+    basename: i18n/messages
\ No newline at end of file