mirror of
https://github.com/201206030/novel-cloud.git
synced 2025-08-24 17:42:42 +00:00
refactor: 基于 novel 项目 & Spring Cloud 2022 & Spring Cloud Alibaba 2022 重构
This commit is contained in:
@@ -4,50 +4,44 @@
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>novel-cloud</artifactId>
|
||||
<groupId>com.java2nb.novel</groupId>
|
||||
<version>1.3.0</version>
|
||||
<groupId>io.github.xxyopen</groupId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>novel-search</artifactId>
|
||||
<description>搜索微服务</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.java2nb.novel</groupId>
|
||||
<artifactId>book-api</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
<groupId>co.elastic.clients</groupId>
|
||||
<artifactId>elasticsearch-java</artifactId>
|
||||
<version>${elasticsearch.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-netflix-hystrix</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.elasticsearch.client</groupId>
|
||||
<artifactId>elasticsearch-rest-high-level-client</artifactId>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.searchbox</groupId>
|
||||
<artifactId>jest</artifactId>
|
||||
<groupId>com.xuxueli</groupId>
|
||||
<artifactId>xxl-job-core</artifactId>
|
||||
<version>${xxl-job.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
|
||||
<groupId>io.github.xxyopen</groupId>
|
||||
<artifactId>novel-config</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.redisson</groupId>
|
||||
<artifactId>redisson-spring-boot-starter</artifactId>
|
||||
<groupId>io.github.xxyopen</groupId>
|
||||
<artifactId>novel-book-api</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -55,37 +49,16 @@
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<!-- <plugin>
|
||||
<groupId>com.spotify</groupId>
|
||||
<artifactId>docker-maven-plugin</artifactId>
|
||||
<version>${docker.maven.plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>build-image</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>build</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<imageName>201206030/${project.artifactId}:${project.version}</imageName>
|
||||
<dockerHost>${docker.host}</dockerHost>
|
||||
<baseImage>java:8</baseImage>
|
||||
<entryPoint>["java", "-jar","/${project.build.finalName}.jar"]</entryPoint>
|
||||
<resources>
|
||||
<resource>
|
||||
<targetPath>/</targetPath>
|
||||
<directory>${project.build.directory}</directory>
|
||||
<include>${project.build.finalName}.jar</include>
|
||||
</resource>
|
||||
</resources>
|
||||
<excludes>
|
||||
<exclude>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>-->
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
|
||||
</project>
|
@@ -1,24 +0,0 @@
|
||||
package com.java2nb.novel;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.web.servlet.ServletComponentScan;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
/**
|
||||
* 搜索微服务启动器
|
||||
* @author xiongxiaoyang
|
||||
* @version 1.0
|
||||
* @since 2020/5/27
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableFeignClients
|
||||
@EnableScheduling
|
||||
@ServletComponentScan
|
||||
public class SearchApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SearchApplication.class);
|
||||
}
|
||||
}
|
@@ -1,40 +0,0 @@
|
||||
package com.java2nb.novel.search.config;
|
||||
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.elasticsearch.client.RestClient;
|
||||
import org.elasticsearch.client.RestHighLevelClient;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* elasticsearch搜索引擎配置
|
||||
* @author xiongxiaoyang
|
||||
*/
|
||||
@Configuration
|
||||
public class EsConfig {
|
||||
|
||||
@Value("${spring.elasticsearch.jest.uris}")
|
||||
private String esUris;
|
||||
|
||||
@Bean(destroyMethod = "close")
|
||||
public RestHighLevelClient esClient(){
|
||||
|
||||
|
||||
String[] uris = esUris.split(",");
|
||||
HttpHost[] hosts = new HttpHost[uris.length];
|
||||
for(int i = 0 ; i < uris.length ; i++){
|
||||
String uri = uris[i];
|
||||
String scheme = uri.substring(0,uri.indexOf(":")).trim();
|
||||
String hostname = uri.substring(uri.indexOf("://")+3,uri.lastIndexOf(":")).trim();
|
||||
Integer port = Integer.parseInt(uri.substring(uri.lastIndexOf(":")+1).trim());
|
||||
hosts[i] = new HttpHost(hostname,port,scheme);
|
||||
}
|
||||
|
||||
return new RestHighLevelClient(
|
||||
RestClient.builder(hosts));
|
||||
|
||||
}
|
||||
}
|
@@ -1,43 +0,0 @@
|
||||
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 xiongxiaoyang
|
||||
* @version 1.0
|
||||
* @since 2020/5/28
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("search")
|
||||
@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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@@ -1,17 +0,0 @@
|
||||
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 {
|
||||
|
||||
|
||||
}
|
@@ -1,70 +0,0 @@
|
||||
package com.java2nb.novel.search.listener;
|
||||
|
||||
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 com.rabbitmq.client.Channel;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.amqp.core.Message;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
||||
/**
|
||||
* 消息监听器
|
||||
* @author xiongxiaoyang
|
||||
* @version 1.0
|
||||
* @since 2020/6/2
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class BookVisitAddListener {
|
||||
|
||||
|
||||
private final CacheService cacheService;
|
||||
|
||||
private final SearchService searchService;
|
||||
|
||||
private final RedissonClient redissonClient;
|
||||
|
||||
private final BookFeignClient bookFeignClient;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 更新搜索引擎
|
||||
* 流量削峰,每本小说1个小时更新一次
|
||||
*/
|
||||
@RabbitListener(queues = {"UPDATE-ES-QUEUE"})
|
||||
public void updateEs(Long bookId, Channel channel, Message message) {
|
||||
|
||||
log.debug("收到更新搜索引擎消息:" + bookId);
|
||||
RLock lock = redissonClient.getLock("addVisitCountToEs");
|
||||
lock.lock();
|
||||
if (cacheService.get(CacheKey.ES_IS_UPDATE_VISIT + bookId) == null) {
|
||||
cacheService.set(CacheKey.ES_IS_UPDATE_VISIT + bookId, "1", 60 * 60);
|
||||
try {
|
||||
Thread.sleep(1000 * 5);
|
||||
Book book = bookFeignClient.queryBookById(bookId);
|
||||
searchService.importToEs(book);
|
||||
}catch (Exception e){
|
||||
cacheService.del(CacheKey.ES_IS_UPDATE_VISIT + bookId);
|
||||
log.error("更新搜索引擎失败"+bookId);
|
||||
}
|
||||
|
||||
}
|
||||
lock.unlock();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -1,79 +0,0 @@
|
||||
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.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 小说数据导入搜索引擎定时任务
|
||||
*
|
||||
* @author xiongxiaoyang
|
||||
* @version 1.0
|
||||
* @since 2020/5/27
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class BookToEsSchedule {
|
||||
|
||||
private final BookFeignClient bookFeignClient;
|
||||
|
||||
private final CacheService cacheService;
|
||||
|
||||
|
||||
private final SearchService searchService;
|
||||
|
||||
private final RedissonClient redissonClient;
|
||||
|
||||
|
||||
/**
|
||||
* 1分钟导入一次
|
||||
*/
|
||||
@Scheduled(fixedRate = 1000 * 60)
|
||||
public void saveToEs() {
|
||||
RLock lock = redissonClient.getLock("saveToEs");
|
||||
lock.lock();
|
||||
|
||||
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);
|
||||
}
|
||||
lock.unlock();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@@ -1,34 +0,0 @@
|
||||
package com.java2nb.novel.search.service;
|
||||
|
||||
|
||||
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);
|
||||
|
||||
|
||||
}
|
@@ -1,217 +0,0 @@
|
||||
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.action.index.IndexRequest;
|
||||
import org.elasticsearch.action.index.IndexResponse;
|
||||
import org.elasticsearch.client.RequestOptions;
|
||||
import org.elasticsearch.client.RestHighLevelClient;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
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 JestClient jestClient;
|
||||
|
||||
private final RestHighLevelClient restHighLevelClient;
|
||||
|
||||
private final String INDEX = "novel";
|
||||
|
||||
|
||||
@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()));
|
||||
|
||||
IndexRequest request = new IndexRequest(INDEX);
|
||||
request.id(book.getId()+"");
|
||||
request.source(new ObjectMapper().writeValueAsString(esBookVO), XContentType.JSON);
|
||||
IndexResponse index = restHighLevelClient.index(request, RequestOptions.DEFAULT);
|
||||
|
||||
log.debug(index.getResult().toString());
|
||||
|
||||
}
|
||||
|
||||
@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)
|
||||
.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).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));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new PageBean<>(page,pageSize,total.longValue(), bookList);
|
||||
}
|
||||
throw new BusinessException(ResponseStatus.ES_SEARCH_FAIL);
|
||||
}
|
||||
}
|
@@ -1,86 +0,0 @@
|
||||
package com.java2nb.novel.search.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 小说ES VO对象
|
||||
* @author xiongxiaoyang
|
||||
* @version 1.0
|
||||
* @since 2020/5/27
|
||||
*/
|
||||
@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;
|
||||
|
||||
|
||||
}
|
@@ -1,52 +0,0 @@
|
||||
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;
|
||||
|
||||
|
||||
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
package io.github.xxyopen.novel.search;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
@SpringBootApplication(scanBasePackages = {"io.github.xxyopen.novel"})
|
||||
@EnableDiscoveryClient
|
||||
@EnableFeignClients(basePackages = {"io.github.xxyopen.novel.book.feign"})
|
||||
public class NovelSearchApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(NovelSearchApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,80 @@
|
||||
package io.github.xxyopen.novel.search.config;
|
||||
|
||||
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
|
||||
import org.elasticsearch.client.RestClient;
|
||||
import org.elasticsearch.client.RestClientBuilder;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.elasticsearch.RestClientBuilderCustomizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.cert.X509Certificate;
|
||||
|
||||
/**
|
||||
* Elasticsearch 相关配置
|
||||
*
|
||||
* @author xiongxiaoyang
|
||||
* @date 2022/5/23
|
||||
*/
|
||||
@Configuration
|
||||
@Slf4j
|
||||
public class EsConfig {
|
||||
|
||||
/**
|
||||
* 解决 ElasticsearchClientConfigurations 修改默认 ObjectMapper 配置的问题
|
||||
*/
|
||||
@Bean
|
||||
JacksonJsonpMapper jacksonJsonpMapper() {
|
||||
return new JacksonJsonpMapper();
|
||||
}
|
||||
|
||||
/**
|
||||
* fix `sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException:
|
||||
* unable to find valid certification path to requested target`
|
||||
*/
|
||||
@Bean
|
||||
RestClient elasticsearchRestClient(RestClientBuilder restClientBuilder,
|
||||
ObjectProvider<RestClientBuilderCustomizer> builderCustomizers) {
|
||||
restClientBuilder.setHttpClientConfigCallback((HttpAsyncClientBuilder clientBuilder) -> {
|
||||
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
|
||||
@Override
|
||||
public void checkClientTrusted(X509Certificate[] chain, String authType) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkServerTrusted(X509Certificate[] chain, String authType) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return new X509Certificate[0];
|
||||
}
|
||||
}};
|
||||
SSLContext sc = null;
|
||||
try {
|
||||
sc = SSLContext.getInstance("SSL");
|
||||
sc.init(null, trustAllCerts, new SecureRandom());
|
||||
} catch (KeyManagementException | NoSuchAlgorithmException e) {
|
||||
log.error("Elasticsearch RestClient 配置失败!", e);
|
||||
}
|
||||
assert sc != null;
|
||||
clientBuilder.setSSLContext(sc);
|
||||
clientBuilder.setSSLHostnameVerifier((hostname, session) -> true);
|
||||
|
||||
builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(clientBuilder));
|
||||
return clientBuilder;
|
||||
});
|
||||
return restClientBuilder.build();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
package io.github.xxyopen.novel.search.config;
|
||||
|
||||
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* XXL-JOB 配置类
|
||||
*
|
||||
* @author xiongxiaoyang
|
||||
* @date 2022/5/31
|
||||
*/
|
||||
@Configuration
|
||||
@Slf4j
|
||||
public class XxlJobConfig {
|
||||
|
||||
@Value("${xxl.job.admin.addresses}")
|
||||
private String adminAddresses;
|
||||
|
||||
@Value("${xxl.job.accessToken}")
|
||||
private String accessToken;
|
||||
|
||||
@Value("${xxl.job.executor.appname}")
|
||||
private String appname;
|
||||
|
||||
@Value("${xxl.job.executor.logpath}")
|
||||
private String logPath;
|
||||
|
||||
@Bean
|
||||
public XxlJobSpringExecutor xxlJobExecutor() {
|
||||
log.info(">>>>>>>>>>> xxl-job config init.");
|
||||
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
|
||||
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
|
||||
xxlJobSpringExecutor.setAccessToken(accessToken);
|
||||
xxlJobSpringExecutor.setAppname(appname);
|
||||
xxlJobSpringExecutor.setLogPath(logPath);
|
||||
return xxlJobSpringExecutor;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,118 @@
|
||||
package io.github.xxyopen.novel.search.constant;
|
||||
|
||||
import io.github.xxyopen.novel.common.constant.SystemConfigConsts;
|
||||
|
||||
/**
|
||||
* elasticsearch 相关常量
|
||||
*
|
||||
* @author xiongxiaoyang
|
||||
* @date 2022/5/23
|
||||
*/
|
||||
public class EsConsts {
|
||||
|
||||
private EsConsts() {
|
||||
throw new IllegalStateException(SystemConfigConsts.CONST_INSTANCE_EXCEPTION_MSG);
|
||||
}
|
||||
|
||||
/**
|
||||
* 小说索引
|
||||
*/
|
||||
public static class BookIndex {
|
||||
|
||||
private BookIndex() {
|
||||
throw new IllegalStateException(SystemConfigConsts.CONST_INSTANCE_EXCEPTION_MSG);
|
||||
}
|
||||
|
||||
/**
|
||||
* 索引名
|
||||
*/
|
||||
public static final String INDEX_NAME = "book";
|
||||
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
public static final String FIELD_ID = "id";
|
||||
|
||||
/**
|
||||
* 作品方向;0-男频 1-女频
|
||||
*/
|
||||
public static final String FIELD_WORK_DIRECTION = "workDirection";
|
||||
|
||||
/**
|
||||
* 类别ID
|
||||
*/
|
||||
public static final String FIELD_CATEGORY_ID = "categoryId";
|
||||
|
||||
/**
|
||||
* 类别名
|
||||
*/
|
||||
public static final String FIELD_CATEGORY_NAME = "categoryName";
|
||||
|
||||
/**
|
||||
* 小说名
|
||||
*/
|
||||
public static final String FIELD_BOOK_NAME = "bookName";
|
||||
|
||||
/**
|
||||
* 作家id
|
||||
*/
|
||||
public static final String FIELD_AUTHOR_ID = "authorId";
|
||||
|
||||
/**
|
||||
* 作家名
|
||||
*/
|
||||
public static final String FIELD_AUTHOR_NAME = "authorName";
|
||||
|
||||
/**
|
||||
* 书籍描述
|
||||
*/
|
||||
public static final String FIELD_BOOK_DESC = "bookDesc";
|
||||
|
||||
/**
|
||||
* 评分;总分:10 ,真实评分 = score/10
|
||||
*/
|
||||
public static final String FIELD_SCORE = "score";
|
||||
|
||||
/**
|
||||
* 书籍状态;0-连载中 1-已完结
|
||||
*/
|
||||
public static final String FIELD_BOOK_STATUS = "bookStatus";
|
||||
|
||||
/**
|
||||
* 点击量
|
||||
*/
|
||||
public static final String FIELD_VISIT_COUNT = "visitCount";
|
||||
|
||||
/**
|
||||
* 总字数
|
||||
*/
|
||||
public static final String FIELD_WORD_COUNT = "wordCount";
|
||||
|
||||
/**
|
||||
* 评论数
|
||||
*/
|
||||
public static final String FIELD_COMMENT_COUNT = "commentCount";
|
||||
|
||||
/**
|
||||
* 最新章节ID
|
||||
*/
|
||||
public static final String FIELD_LAST_CHAPTER_ID = "lastChapterId";
|
||||
|
||||
/**
|
||||
* 最新章节名
|
||||
*/
|
||||
public static final String FIELD_LAST_CHAPTER_NAME = "lastChapterName";
|
||||
|
||||
/**
|
||||
* 最新章节更新时间
|
||||
*/
|
||||
public static final String FIELD_LAST_CHAPTER_UPDATE_TIME = "lastChapterUpdateTime";
|
||||
|
||||
/**
|
||||
* 是否收费;1-收费 0-免费
|
||||
*/
|
||||
public static final String FIELD_IS_VIP = "isVip";
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,41 @@
|
||||
package io.github.xxyopen.novel.search.controller.front;
|
||||
|
||||
import io.github.xxyopen.novel.book.dto.req.BookSearchReqDto;
|
||||
import io.github.xxyopen.novel.book.dto.resp.BookInfoRespDto;
|
||||
import io.github.xxyopen.novel.common.constant.ApiRouterConsts;
|
||||
import io.github.xxyopen.novel.common.resp.PageRespDto;
|
||||
import io.github.xxyopen.novel.common.resp.RestResp;
|
||||
import io.github.xxyopen.novel.search.service.SearchService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springdoc.core.annotations.ParameterObject;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 前台门户-搜索模块 API 控制器
|
||||
*
|
||||
* @author xiongxiaoyang
|
||||
* @date 2022/5/27
|
||||
*/
|
||||
@Tag(name = "SearchController", description = "前台门户-搜索模块")
|
||||
@RestController
|
||||
@RequestMapping(ApiRouterConsts.API_FRONT_SEARCH_URL_PREFIX)
|
||||
@RequiredArgsConstructor
|
||||
public class SearchController {
|
||||
|
||||
private final SearchService searchService;
|
||||
|
||||
/**
|
||||
* 小说搜索接口
|
||||
*/
|
||||
@Operation(summary = "小说搜索接口")
|
||||
@GetMapping("books")
|
||||
public RestResp<PageRespDto<BookInfoRespDto>> searchBooks(
|
||||
@ParameterObject BookSearchReqDto condition) {
|
||||
return searchService.searchBooks(condition);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,34 @@
|
||||
package io.github.xxyopen.novel.search.manager.feign;
|
||||
|
||||
import io.github.xxyopen.novel.book.dto.resp.BookEsRespDto;
|
||||
import io.github.xxyopen.novel.book.feign.BookFeign;
|
||||
import io.github.xxyopen.novel.common.constant.ErrorCodeEnum;
|
||||
import io.github.xxyopen.novel.common.resp.RestResp;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 小说微服务调用 Feign 客户端管理
|
||||
*
|
||||
* @author xiongxiaoyang
|
||||
* @date 2023/3/29
|
||||
*/
|
||||
@Component
|
||||
@AllArgsConstructor
|
||||
public class BookFeignManager {
|
||||
|
||||
private final BookFeign bookFeign;
|
||||
|
||||
public List<BookEsRespDto> listEsBooks(Long maxBookId){
|
||||
RestResp<List<BookEsRespDto>> listRestResp = bookFeign.listNextEsBooks(maxBookId);
|
||||
if(Objects.equals(ErrorCodeEnum.OK.getCode(),listRestResp.getCode())){
|
||||
return listRestResp.getData();
|
||||
}
|
||||
return new ArrayList<>(0);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
package io.github.xxyopen.novel.search.service;
|
||||
|
||||
|
||||
import io.github.xxyopen.novel.book.dto.req.BookSearchReqDto;
|
||||
import io.github.xxyopen.novel.book.dto.resp.BookInfoRespDto;
|
||||
import io.github.xxyopen.novel.common.resp.PageRespDto;
|
||||
import io.github.xxyopen.novel.common.resp.RestResp;
|
||||
|
||||
/**
|
||||
* 搜索 服务类
|
||||
*
|
||||
* @author xiongxiaoyang
|
||||
* @date 2022/5/23
|
||||
*/
|
||||
public interface SearchService {
|
||||
|
||||
/**
|
||||
* 小说搜索
|
||||
*
|
||||
* @param condition 搜索条件
|
||||
* @return 搜索结果
|
||||
*/
|
||||
RestResp<PageRespDto<BookInfoRespDto>> searchBooks(BookSearchReqDto condition);
|
||||
|
||||
}
|
@@ -0,0 +1,177 @@
|
||||
package io.github.xxyopen.novel.search.service.impl;
|
||||
|
||||
import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
||||
import co.elastic.clients.elasticsearch._types.SortOrder;
|
||||
import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
|
||||
import co.elastic.clients.elasticsearch._types.query_dsl.RangeQuery;
|
||||
import co.elastic.clients.elasticsearch._types.query_dsl.TermQuery;
|
||||
import co.elastic.clients.elasticsearch.core.SearchRequest;
|
||||
import co.elastic.clients.elasticsearch.core.SearchResponse;
|
||||
import co.elastic.clients.elasticsearch.core.search.Hit;
|
||||
import co.elastic.clients.elasticsearch.core.search.TotalHits;
|
||||
import co.elastic.clients.json.JsonData;
|
||||
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import io.github.xxyopen.novel.book.dto.req.BookSearchReqDto;
|
||||
import io.github.xxyopen.novel.book.dto.resp.BookEsRespDto;
|
||||
import io.github.xxyopen.novel.book.dto.resp.BookInfoRespDto;
|
||||
import io.github.xxyopen.novel.common.resp.PageRespDto;
|
||||
import io.github.xxyopen.novel.common.resp.RestResp;
|
||||
import io.github.xxyopen.novel.search.constant.EsConsts;
|
||||
import io.github.xxyopen.novel.search.service.SearchService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Elasticsearch 搜索 服务实现类
|
||||
*
|
||||
* @author xiongxiaoyang
|
||||
* @date 2022/5/23
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class SearchServiceImpl implements SearchService {
|
||||
|
||||
private final ElasticsearchClient esClient;
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public RestResp<PageRespDto<BookInfoRespDto>> searchBooks(BookSearchReqDto condition) {
|
||||
|
||||
SearchResponse<BookEsRespDto> response = esClient.search(s -> {
|
||||
|
||||
SearchRequest.Builder searchBuilder = s.index(EsConsts.BookIndex.INDEX_NAME);
|
||||
// 构建检索条件
|
||||
buildSearchCondition(condition, searchBuilder);
|
||||
// 排序
|
||||
if (!StringUtils.isBlank(condition.getSort())) {
|
||||
searchBuilder.sort(o -> o.field(f -> f
|
||||
.field(StringUtils.underlineToCamel(condition.getSort().split(" ")[0]))
|
||||
.order(SortOrder.Desc))
|
||||
);
|
||||
}
|
||||
// 分页
|
||||
searchBuilder.from((condition.getPageNum() - 1) * condition.getPageSize())
|
||||
.size(condition.getPageSize());
|
||||
// 设置高亮显示
|
||||
searchBuilder.highlight(h -> h.fields(EsConsts.BookIndex.FIELD_BOOK_NAME,
|
||||
t -> t.preTags("<em style='color:red'>").postTags("</em>"))
|
||||
.fields(EsConsts.BookIndex.FIELD_AUTHOR_NAME,
|
||||
t -> t.preTags("<em style='color:red'>").postTags("</em>")));
|
||||
|
||||
return searchBuilder;
|
||||
},
|
||||
BookEsRespDto.class
|
||||
);
|
||||
|
||||
TotalHits total = response.hits().total();
|
||||
|
||||
List<BookInfoRespDto> list = new ArrayList<>();
|
||||
List<Hit<BookEsRespDto>> hits = response.hits().hits();
|
||||
// 类型推断 var 非常适合 for 循环,JDK 10 引入,JDK 11 改进
|
||||
for (var hit : hits) {
|
||||
BookEsRespDto book = hit.source();
|
||||
assert book != null;
|
||||
if (!CollectionUtils.isEmpty(hit.highlight().get(EsConsts.BookIndex.FIELD_BOOK_NAME))) {
|
||||
book.setBookName(hit.highlight().get(EsConsts.BookIndex.FIELD_BOOK_NAME).get(0));
|
||||
}
|
||||
if (!CollectionUtils.isEmpty(
|
||||
hit.highlight().get(EsConsts.BookIndex.FIELD_AUTHOR_NAME))) {
|
||||
book.setAuthorName(
|
||||
hit.highlight().get(EsConsts.BookIndex.FIELD_AUTHOR_NAME).get(0));
|
||||
}
|
||||
list.add(BookInfoRespDto.builder()
|
||||
.id(book.getId())
|
||||
.bookName(book.getBookName())
|
||||
.categoryId(book.getCategoryId())
|
||||
.categoryName(book.getCategoryName())
|
||||
.authorId(book.getAuthorId())
|
||||
.authorName(book.getAuthorName())
|
||||
.wordCount(book.getWordCount())
|
||||
.lastChapterName(book.getLastChapterName())
|
||||
.build());
|
||||
}
|
||||
assert total != null;
|
||||
return RestResp.ok(
|
||||
PageRespDto.of(condition.getPageNum(), condition.getPageSize(), total.value(), list));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建检索条件
|
||||
*/
|
||||
private void buildSearchCondition(BookSearchReqDto condition,
|
||||
SearchRequest.Builder searchBuilder) {
|
||||
|
||||
BoolQuery boolQuery = BoolQuery.of(b -> {
|
||||
|
||||
// 只查有字数的小说
|
||||
b.must(RangeQuery.of(m -> m
|
||||
.field(EsConsts.BookIndex.FIELD_WORD_COUNT)
|
||||
.gt(JsonData.of(0))
|
||||
)._toQuery());
|
||||
|
||||
if (!StringUtils.isBlank(condition.getKeyword())) {
|
||||
// 关键词匹配
|
||||
b.must((q -> q.multiMatch(t -> t
|
||||
.fields(EsConsts.BookIndex.FIELD_BOOK_NAME + "^2",
|
||||
EsConsts.BookIndex.FIELD_AUTHOR_NAME + "^1.8",
|
||||
EsConsts.BookIndex.FIELD_BOOK_DESC + "^0.1")
|
||||
.query(condition.getKeyword())
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
// 精确查询
|
||||
if (Objects.nonNull(condition.getWorkDirection())) {
|
||||
b.must(TermQuery.of(m -> m
|
||||
.field(EsConsts.BookIndex.FIELD_WORK_DIRECTION)
|
||||
.value(condition.getWorkDirection())
|
||||
)._toQuery());
|
||||
}
|
||||
|
||||
if (Objects.nonNull(condition.getCategoryId())) {
|
||||
b.must(TermQuery.of(m -> m
|
||||
.field(EsConsts.BookIndex.FIELD_CATEGORY_ID)
|
||||
.value(condition.getCategoryId())
|
||||
)._toQuery());
|
||||
}
|
||||
|
||||
// 范围查询
|
||||
if (Objects.nonNull(condition.getWordCountMin())) {
|
||||
b.must(RangeQuery.of(m -> m
|
||||
.field(EsConsts.BookIndex.FIELD_WORD_COUNT)
|
||||
.gte(JsonData.of(condition.getWordCountMin()))
|
||||
)._toQuery());
|
||||
}
|
||||
|
||||
if (Objects.nonNull(condition.getWordCountMax())) {
|
||||
b.must(RangeQuery.of(m -> m
|
||||
.field(EsConsts.BookIndex.FIELD_WORD_COUNT)
|
||||
.lt(JsonData.of(condition.getWordCountMax()))
|
||||
)._toQuery());
|
||||
}
|
||||
|
||||
if (Objects.nonNull(condition.getUpdateTimeMin())) {
|
||||
b.must(RangeQuery.of(m -> m
|
||||
.field(EsConsts.BookIndex.FIELD_LAST_CHAPTER_UPDATE_TIME)
|
||||
.gte(JsonData.of(condition.getUpdateTimeMin().getTime()))
|
||||
)._toQuery());
|
||||
}
|
||||
|
||||
return b;
|
||||
|
||||
});
|
||||
|
||||
searchBuilder.query(q -> q.bool(boolQuery));
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,82 @@
|
||||
package io.github.xxyopen.novel.search.task;
|
||||
|
||||
import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
||||
import co.elastic.clients.elasticsearch._types.Time;
|
||||
import co.elastic.clients.elasticsearch.core.BulkRequest;
|
||||
import co.elastic.clients.elasticsearch.core.BulkResponse;
|
||||
import co.elastic.clients.elasticsearch.core.bulk.BulkResponseItem;
|
||||
import com.xxl.job.core.biz.model.ReturnT;
|
||||
import com.xxl.job.core.handler.annotation.XxlJob;
|
||||
import io.github.xxyopen.novel.book.dto.resp.BookEsRespDto;
|
||||
import io.github.xxyopen.novel.search.constant.EsConsts;
|
||||
import io.github.xxyopen.novel.search.manager.feign.BookFeignManager;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 小说数据同步到 elasticsearch 任务
|
||||
*
|
||||
* @author xiongxiaoyang
|
||||
* @date 2022/5/23
|
||||
*/
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class BookToEsTask {
|
||||
|
||||
private final BookFeignManager bookFeignManager;
|
||||
|
||||
private final ElasticsearchClient elasticsearchClient;
|
||||
|
||||
/**
|
||||
* 每月凌晨做一次全量数据同步
|
||||
*/
|
||||
@SneakyThrows
|
||||
@XxlJob("saveToEsJobHandler")
|
||||
public ReturnT<String> saveToEs() {
|
||||
|
||||
try {
|
||||
long maxId = 0;
|
||||
for (; ; ) {
|
||||
List<BookEsRespDto> books = bookFeignManager.listEsBooks(maxId);
|
||||
if (books.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
BulkRequest.Builder br = new BulkRequest.Builder();
|
||||
|
||||
for (BookEsRespDto book : books) {
|
||||
br.operations(op -> op
|
||||
.index(idx -> idx
|
||||
.index(EsConsts.BookIndex.INDEX_NAME)
|
||||
.id(book.getId().toString())
|
||||
.document(book)
|
||||
)
|
||||
).timeout(Time.of(t -> t.time("10s")));
|
||||
maxId = book.getId();
|
||||
}
|
||||
|
||||
BulkResponse result = elasticsearchClient.bulk(br.build());
|
||||
|
||||
// Log errors, if any
|
||||
if (result.errors()) {
|
||||
log.error("Bulk had errors");
|
||||
for (BulkResponseItem item : result.items()) {
|
||||
if (item.error() != null) {
|
||||
log.error(item.error().reason());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ReturnT.SUCCESS;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
return ReturnT.FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -1,4 +1,34 @@
|
||||
server:
|
||||
port: 9050
|
||||
spring:
|
||||
profiles:
|
||||
include: [common]
|
||||
include: common
|
||||
active: dev
|
||||
elasticsearch:
|
||||
uris:
|
||||
- https://127.0.0.1:9200
|
||||
username: elastic
|
||||
password: Fy2JWjJ1hcO2mi1USFLR
|
||||
|
||||
# XXL-JOB 配置
|
||||
xxl:
|
||||
job:
|
||||
admin:
|
||||
### 调度中心部署根地址 [选填]:如调度中心集群部署存在多个地址则用逗号分隔。执行器将会使用该地址进行"执行器心跳注册"和"任务结果回调";为空则关闭自动注册;
|
||||
addresses: http://127.0.0.1:8080/xxl-job-admin
|
||||
executor:
|
||||
### 执行器AppName [选填]:执行器心跳注册分组依据;为空则关闭自动注册
|
||||
appname: xxl-job-executor-novel
|
||||
### 执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径;
|
||||
logpath: logs/xxl-job/jobhandler
|
||||
### xxl-job, access token
|
||||
accessToken: 123
|
||||
|
||||
management:
|
||||
# 端点启用配置
|
||||
endpoint:
|
||||
logfile:
|
||||
# 启用返回日志文件内容的端点
|
||||
enabled: true
|
||||
# 外部日志文件路径
|
||||
external-file: logs/novel-search-service.log
|
@@ -1,16 +1,5 @@
|
||||
spring:
|
||||
application:
|
||||
name: novel-search
|
||||
name: novel-search-service
|
||||
profiles:
|
||||
active: dev
|
||||
cloud:
|
||||
nacos:
|
||||
config:
|
||||
ext‐config[0]:
|
||||
data‐id: novel-redis.yml
|
||||
group: novel-common
|
||||
refresh: true
|
||||
ext‐config[1]:
|
||||
data‐id: novel-rabbitmq.yml
|
||||
group: novel-common
|
||||
refresh: true
|
||||
include: common
|
||||
|
@@ -1,20 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<!-- 彩色日志依赖的渲染类 -->
|
||||
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
|
||||
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
|
||||
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
|
||||
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
|
||||
<conversionRule conversionWord="wex"
|
||||
converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>
|
||||
<conversionRule conversionWord="wEx"
|
||||
converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>
|
||||
<!-- 彩色日志格式 -->
|
||||
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}" />
|
||||
<property name="CONSOLE_LOG_PATTERN"
|
||||
value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
|
||||
|
||||
<!-- %m输出的信息,%p日志级别,%t线程名,%d日期,%c类的全名,%i索引【从数字0开始递增】,,, -->
|
||||
<!-- appender是configuration的子节点,是负责写日志的组件。 -->
|
||||
<!-- ConsoleAppender:把日志输出到控制台 -->
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<!--
|
||||
<pattern>%d %p (%file:%line\)- %m%n</pattern>
|
||||
-->
|
||||
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
|
||||
<!-- 控制台也要使用UTF-8,不要使用GBK,否则会中文乱码 -->
|
||||
<charset>UTF-8</charset>
|
||||
@@ -26,7 +26,7 @@
|
||||
<!-- 2.如果日期没有发生变化,但是当前日志的文件大小超过1KB时,对当前日志进行分割 重命名 -->
|
||||
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
|
||||
<File>logs/novel-search.log</File>
|
||||
<File>logs/novel-search-service.log</File>
|
||||
<!-- rollingPolicy:当发生滚动时,决定 RollingFileAppender 的行为,涉及文件移动和重命名。 -->
|
||||
<!-- TimeBasedRollingPolicy: 最常用的滚动策略,它根据时间来制定滚动策略,既负责滚动也负责出发滚动 -->
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
@@ -49,24 +49,31 @@
|
||||
<charset>UTF-8</charset>
|
||||
</encoder>
|
||||
</appender>
|
||||
<!--输出到logstash的appender-->
|
||||
<appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
|
||||
<!--可以访问的logstash日志收集端口-->
|
||||
<destination>198.245.61.51:4560</destination>
|
||||
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder"/>
|
||||
</appender>
|
||||
<!-- 控制台输出日志级别 -->
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
<appender-ref ref="FILE" />
|
||||
<appender-ref ref="LOGSTASH"/>
|
||||
</root>
|
||||
<!-- 指定项目中某个包,当有日志操作行为时的日志记录级别 -->
|
||||
<!-- com.maijinjie.springboot 为根包,也就是只要是发生在这个根包下面的所有日志操作行为的权限都是DEBUG -->
|
||||
<!-- 级别依次为【从高到低】:FATAL > ERROR > WARN > INFO > DEBUG > TRACE -->
|
||||
<logger name="com.java2nb" level="DEBUG">
|
||||
<appender-ref ref="STDOUT" />
|
||||
<appender-ref ref="FILE" />
|
||||
<appender-ref ref="LOGSTASH"/>
|
||||
</logger>
|
||||
<springProfile name="dev">
|
||||
<!-- ROOT 日志级别 -->
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
<appender-ref ref="FILE"/>
|
||||
</root>
|
||||
<!-- 指定项目中某个包,当有日志操作行为时的日志记录级别 -->
|
||||
<!-- com.maijinjie.springboot 为根包,也就是只要是发生在这个根包下面的所有日志操作行为的权限都是DEBUG -->
|
||||
<!-- 级别依次为【从高到低】:FATAL > ERROR > WARN > INFO > DEBUG > TRACE -->
|
||||
<logger name="io.github.xxyopen" level="DEBUG" additivity="false">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
<appender-ref ref="FILE"/>
|
||||
</logger>
|
||||
</springProfile>
|
||||
|
||||
<springProfile name="prod">
|
||||
<!-- ROOT 日志级别 -->
|
||||
<root level="INFO">
|
||||
<appender-ref ref="FILE"/>
|
||||
</root>
|
||||
<!-- 指定项目中某个包,当有日志操作行为时的日志记录级别 -->
|
||||
<!-- com.maijinjie.springboot 为根包,也就是只要是发生在这个根包下面的所有日志操作行为的权限都是DEBUG -->
|
||||
<!-- 级别依次为【从高到低】:FATAL > ERROR > WARN > INFO > DEBUG > TRACE -->
|
||||
<logger name="io.github.xxyopen" level="ERROR" additivity="false">
|
||||
<appender-ref ref="FILE"/>
|
||||
</logger>
|
||||
</springProfile>
|
||||
</configuration>
|
Reference in New Issue
Block a user