mirror of
https://github.com/201206030/novel-cloud.git
synced 2025-07-13 21:56:38 +00:00
搜索服务完成
This commit is contained in:
@ -14,7 +14,7 @@
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.java2nb.novel</groupId>
|
||||
<artifactId>novel-common</artifactId>
|
||||
<artifactId>book-api</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
@ -49,5 +49,15 @@
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
|
||||
</project>
|
@ -3,6 +3,7 @@ package com.java2nb.novel;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
/**
|
||||
* 搜索微服务启动器
|
||||
@ -12,6 +13,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableFeignClients
|
||||
@EnableScheduling
|
||||
public class SearchApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
@ -0,0 +1,41 @@
|
||||
package com.java2nb.novel.search.controller;
|
||||
|
||||
import com.java2nb.novel.common.bean.PageBean;
|
||||
import com.java2nb.novel.common.bean.ResultBean;
|
||||
import com.java2nb.novel.search.service.SearchService;
|
||||
import com.java2nb.novel.search.vo.EsBookVO;
|
||||
import com.java2nb.novel.search.vo.SearchParamVO;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import io.swagger.annotations.ApiParam;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("user")
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
@Api(tags = "搜索相关接口")
|
||||
|
||||
public class SearchController {
|
||||
|
||||
private final SearchService searchService;
|
||||
|
||||
/**
|
||||
* 分页搜索小说列表接口
|
||||
* */
|
||||
@ApiOperation("分页搜索小说列表接口")
|
||||
@GetMapping("searchByPage")
|
||||
public ResultBean<PageBean<EsBookVO>> searchByPage(SearchParamVO paramVO, @ApiParam("查询页码") @RequestParam(value = "curr", defaultValue = "1") int page,@ApiParam("每页大小") @RequestParam(value = "limit", defaultValue = "20") int pageSize){
|
||||
PageBean<EsBookVO> pageBean = searchService.searchBook(paramVO,page,pageSize);
|
||||
return ResultBean.ok(pageBean);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.java2nb.novel.search.feign;
|
||||
|
||||
|
||||
import com.java2nb.novel.book.api.BookApi;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
|
||||
/**
|
||||
* 小说服务Feign客户端
|
||||
* @author xiongxiaoyang
|
||||
* @version 1.0
|
||||
* @since 2020/5/28
|
||||
*/
|
||||
@FeignClient(value = "book-service")
|
||||
public interface BookFeignClient extends BookApi {
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
package com.java2nb.novel.search.schedule;
|
||||
|
||||
import com.java2nb.novel.book.entity.Book;
|
||||
import com.java2nb.novel.common.cache.CacheKey;
|
||||
import com.java2nb.novel.common.cache.CacheService;
|
||||
import com.java2nb.novel.search.feign.BookFeignClient;
|
||||
import com.java2nb.novel.search.service.SearchService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 小说导入搜索引擎
|
||||
*
|
||||
* @author Administrator
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class BookToEsSchedule {
|
||||
|
||||
private final BookFeignClient bookFeignClient;
|
||||
|
||||
private final CacheService cacheService;
|
||||
|
||||
|
||||
|
||||
private final SearchService searchService;
|
||||
|
||||
|
||||
/**
|
||||
* 1分钟导入一次
|
||||
*/
|
||||
@Scheduled(fixedRate = 1000 * 60)
|
||||
public void saveToEs() {
|
||||
//TODO 引入Redisson框架实现分布式锁
|
||||
//可以重复更新,只是效率可能略有降低,所以暂不实现分布式锁
|
||||
if (cacheService.get(CacheKey.ES_TRANS_LOCK) == null) {
|
||||
cacheService.set(CacheKey.ES_TRANS_LOCK, "1", 60 * 20);
|
||||
try {
|
||||
//查询需要更新的小说
|
||||
Date lastDate = (Date) cacheService.getObject(CacheKey.ES_LAST_UPDATE_TIME);
|
||||
if (lastDate == null) {
|
||||
lastDate = new SimpleDateFormat("yyyy-MM-dd").parse("2020-01-01");
|
||||
}
|
||||
|
||||
|
||||
List<Book> books = bookFeignClient.queryBookByMinUpdateTime(lastDate, 100);
|
||||
for (Book book : books) {
|
||||
searchService.importToEs(book);
|
||||
lastDate = book.getUpdateTime();
|
||||
Thread.sleep(5000);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
cacheService.setObject(CacheKey.ES_LAST_UPDATE_TIME, lastDate);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
cacheService.del(CacheKey.ES_TRANS_LOCK);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.java2nb.novel.search.service;
|
||||
|
||||
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import com.java2nb.novel.book.entity.Book;
|
||||
import com.java2nb.novel.common.bean.PageBean;
|
||||
import com.java2nb.novel.search.vo.EsBookVO;
|
||||
import com.java2nb.novel.search.vo.SearchParamVO;
|
||||
|
||||
|
||||
/**
|
||||
* 搜索服务接口
|
||||
* @author xiongxiaoyang
|
||||
* @version 1.0
|
||||
* @since 2020/5/28
|
||||
*/
|
||||
public interface SearchService {
|
||||
|
||||
/**
|
||||
* 导入到es
|
||||
* @param book 小说数据
|
||||
*/
|
||||
void importToEs(Book book);
|
||||
|
||||
/**
|
||||
* 搜索
|
||||
* @param params 搜索参数
|
||||
* @param page 当前页码
|
||||
* @param pageSize 每页大小
|
||||
* @return 分页信息
|
||||
*/
|
||||
PageBean<EsBookVO> searchBook(SearchParamVO params, int page, int pageSize);
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,212 @@
|
||||
package com.java2nb.novel.search.service.impl;
|
||||
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import com.java2nb.novel.book.entity.Book;
|
||||
import com.java2nb.novel.common.bean.PageBean;
|
||||
import com.java2nb.novel.common.enums.ResponseStatus;
|
||||
import com.java2nb.novel.common.exception.BusinessException;
|
||||
import com.java2nb.novel.common.utils.StringUtil;
|
||||
import com.java2nb.novel.search.service.SearchService;
|
||||
import com.java2nb.novel.search.vo.EsBookVO;
|
||||
import com.java2nb.novel.search.vo.SearchParamVO;
|
||||
import io.searchbox.client.JestClient;
|
||||
import io.searchbox.core.*;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
||||
import org.elasticsearch.search.sort.SortOrder;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 搜索服务接口实现类
|
||||
* @author xiongxiaoyang
|
||||
* @version 1.0
|
||||
* @since 2020/5/28
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class SearchServiceImpl implements SearchService {
|
||||
|
||||
private final String INDEX = "novel";
|
||||
|
||||
private final String TYPE = "book";
|
||||
|
||||
private final JestClient jestClient;
|
||||
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public void importToEs(Book book) {
|
||||
|
||||
//导入到ES
|
||||
EsBookVO esBookVO = new EsBookVO();
|
||||
BeanUtils.copyProperties(book, esBookVO, "lastIndexUpdateTime");
|
||||
esBookVO.setLastIndexUpdateTime(new SimpleDateFormat("yyyy/MM/dd HH:mm").format(book.getLastIndexUpdateTime()));
|
||||
Index action = new Index.Builder(esBookVO).index(INDEX).type(TYPE).id(book.getId().toString()).build();
|
||||
|
||||
jestClient.execute(action);
|
||||
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public PageBean<EsBookVO> searchBook(SearchParamVO params, int page, int pageSize) {
|
||||
|
||||
if (params.getUpdatePeriod() != null) {
|
||||
long cur = System.currentTimeMillis();
|
||||
long period = params.getUpdatePeriod() * 24 * 3600 * 1000;
|
||||
long time = cur - period;
|
||||
params.setUpdateTimeMin(new Date(time));
|
||||
}
|
||||
|
||||
List<EsBookVO> bookList = new ArrayList<>(0);
|
||||
|
||||
//使用搜索引擎搜索
|
||||
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
||||
// 构造查询哪个字段
|
||||
if (StringUtils.isNoneBlank(params.getKeyword())) {
|
||||
boolQueryBuilder = boolQueryBuilder.must(QueryBuilders.queryStringQuery(params.getKeyword()));
|
||||
}
|
||||
|
||||
// 作品方向
|
||||
if (params.getWorkDirection() != null) {
|
||||
boolQueryBuilder.filter(QueryBuilders.termQuery("workDirection", params.getWorkDirection()));
|
||||
}
|
||||
|
||||
// 分类
|
||||
if (params.getCatId() != null) {
|
||||
boolQueryBuilder.filter(QueryBuilders.termQuery("catId", params.getCatId()));
|
||||
}
|
||||
if (params.getBookStatus() != null) {
|
||||
boolQueryBuilder.filter(QueryBuilders.termQuery("bookStatus", params.getBookStatus()));
|
||||
}
|
||||
|
||||
if (params.getWordCountMin() == null) {
|
||||
params.setWordCountMin(0);
|
||||
}
|
||||
if (params.getWordCountMax() == null) {
|
||||
params.setWordCountMax(Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
boolQueryBuilder.filter(QueryBuilders.rangeQuery("wordCount").gte(params.getWordCountMin()).lte(params.getWordCountMax()));
|
||||
|
||||
if (params.getUpdateTimeMin() != null) {
|
||||
boolQueryBuilder.filter(QueryBuilders.rangeQuery("lastIndexUpdateTime").gte(params.getUpdateTimeMin()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
||||
searchSourceBuilder.query(boolQueryBuilder);
|
||||
|
||||
|
||||
Count count = new Count.Builder().addIndex(INDEX).addType(TYPE)
|
||||
.query(searchSourceBuilder.toString()).build();
|
||||
CountResult results = jestClient.execute(count);
|
||||
Double total = results.getCount();
|
||||
|
||||
|
||||
// 高亮字段
|
||||
HighlightBuilder highlightBuilder = new HighlightBuilder();
|
||||
highlightBuilder.field("authorName");
|
||||
highlightBuilder.field("bookName");
|
||||
highlightBuilder.field("bookDesc");
|
||||
highlightBuilder.field("lastIndexName");
|
||||
highlightBuilder.field("catName");
|
||||
highlightBuilder.preTags("<span style='color:red'>").postTags("</span>");
|
||||
highlightBuilder.fragmentSize(20000);
|
||||
searchSourceBuilder.highlighter(highlightBuilder);
|
||||
|
||||
|
||||
//设置排序
|
||||
if (params.getSort() != null) {
|
||||
searchSourceBuilder.sort(StringUtil.camelName(params.getSort()), SortOrder.DESC);
|
||||
}
|
||||
|
||||
// 设置分页
|
||||
searchSourceBuilder.from((page - 1) * pageSize);
|
||||
searchSourceBuilder.size(pageSize);
|
||||
|
||||
// 构建Search对象
|
||||
Search search = new Search.Builder(searchSourceBuilder.toString()).addIndex(INDEX).addType(TYPE).build();
|
||||
log.debug(search.toString());
|
||||
SearchResult result;
|
||||
result = jestClient.execute(search);
|
||||
if (result.isSucceeded()) {
|
||||
log.debug(result.getJsonString());
|
||||
|
||||
Map resultMap = new ObjectMapper().readValue(result.getJsonString(), Map.class);
|
||||
if (resultMap.get("hits") != null) {
|
||||
Map hitsMap = (Map) resultMap.get("hits");
|
||||
if (hitsMap.size() > 0 && hitsMap.get("hits") != null) {
|
||||
List hitsList = (List) hitsMap.get("hits");
|
||||
if (hitsList.size() > 0 && result.getSourceAsString() != null) {
|
||||
|
||||
JavaType jt = new ObjectMapper().getTypeFactory().constructParametricType(ArrayList.class, EsBookVO.class);
|
||||
bookList = new ObjectMapper().readValue("[" + result.getSourceAsString() + "]", jt);
|
||||
|
||||
if (bookList != null) {
|
||||
for (int i = 0; i < bookList.size(); i++) {
|
||||
hitsMap = (Map) hitsList.get(i);
|
||||
Map highlightMap = (Map) hitsMap.get("highlight");
|
||||
if (highlightMap != null && highlightMap.size() > 0) {
|
||||
|
||||
List<String> authorNameList = (List<String>) highlightMap.get("authorName");
|
||||
if (authorNameList != null && authorNameList.size() > 0) {
|
||||
bookList.get(i).setAuthorName(authorNameList.get(0));
|
||||
}
|
||||
|
||||
List<String> bookNameList = (List<String>) highlightMap.get("bookName");
|
||||
if (bookNameList != null && bookNameList.size() > 0) {
|
||||
bookList.get(i).setBookName(bookNameList.get(0));
|
||||
}
|
||||
|
||||
List<String> bookDescList = (List<String>) highlightMap.get("bookDesc");
|
||||
if (bookDescList != null && bookDescList.size() > 0) {
|
||||
bookList.get(i).setBookDesc(bookDescList.get(0));
|
||||
}
|
||||
|
||||
List<String> lastIndexNameList = (List<String>) highlightMap.get("lastIndexName");
|
||||
if (lastIndexNameList != null && lastIndexNameList.size() > 0) {
|
||||
bookList.get(i).setLastIndexName(lastIndexNameList.get(0));
|
||||
}
|
||||
|
||||
List<String> catNameList = (List<String>) highlightMap.get("catName");
|
||||
if (catNameList != null && catNameList.size() > 0) {
|
||||
bookList.get(i).setCatName(catNameList.get(0));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PageBean<EsBookVO> pageBean = new PageBean<>(bookList);
|
||||
pageBean.setTotal(total.longValue());
|
||||
pageBean.setPageNum(page);
|
||||
pageBean.setPageSize(pageSize);
|
||||
return pageBean;
|
||||
}
|
||||
throw new BusinessException(ResponseStatus.ES_SEARCH_FAIL);
|
||||
}
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
package com.java2nb.novel.search.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
*/
|
||||
@Data
|
||||
public class EsBookVO {
|
||||
|
||||
|
||||
private Long id;
|
||||
|
||||
@ApiModelProperty(value = "作品方向,0:男频,1:女频'")
|
||||
private Byte workDirection;
|
||||
|
||||
@ApiModelProperty(value = "分类ID")
|
||||
private Integer catId;
|
||||
|
||||
@ApiModelProperty(value = "分类名")
|
||||
private String catName;
|
||||
|
||||
@ApiModelProperty(value = "小说封面")
|
||||
private String picUrl;
|
||||
|
||||
@ApiModelProperty(value = "小说名")
|
||||
private String bookName;
|
||||
|
||||
@ApiModelProperty(value = "作者id")
|
||||
private Long authorId;
|
||||
|
||||
@ApiModelProperty(value = "作者名")
|
||||
private String authorName;
|
||||
|
||||
@ApiModelProperty(value = "书籍描述")
|
||||
private String bookDesc;
|
||||
|
||||
@ApiModelProperty(value = "评分")
|
||||
private Float score;
|
||||
|
||||
|
||||
@ApiModelProperty(value = "书籍状态,0:连载中,1:已完结")
|
||||
private Byte bookStatus;
|
||||
|
||||
@ApiModelProperty(value = "点击量")
|
||||
private Long visitCount;
|
||||
|
||||
@ApiModelProperty(value = "总字数")
|
||||
private Integer wordCount;
|
||||
|
||||
@ApiModelProperty(value = "评论数")
|
||||
private Integer commentCount;
|
||||
|
||||
@ApiModelProperty(value = "最新目录ID")
|
||||
private Long lastIndexId;
|
||||
|
||||
@ApiModelProperty(value = "最新目录名")
|
||||
private String lastIndexName;
|
||||
|
||||
@ApiModelProperty(value = "最新目录更新时间")
|
||||
private String lastIndexUpdateTime;
|
||||
|
||||
@ApiModelProperty(value = "是否收费,1:收费,0:免费")
|
||||
private Byte isVip;
|
||||
|
||||
@ApiModelProperty(value = "状态,0:入库,1:上架")
|
||||
private Byte status;
|
||||
|
||||
|
||||
|
||||
|
||||
private Integer crawlSourceId;
|
||||
|
||||
|
||||
private String crawlBookId;
|
||||
|
||||
|
||||
|
||||
private Byte crawlIsStop;
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package com.java2nb.novel.search.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import springfox.documentation.annotations.ApiIgnore;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.Pattern;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 封装页面搜索参数
|
||||
* @author xiongxiaoyang
|
||||
* @version 1.0
|
||||
* @since 2020/5/28
|
||||
*/
|
||||
@Data
|
||||
public class SearchParamVO {
|
||||
|
||||
@ApiModelProperty("搜索关键字")
|
||||
private String keyword;
|
||||
|
||||
@ApiModelProperty("作品方向")
|
||||
private Byte workDirection;
|
||||
|
||||
@ApiModelProperty("分类ID")
|
||||
private Integer catId;
|
||||
|
||||
@ApiModelProperty("是否收费,1:收费,0:免费")
|
||||
private Byte isVip;
|
||||
|
||||
@ApiModelProperty("小说更新状态,0:连载中,1:已完结")
|
||||
private Byte bookStatus;
|
||||
|
||||
@ApiModelProperty("字数最小值")
|
||||
private Integer wordCountMin;
|
||||
|
||||
@ApiModelProperty("字数最大值")
|
||||
private Integer wordCountMax;
|
||||
|
||||
@ApiModelProperty(hidden = true)
|
||||
private Date updateTimeMin;
|
||||
|
||||
@ApiModelProperty("更新时间(单位:天)")
|
||||
private Long updatePeriod;
|
||||
|
||||
@ApiModelProperty("排序字段")
|
||||
private String sort;
|
||||
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user