支持文章检索

pull/78/head
ruibaby 2019-01-11 22:01:58 +08:00
parent 24846b0089
commit f7533f4d7d
8 changed files with 174 additions and 148 deletions

View File

@ -34,30 +34,31 @@ public interface PostRepository extends JpaRepository<Post, Long> {
*
*
* @param postType post or page
*
* @return List
*/
List<Post> findPostsByPostType(String postType);
/**
*
*
* @param postType post or page
* @param pageable
*
* @return Page
*/
Page<Post> findPostsByPostType(String postType, Pageable pageable);
/**
*
*
* @param keyWord keyword
* @param postType0 postType0
* @param postStatus0 postStatus0
* @param postTitle postTitle
* @param postType1 postType1
* @param postStatus1 postStatus1
* @param postContent postContent
* @param pageable pageable
*
* @return List
* @return Page
*/
List<Post> findByPostTitleLike(String keyWord, Pageable pageable);
Page<Post> findByPostTypeAndPostStatusAndPostTitleLikeOrPostTypeAndPostStatusAndPostContentLike(
String postType0,
Integer postStatus0,
String postTitle,
String postType1,
Integer postStatus1,
String postContent,
Pageable pageable
);
/**
*
@ -65,7 +66,6 @@ public interface PostRepository extends JpaRepository<Post, Long> {
* @param status 012
* @param postType post or page
* @param pageable
*
* @return Page
*/
Page<Post> findPostsByPostStatusAndPostType(Integer status, String postType, Pageable pageable);
@ -75,7 +75,6 @@ public interface PostRepository extends JpaRepository<Post, Long> {
*
* @param status 0,1,2
* @param postType post or page
*
* @return List
*/
List<Post> findPostsByPostStatusAndPostType(Integer status, String postType);
@ -85,7 +84,6 @@ public interface PostRepository extends JpaRepository<Post, Long> {
*
* @param postUrl
* @param postType post or page
*
* @return Post
*/
Post findPostByPostUrlAndPostType(String postUrl, String postType);
@ -95,7 +93,6 @@ public interface PostRepository extends JpaRepository<Post, Long> {
*
* @param postId
* @param postType post or page
*
* @return Post
*/
Post findPostByPostIdAndPostType(Long postId, String postType);
@ -104,21 +101,19 @@ public interface PostRepository extends JpaRepository<Post, Long> {
*
*
* @param postDate
*
* @return Post
*/
@Query(value = "SELECT * FROM halo_post WHERE post_status = 0 AND post_type='post' AND post_date < :postDate ORDER BY post_date DESC LIMIT 1", nativeQuery = true)
Post queryPrePost(Date postDate);
Post queryPrePost(@Param("postDate") Date postDate);
/**
*
*
* @param postDate
*
* @return Post
*/
@Query(value = "SELECT * FROM halo_post WHERE post_status = 0 AND post_type='post' AND post_date > :postDate ORDER BY post_date ASC LIMIT 1", nativeQuery = true)
Post queryNextPost(Date postDate);
Post queryNextPost(@Param("postDate") Date postDate);
/**
*
@ -139,7 +134,6 @@ public interface PostRepository extends JpaRepository<Post, Long> {
/**
* @return List
*
* @Author Aquan
* @Description
* @Date 2019.1.4 11:19
@ -150,7 +144,6 @@ public interface PostRepository extends JpaRepository<Post, Long> {
/**
* @return Integer
*
* @Author Aquan
* @Description
* @Date 2019.1.4 15:03
@ -165,7 +158,6 @@ public interface PostRepository extends JpaRepository<Post, Long> {
*
* @param year year
* @param month month
*
* @return List
*/
@Query(value = "SELECT *,YEAR(post_date) AS YEAR,MONTH(post_date) AS MONTH FROM halo_post WHERE post_status=0 and post_type='post' AND YEAR(post_date)=:year AND MONTH(post_date)=:month ORDER BY post_date DESC", nativeQuery = true)
@ -175,7 +167,6 @@ public interface PostRepository extends JpaRepository<Post, Long> {
*
*
* @param year year
*
* @return List
*/
@Query(value = "SELECT *,YEAR(post_date) AS YEAR FROM halo_post WHERE post_status=0 AND post_type='post' AND YEAR(post_date)=:year ORDER BY post_date DESC", nativeQuery = true)
@ -187,7 +178,6 @@ public interface PostRepository extends JpaRepository<Post, Long> {
* @param year year
* @param month month
* @param pageable pageable
*
* @return Page
*/
@Query(value = "SELECT * FROM halo_post WHERE post_status=0 and post_type='post' AND YEAR(post_date)=:year AND MONTH(post_date)=:month ORDER BY post_date DESC", countQuery = "SELECT COUNT(*) FROM halo_post WHERE post_status=0 AND YEAR(post_date)=:year AND MONTH(post_date)=:month", nativeQuery = true)
@ -199,7 +189,6 @@ public interface PostRepository extends JpaRepository<Post, Long> {
* @param category category
* @param status status
* @param pageable pageable
*
* @return Page
*/
Page<Post> findPostByCategoriesAndPostStatus(Category category, Integer status, Pageable pageable);
@ -210,7 +199,6 @@ public interface PostRepository extends JpaRepository<Post, Long> {
* @param tag tag
* @param status status
* @param pageable pageable
*
* @return Page
*/
Page<Post> findPostsByTagsAndPostStatus(Tag tag, Integer status, Pageable pageable);
@ -219,27 +207,14 @@ public interface PostRepository extends JpaRepository<Post, Long> {
*
*
* @param tag tag
*
* @return List
*/
List<Post> findPostsByTags(Tag tag);
/**
*
*
* @param keyword
* @param pageable
*
* @return Page
*/
@Query(value = "SELECT * FROM halo_post WHERE post_status = 0 AND post_type='post' AND post_title LIKE '%=:keyword%' OR post_content LIKE '%=:keyword%'", nativeQuery = true)
Page<Post> findPostByPostTitleLikeOrPostContentLikeAndPostTypeAndPostStatus(String keyword, Pageable pageable);
/**
*
*
* @param postStatus
*
* @return List<Post>
*/
List<Post> findPostsByPostTypeOrderByPostViewsDesc(String postStatus);
@ -257,7 +232,6 @@ public interface PostRepository extends JpaRepository<Post, Long> {
*
* @param status
* @param postType
*
* @return
*/
Integer countAllByPostStatusAndPostType(Integer status, String postType);
@ -266,7 +240,6 @@ public interface PostRepository extends JpaRepository<Post, Long> {
*
*
* @param limit
*
* @return List
*/
@Query(value = "SELECT * FROM halo_post WHERE post_status = 0 AND post_type = 'post' ORDER BY post_date DESC LIMIT :limit", nativeQuery = true)

View File

@ -26,7 +26,6 @@ public interface PostService {
*
*
* @param post Post
*
* @return Post
*/
Post save(Post post);
@ -35,7 +34,6 @@ public interface PostService {
*
*
* @param postId postId
*
* @return Post
*/
Post remove(Long postId);
@ -45,7 +43,6 @@ public interface PostService {
*
* @param postId postId
* @param status status
*
* @return Post
*/
Post updatePostStatus(Long postId, Integer status);
@ -61,7 +58,6 @@ public interface PostService {
*
*
* @param postType post or page
*
* @return List
*/
List<Post> findAll(String postType);
@ -69,12 +65,13 @@ public interface PostService {
/**
*
*
* @param keyWord keyword
* @param pageable pageable
*
* @return List
* @param keyword
* @param postType
* @param postStatus
* @param pageable
* @return Page
*/
List<Post> searchPosts(String keyWord, Pageable pageable);
Page<Post> searchPosts(String keyword, String postType, Integer postStatus, Pageable pageable);
/**
*
@ -82,7 +79,6 @@ public interface PostService {
* @param status 012
* @param postType post or page
* @param pageable
*
* @return Page
*/
Page<Post> findPostByStatus(Integer status, String postType, Pageable pageable);
@ -91,7 +87,6 @@ public interface PostService {
*
*
* @param pageable pageable
*
* @return Page
*/
Page<Post> findPostByStatus(Pageable pageable);
@ -101,7 +96,6 @@ public interface PostService {
*
* @param status 012
* @param postType post or page
*
* @return List
*/
List<Post> findPostByStatus(Integer status, String postType);
@ -110,7 +104,6 @@ public interface PostService {
*
*
* @param postId postId
*
* @return Post
*/
Optional<Post> findByPostId(Long postId);
@ -120,7 +113,6 @@ public interface PostService {
*
* @param postId postId
* @param postType postType
*
* @return Post
*/
Post findByPostId(Long postId, String postType);
@ -130,7 +122,6 @@ public interface PostService {
*
* @param postUrl
* @param postType post or page
*
* @return Post
*/
Post findByPostUrl(String postUrl, String postType);
@ -146,7 +137,6 @@ public interface PostService {
*
*
* @param postDate postDate
*
* @return Post
*/
Post getNextPost(Date postDate);
@ -155,7 +145,6 @@ public interface PostService {
*
*
* @param postDate postDate
*
* @return Post
*/
Post getPrePost(Date postDate);
@ -176,7 +165,6 @@ public interface PostService {
/**
* @return List
*
* @Author Aquan
* @Description
* @Date 2019.1.4 11:14
@ -190,7 +178,6 @@ public interface PostService {
*
* @param year year
* @param month month
*
* @return List
*/
List<Post> findPostByYearAndMonth(String year, String month);
@ -201,7 +188,6 @@ public interface PostService {
* @param year year
* @param month month
* @param pageable pageable
*
* @return Page
*/
Page<Post> findPostByYearAndMonth(String year, String month, Pageable pageable);
@ -210,7 +196,6 @@ public interface PostService {
*
*
* @param year year
*
* @return List
*/
List<Post> findPostByYear(String year);
@ -220,7 +205,6 @@ public interface PostService {
*
* @param category category
* @param pageable pageable
*
* @return Page
*/
Page<Post> findPostByCategories(Category category, Pageable pageable);
@ -230,21 +214,10 @@ public interface PostService {
*
* @param tag tag
* @param pageable pageable
*
* @return Page
*/
Page<Post> findPostsByTags(Tag tag, Pageable pageable);
/**
*
*
* @param keyword
* @param pageable
*
* @return Page
*/
Page<Post> searchByKeywords(String keyword, Pageable pageable);
/**
*
*
@ -256,7 +229,6 @@ public interface PostService {
*
*
* @param post post
*
* @return List
*/
List<Post> relatedPosts(Post post);
@ -272,7 +244,6 @@ public interface PostService {
*
*
* @param status
*
* @return
*/
Integer getCountByStatus(Integer status);
@ -281,7 +252,6 @@ public interface PostService {
* rss
*
* @param posts posts
*
* @return String
*/
String buildRss(List<Post> posts);
@ -290,7 +260,6 @@ public interface PostService {
* sitemap
*
* @param posts posts
*
* @return String
*/
String buildSiteMap(List<Post> posts);
@ -308,7 +277,6 @@ public interface PostService {
* @param post post
* @param cateList cateList
* @param tagList tagList
*
* @return Post Post
*/
Post buildCategoriesAndTags(Post post, List<String> cateList, @RequestParam("tagList") String tagList);
@ -317,7 +285,6 @@ public interface PostService {
*
*
* @param limit
*
* @return List
*/
List<Post> getRecentPosts(int limit);

View File

@ -57,7 +57,6 @@ public class PostServiceImpl implements PostService {
*
*
* @param post Post
*
* @return Post
*/
@Override
@ -82,7 +81,6 @@ public class PostServiceImpl implements PostService {
*
*
* @param postId postId
*
* @return Post
*/
@Override
@ -98,7 +96,6 @@ public class PostServiceImpl implements PostService {
*
* @param postId postId
* @param status status
*
* @return Post
*/
@Override
@ -133,7 +130,6 @@ public class PostServiceImpl implements PostService {
*
*
* @param postType post or page
*
* @return List
*/
@Override
@ -145,14 +141,23 @@ public class PostServiceImpl implements PostService {
/**
*
*
* @param keyWord keyword
* @param pageable pageable
*
* @return List
* @param keyword
* @param postType
* @param postStatus
* @param pageable
* @return Page
*/
@Override
public List<Post> searchPosts(String keyWord, Pageable pageable) {
return postRepository.findByPostTitleLike(keyWord, pageable);
public Page<Post> searchPosts(String keyword, String postType, Integer postStatus, Pageable pageable) {
return postRepository.findByPostTypeAndPostStatusAndPostTitleLikeOrPostTypeAndPostStatusAndPostContentLike(
postType,
postStatus,
"%" + keyword + "%",
postType,
postStatus,
"%" + keyword + "%",
pageable
);
}
/**
@ -161,7 +166,6 @@ public class PostServiceImpl implements PostService {
* @param status 012
* @param postType post or page
* @param pageable
*
* @return Page
*/
@Override
@ -173,7 +177,6 @@ public class PostServiceImpl implements PostService {
*
*
* @param pageable pageable
*
* @return Page
*/
@Override
@ -187,7 +190,6 @@ public class PostServiceImpl implements PostService {
*
* @param status 012
* @param postType post or page
*
* @return List
*/
@Override
@ -200,7 +202,6 @@ public class PostServiceImpl implements PostService {
*
*
* @param postId postId
*
* @return Optional
*/
@Override
@ -212,7 +213,6 @@ public class PostServiceImpl implements PostService {
*
*
* @param postId postId
*
* @return Post
*/
@Override
@ -225,7 +225,6 @@ public class PostServiceImpl implements PostService {
*
* @param postUrl
* @param postType post or page
*
* @return Post
*/
@Override
@ -249,7 +248,6 @@ public class PostServiceImpl implements PostService {
*
*
* @param postDate postDate
*
* @return Post
*/
@Override
@ -261,7 +259,6 @@ public class PostServiceImpl implements PostService {
*
*
* @param postDate postDate
*
* @return Post
*/
@Override
@ -314,7 +311,6 @@ public class PostServiceImpl implements PostService {
/**
* @return List
*
* @Author Aquan
* @Description
* @Date 2019.1.4 11:16
@ -341,7 +337,6 @@ public class PostServiceImpl implements PostService {
*
* @param year year
* @param month month
*
* @return List
*/
@Override
@ -354,7 +349,6 @@ public class PostServiceImpl implements PostService {
*
*
* @param year year
*
* @return List
*/
@Override
@ -369,7 +363,6 @@ public class PostServiceImpl implements PostService {
* @param year year year
* @param month month month
* @param pageable pageable pageable
*
* @return Page
*/
@Override
@ -383,7 +376,6 @@ public class PostServiceImpl implements PostService {
* @param category category
* @param status status
* @param pageable pageable
*
* @return Page
*/
@Override
@ -398,7 +390,6 @@ public class PostServiceImpl implements PostService {
* @param tag tag
* @param status status
* @param pageable pageable
*
* @return Page
*/
@Override
@ -407,19 +398,6 @@ public class PostServiceImpl implements PostService {
return postRepository.findPostsByTagsAndPostStatus(tag, PostStatusEnum.PUBLISHED.getCode(), pageable);
}
/**
*
*
* @param keyword
* @param pageable
*
* @return Page
*/
@Override
public Page<Post> searchByKeywords(String keyword, Pageable pageable) {
return postRepository.findPostByPostTitleLikeOrPostContentLikeAndPostTypeAndPostStatus(keyword, pageable);
}
/**
*
*
@ -435,7 +413,6 @@ public class PostServiceImpl implements PostService {
*
*
* @param post post
*
* @return List
*/
@Override
@ -473,7 +450,6 @@ public class PostServiceImpl implements PostService {
*
*
* @param status
*
* @return
*/
@Override
@ -485,7 +461,6 @@ public class PostServiceImpl implements PostService {
* rss
*
* @param posts posts
*
* @return String
*/
@Override
@ -503,7 +478,6 @@ public class PostServiceImpl implements PostService {
* sitemap
*
* @param posts posts
*
* @return String
*/
@Override
@ -531,7 +505,6 @@ public class PostServiceImpl implements PostService {
* @param post post
* @param cateList cateList
* @param tagList tagList
*
* @return Post Post
*/
@Override
@ -549,7 +522,6 @@ public class PostServiceImpl implements PostService {
*
*
* @param limit
*
* @return List
*/
@Override

View File

@ -18,7 +18,6 @@ import cc.ryanc.halo.web.controller.core.BaseController;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HtmlUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
@ -121,7 +120,7 @@ public class PostController extends BaseController {
//排序规则
final Sort sort = new Sort(Sort.Direction.DESC, "postId");
final Pageable pageable = PageRequest.of(page, size, sort);
model.addAttribute("posts", postService.searchPosts(keyword, pageable));
model.addAttribute("posts", postService.searchPosts(keyword,PostTypeEnum.POST_TYPE_POST.getDesc(),PostStatusEnum.PUBLISHED.getCode(),pageable));
} catch (Exception e) {
log.error("未知错误:{}", e.getMessage());
}

View File

@ -18,7 +18,6 @@ import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* <pre>
@ -45,7 +44,6 @@ public class FrontIndexController extends BaseController {
*/
@GetMapping
public String index(Model model) {
//调用方法渲染首页
return this.index(model, 1);
}
@ -73,23 +71,9 @@ public class FrontIndexController extends BaseController {
return this.renderNotFound();
}
final int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);
model.addAttribute("is_index",true);
model.addAttribute("is_index", true);
model.addAttribute("posts", posts);
model.addAttribute("rainbow", rainbow);
return this.render("index");
}
/**
*
*
* @param keyword keyword
* @param model model
* @return /themes/{theme}/index
*/
@GetMapping(value = "search")
public String search(@RequestParam("keyword") String keyword, Model model) {
final Page<Post> posts = postService.searchByKeywords(keyword, null);
model.addAttribute("posts", posts);
return this.render("index");
}
}

View File

@ -0,0 +1,80 @@
package cc.ryanc.halo.web.controller.front;
import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.model.enums.BlogPropertiesEnum;
import cc.ryanc.halo.model.enums.PostStatusEnum;
import cc.ryanc.halo.model.enums.PostTypeEnum;
import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.web.controller.core.BaseController;
import cn.hutool.core.util.PageUtil;
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.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* <pre>
*
* </pre>
*
* @author : RYAN0UP
* @date : 2019/1/11
*/
@Slf4j
@Controller
@RequestMapping(value = "/search")
public class FrontSearchController extends BaseController {
@Autowired
private PostService postService;
/**
*
*
* @param model model
* @param keyword
* @return /themes/{theme}/search
*/
@GetMapping
public String search(Model model,
@RequestParam(value = "keyword") String keyword) {
return this.search(model, keyword, 1);
}
/**
*
*
* @param model model
* @param keyword
* @param page
* @return /themes/{theme}/search
*/
@GetMapping(value = "page/{page}")
public String search(Model model,
@RequestParam(value = "keyword") String keyword,
@PathVariable(value = "page") Integer page) {
final Sort sort = new Sort(Sort.Direction.DESC, "postDate");
int size = 10;
if (StrUtil.isNotBlank(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp()))) {
size = Integer.parseInt(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp()));
}
final Pageable pageable = PageRequest.of(page - 1, size, sort);
final Page<Post> posts = postService.searchPosts(keyword,PostTypeEnum.POST_TYPE_POST.getDesc(),PostStatusEnum.PUBLISHED.getCode(),pageable);
final int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);
model.addAttribute("is_search", true);
model.addAttribute("keyword", keyword);
model.addAttribute("posts", posts);
model.addAttribute("rainbow", rainbow);
return this.render("search");
}
}

View File

@ -1,4 +1,3 @@
<div class="mdl-textfield mdl-js-textfield mdl-textfield--expandable" method="post" action="">
<label id="search-label" class="mdl-button mdl-js-ripple-effect mdl-js-button mdl-button--fab mdl-color--accent mdl-shadow--4dp" for="search">
<i class="material-icons mdl-color-text--white" role="presentation">search</i>

View File

@ -0,0 +1,52 @@
<#include "module/macro.ftl">
<@layout title="搜索:${keyword} | ${options.blog_title!'Material'}" keywords="${options.seo_keywords!'Material'}}" description="${options.seo_desc!'Material'}}">
<!-- Index Module -->
<div class="material-index mdl-grid">
<#if (options.theme_material_scheme!'Paradox') == "Paradox" && posts.number==0 && !isArchives??>
<!-- Paradox Header -->
<#include "_partial/daily_pic.ftl">
<#include "_partial/blog_info.ftl">
</#if>
<div class="locate-thumbnail-symbol"></div>
<!-- Pin on top -->
<!-- Normal Post -->
<#if (options.theme_material_scheme!'Paradox') == "Paradox">
<!-- Paradox Thumbnail -->
<#include "_partial/Paradox-post_entry.ftl">
<#else>
<!-- Isolation Thumbnail -->
<#include "_partial/Isolation-post_entry.ftl">
</#if>
<#if posts.totalPages gt 1 >
<nav class="material-nav mdl-cell mdl-cell--12-col">
<#if posts.hasPrevious()>
<#if posts.number == 1>
<a class="extend prev" rel="prev" href="/search?keyword=${keyword}">
<button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon">
<i class="material-icons" role="presentation">arrow_back</i>
</button>
</a>
<#else >
<a class="extend prev" rel="prev" href="/search/page/${posts.number}?keyword=${keyword}">
<button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon">
<i class="material-icons" role="presentation">arrow_back</i>
</button>
</a>
</#if>
</#if>
<span class="page-number current">${posts.number+1}</span>
<#if posts.hasNext()>
<a class="extend next" rel="next" href="/search/page/${posts.number+2}?keyword=${keyword}">
<button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon">
<i class="material-icons" role="presentation">arrow_forward</i>
</button>
</a>
</#if>
</nav>
</#if>
<#if (options.theme_material_scheme!'Paradox') == "Paradox">
<#include "_partial/Paradox-post_entry-thumbnail.ftl">
</#if>
</div>
</@layout>