Compare commits

...

13 Commits

Author SHA1 Message Date
xxy
9975faed01 点击量计算优化 2020-05-26 02:27:39 +08:00
xxy
8fde3a3725 点击量计算优化 2020-05-26 02:13:28 +08:00
xxy
430504ee28 点击量计算优化 2020-05-26 02:10:37 +08:00
xxy
16447c60ec 引入redisson 2020-05-26 00:28:12 +08:00
xxy
be7cbe2d6f 欲引入Redisson框架实现分布式锁 2020-05-26 00:04:00 +08:00
xxy
8f1ed88b07 es采集优化 2020-05-25 23:38:46 +08:00
xxy
9b9851e7ab 文档更新 2020-05-25 23:16:08 +08:00
a55edf0408 es优化 2020-05-25 21:37:00 +08:00
856c4c0667 es优化 2020-05-25 21:26:47 +08:00
e4dd5bcb71 引入rabbitmq流量削峰,累积点击量后统一更新 2020-05-25 21:06:51 +08:00
5dbddbdd96 es导入优化 2020-05-25 17:51:05 +08:00
73be43e1c5 优化es采集 2020-05-25 17:11:03 +08:00
ce2a3b4647 集成redis 2020-05-25 16:54:39 +08:00
24 changed files with 624 additions and 215 deletions

View File

@ -40,7 +40,28 @@ novel-plus -- 父工程
```
#### 技术选型
Springboot+Mybatis+Mysql+ElasticSearch+Ehcache+Thymeleaf+Layui
| 技术 | 说明
| -------------------- | ---------------------------
| SpringBoot | Spring应用快速开发脚手架
| MyBatis | 持久层ORM框架
| MyBatis Dynamic SQL | Mybatis动态sql
| PageHelper | MyBatis分页插件
| MyBatisGenerator | 持久层代码生成插件
| JJWT | JWT登录支持
| SpringSecurity | 安全框架
| Shiro | 安全框架
| Ehcache | Java进程内缓存框架(默认缓存)
| Redis | 分布式缓存(缓存替换方案默认关闭一行配置开启)
| ElasticSearch | 搜索引擎(搜索增强方案默认关闭一行配置开启)
| RabbitMq | 消息队列(流量削峰默认关闭一行配置开启)
| Redisson | 实现分布式锁
| Lombok | 简化对象封装工具
| Docker | 应用容器引擎
| Mysql | 数据库服务
| Thymeleaf | 模板引擎
| Layui | 前端UI
#### PC站截图

6
novel-admin/Dockerfile Normal file
View File

@ -0,0 +1,6 @@
FROM java:8
ADD novel-admin-1.0.0.jar /root
ENV dburl=""
ENV username=""
ENV password=""
ENTRYPOINT ["sh","-c","java -Dspring.datasource.url=${dburl} -Dspring.datasource.username=${username} -Dspring.datasource.password=${password} -jar /root/novel-admin-1.0.0.jar"]

View File

@ -31,11 +31,20 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!--ehcache-->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
<!--集成redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>${redis.version}</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>

View File

@ -50,4 +50,19 @@ public interface CacheKey {
* 上一次搜索引擎更新的时间
* */
String ES_LAST_UPDATE_TIME = "esLastUpdateTime";
/**
* 搜索引擎转换锁
* */
String ES_TRANS_LOCK = "esTransLock";
/**
* 上一次搜索引擎是否更新过小说点击量
* */
String ES_IS_UPDATE_VISIT = "esIsUpdateVisit";
/**
* 累积的小说点击量
* */
String BOOK_ADD_VISIT_COUNT = "bookAddVisitCount";
}

View File

@ -51,9 +51,5 @@ public interface CacheService {
* */
void expire(String key, long timeout);
/**
* 刷新缓存
* */
void refresh(String key);
}

View File

@ -1,22 +1,23 @@
package com.java2nb.novel.core.cache.impl;
import com.java2nb.novel.core.cache.CacheService;
import lombok.RequiredArgsConstructor;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
/**
* @author xxy
*/
@ConditionalOnProperty(prefix = "cache", name = "type", havingValue = "ehcache")
@RequiredArgsConstructor
@Service
public class EhCacheServiceImpl implements CacheService {
@Autowired
private CacheManager cacheManager ;
private static final String CACHE_NAME = "utilCache";
private final CacheManager cacheManager ;
/**
@ -30,14 +31,6 @@ public class EhCacheServiceImpl implements CacheService {
}
public CacheManager getCacheManager() {
return cacheManager;
}
@Override
public String get(String key) {
Element element = getCache().get(key);
@ -125,20 +118,6 @@ public class EhCacheServiceImpl implements CacheService {
}
@Override
public void refresh(String key) {
Element element = getCache().get(key);
if (element != null) {
Object value = element.getValue();
int timeToLive = element.getTimeToLive();
element = new Element(key, value);
element.setTimeToLive(timeToLive);
Cache cache = getCache();
cache.put(element);
}
}
}

View File

@ -0,0 +1,73 @@
package com.java2nb.novel.core.cache.impl;
import com.java2nb.novel.core.cache.CacheService;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
/**
* @author xxy
*/
@ConditionalOnProperty(prefix = "cache", name = "type", havingValue = "redis")
@RequiredArgsConstructor
@Service
public class RedisServiceImpl implements CacheService {
private final StringRedisTemplate stringRedisTemplate;
private final RedisTemplate<Object, Object> redisTemplate;
@Override
public String get(String key) {
return stringRedisTemplate.opsForValue().get(key);
}
@Override
public void set(String key, String value) {
stringRedisTemplate.opsForValue().set(key,value);
}
@Override
public void set(String key, String value, long timeout) {
stringRedisTemplate.opsForValue().set(key,value,timeout, TimeUnit.SECONDS);
}
@Override
public Object getObject(String key) {
return redisTemplate.opsForValue().get(key);
}
@Override
public void setObject(String key, Object value) {
redisTemplate.opsForValue().set(key,value);
}
@Override
public void setObject(String key, Object value, long timeout) {
redisTemplate.opsForValue().set(key,value,timeout, TimeUnit.SECONDS);
}
@Override
public void del(String key) {
redisTemplate.delete(key);
stringRedisTemplate.delete(key);
}
@Override
public boolean contains(String key) {
return redisTemplate.hasKey(key) || stringRedisTemplate.hasKey(key);
}
@Override
public void expire(String key, long timeout) {
redisTemplate.expire(key,timeout, TimeUnit.SECONDS);
stringRedisTemplate.expire(key,timeout, TimeUnit.SECONDS);
}
}

View File

@ -58,13 +58,16 @@ public enum ResponseStatus {
, BOOKNAME_EXISTS(4003,"已发布过同名小说!")
,
/**
* 搜索引擎相关错误
* */
ES_SEARCH_FAIL(9001,"密码错误!"),
/**
* 其他通用错误
* */
PASSWORD_ERROR(88001,"密码错误!");
private int code;
private String msg;

View File

@ -4,8 +4,27 @@ spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/novel_plus?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: test123456
password:
driver-class-name: com.mysql.cj.jdbc.Driver
#Redis服务器IP
redis:
host: 127.0.0.1
#Redis服务器连接端口
port: 6379
#Redis服务器连接密码
password: test
jedis:
pool:
#连接池最大连接数使用负值表示没有限制
max-active: 8
#连接池最大阻塞等待时间使用负值表示没有限制
max-wait: 1
#连接池最大阻塞等待时间使用负值表示没有限制
max-idle: 8
#连接池中的最小空闲连接
min-idle: 0
#连接超时时间毫秒
timeout: 30000

View File

@ -11,6 +11,9 @@ spring:
generator:
write-numbers-as-strings: true
#缓存类型ehcache(默认)redis
cache:
type: ehcache
mybatis:
configuration:

View File

@ -27,6 +27,11 @@
<version>${jjwt.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>io.searchbox</groupId>
@ -40,6 +45,14 @@
</dependency>
<!--引入redisson分布式锁-->
<!-- <dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>${redisson.version}</version>
</dependency>-->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>

View File

@ -4,14 +4,14 @@ import com.github.pagehelper.PageInfo;
import com.java2nb.novel.core.bean.ResultBean;
import com.java2nb.novel.core.bean.UserDetails;
import com.java2nb.novel.core.enums.ResponseStatus;
import com.java2nb.novel.entity.Book;
import com.java2nb.novel.entity.BookComment;
import com.java2nb.novel.entity.BookIndex;
import com.java2nb.novel.search.BookSP;
import com.java2nb.novel.service.BookService;
import com.java2nb.novel.vo.BookVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@ -19,7 +19,6 @@ import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
@ -33,6 +32,11 @@ public class BookController extends BaseController{
private final BookService bookService;
private final RabbitTemplate rabbitTemplate;
@Value("${spring.rabbitmq.enable}")
private Integer enableMq;
/**
* 查询首页小说设置列表数据
@ -105,7 +109,11 @@ public class BookController extends BaseController{
* */
@PostMapping("addVisitCount")
public ResultBean addVisitCount(Long bookId){
bookService.addVisitCount(bookId);
if(enableMq == 1) {
rabbitTemplate.convertAndSend("ADD-BOOK-VISIT-EXCHANGE", null, bookId);
}else {
bookService.addVisitCount(bookId, 1);
}
return ResultBean.ok();
}

View File

@ -0,0 +1,59 @@
package com.java2nb.novel.core.config;
import org.springframework.amqp.core.*;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author 11797
*/
@Configuration
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "enable", havingValue = "1")
public class RabbitConfig {
/**
* 更新数据库队列
*/
@Bean
public Queue updateDbQueue() {
return new Queue("UPDATE-DB-QUEUE", true);
}
/**
* 更新数据库队列
*/
@Bean
public Queue updateEsQueue() {
return new Queue("UPDATE-ES-QUEUE", true);
}
/**
* 增加点击量交换机
*/
@Bean
public FanoutExchange addVisitExchange() {
return new FanoutExchange("ADD-BOOK-VISIT-EXCHANGE");
}
/**
* 更新搜索引擎队列绑定到增加点击量交换机中
*/
@Bean
public Binding updateEsBinding() {
return BindingBuilder.bind(updateEsQueue()).to(addVisitExchange());
}
/**
* 更新数据库绑定到增加点击量交换机中
*/
@Bean
public Binding updateDbBinding() {
return BindingBuilder.bind(updateDbQueue()).to(addVisitExchange());
}
}

View File

@ -0,0 +1,107 @@
package com.java2nb.novel.core.listener;
import com.java2nb.novel.core.cache.CacheKey;
import com.java2nb.novel.core.cache.CacheService;
import com.java2nb.novel.core.utils.Constants;
import com.java2nb.novel.entity.Book;
import com.java2nb.novel.service.BookService;
import com.java2nb.novel.service.SearchService;
import com.java2nb.novel.vo.EsBookVO;
import com.rabbitmq.client.Channel;
import io.searchbox.client.JestClient;
import io.searchbox.core.Index;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import java.text.SimpleDateFormat;
/**
* @author 11797
*/
@Component
@Slf4j
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "enable", havingValue = "1")
public class BookVisitAddListener {
private final BookService bookService;
private final CacheService cacheService;
private final SearchService searchService;
// private final RedissonClient redissonClient;
/**
* 更新数据库
* 流量削峰每本小说累积10个点击更新一次
*/
@SneakyThrows
@RabbitListener(queues = {"UPDATE-DB-QUEUE"})
public void updateDb(Long bookId, Channel channel, Message message) {
log.debug("收到更新数据库消息:" + bookId);
Thread.sleep(1000 * 2);
//TODO 操作共享资源visitCount集群环境下有线程安全问题引入Redisson框架实现分布式锁
//RLock lock = redissonClient.getLock("visitCount");
//lock.lock();
//目前visitCount不重要数据可丢失暂不实现分布式锁
Integer visitCount = (Integer) cacheService.getObject(CacheKey.BOOK_ADD_VISIT_COUNT+bookId);
if(visitCount == null){
visitCount = 0 ;
}
cacheService.setObject(CacheKey.BOOK_ADD_VISIT_COUNT+bookId,++visitCount);
if(visitCount >= Constants.ADD_MAX_VISIT_COUNT) {
bookService.addVisitCount(bookId,visitCount);
cacheService.del(CacheKey.BOOK_ADD_VISIT_COUNT+bookId);
}
//TODO 操作共享资源visitCount集群环境下有线程安全问题引入Redisson框架实现分布式锁
//lock.unlock();
}
/**
* 更新搜索引擎
* 流量削峰每本小说1个小时更新一次
*/
@RabbitListener(queues = {"UPDATE-ES-QUEUE"})
public void updateEs(Long bookId, Channel channel, Message message) {
log.debug("收到更新搜索引擎消息:" + bookId);
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 = bookService.queryBookDetail(bookId);
searchService.importToEs(book);
}catch (Exception e){
cacheService.del(CacheKey.ES_IS_UPDATE_VISIT + bookId);
log.error("更新搜索引擎失败"+bookId);
}
}
}
}

View File

@ -5,6 +5,7 @@ import com.java2nb.novel.core.cache.CacheService;
import com.java2nb.novel.core.utils.BeanUtil;
import com.java2nb.novel.entity.Book;
import com.java2nb.novel.service.BookService;
import com.java2nb.novel.service.SearchService;
import com.java2nb.novel.vo.EsBookVO;
import io.searchbox.client.JestClient;
import io.searchbox.core.DocumentResult;
@ -37,18 +38,20 @@ public class BookToEsSchedule {
private final CacheService cacheService;
private final JestClient jestClient;
private boolean lock = false;
private final SearchService searchService;
/**
* 2分钟导入一次
* 1分钟导入一次
*/
@Scheduled(fixedRate = 1000 * 60 * 2)
@Scheduled(fixedRate = 1000 * 60)
public void saveToEs() {
if (!lock) {
lock = true;
//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);
@ -56,37 +59,25 @@ public class BookToEsSchedule {
lastDate = new SimpleDateFormat("yyyy-MM-dd").parse("2020-01-01");
}
long count ;
do {
List<Book> books = bookService.queryBookByUpdateTimeByPage(lastDate, 100);
for (Book book : books) {
//导入到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("novel").type("book").id(book.getId().toString()).build();
jestClient.execute(action);
searchService.importToEs(book);
lastDate = book.getUpdateTime();
//1秒钟导入一本书1分钟导入60本
Thread.sleep(1000);
Thread.sleep(5000);
}
count = books.size();
}while (count == 100);
cacheService.setObject(CacheKey.ES_LAST_UPDATE_TIME, lastDate);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
cacheService.del(CacheKey.ES_TRANS_LOCK);
lock = false;
}

View File

@ -30,4 +30,9 @@ public class Constants {
* 首页设置的小说数量
* */
public static final int INDEX_BOOK_SETTING_NUM = 32;
/**
* 累积的最大点击量
* */
public static final Integer ADD_MAX_VISIT_COUNT = 10;
}

View File

@ -16,7 +16,7 @@ public interface FrontBookMapper extends BookMapper {
List<BookVO> searchByPage(BookSP params);
void addVisitCount(@Param("bookId") Long bookId, @Param("date") Date date);
void addVisitCount(@Param("bookId") Long bookId, @Param("visitCount") Integer visitCount);
List<Book> listRecBookByCatId(@Param("catId") Integer catId);

View File

@ -112,8 +112,8 @@ public interface BookService {
/**
* 增加点击次数
* @param bookId 书籍ID
* */
void addVisitCount(Long bookId);
* @param visitCount*/
void addVisitCount(Long bookId, Integer visitCount);
/**
* 查询章节数

View File

@ -0,0 +1,27 @@
package com.java2nb.novel.service;
import com.github.pagehelper.PageInfo;
import com.java2nb.novel.entity.Book;
import com.java2nb.novel.search.BookSP;
/**
* @author 11797
*/
public interface SearchService {
/**
* 导入到es
* @param book 小说数据
*/
void importToEs(Book book);
/**
* 搜索
* @param params 搜索参数
* @param page 当前页码
* @param pageSize 每页大小
* @return 分页信息
*/
PageInfo searchBook(BookSP params, int page, int pageSize);
}

View File

@ -1,6 +1,5 @@
package com.java2nb.novel.service.impl;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@ -15,21 +14,14 @@ import com.java2nb.novel.mapper.*;
import com.java2nb.novel.search.BookSP;
import com.java2nb.novel.service.AuthorService;
import com.java2nb.novel.service.BookService;
import com.java2nb.novel.service.SearchService;
import com.java2nb.novel.vo.BookCommentVO;
import com.java2nb.novel.vo.BookSettingVO;
import com.java2nb.novel.vo.BookVO;
import com.java2nb.novel.vo.EsBookVO;
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.mybatis.dynamic.sql.SortSpecification;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
@ -39,7 +31,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.orderbyhelper.OrderByHelper;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@ -89,7 +80,7 @@ public class BookServiceImpl implements BookService {
private final AuthorService authorService;
private final JestClient jestClient;
private final SearchService searchService;
@SneakyThrows
@ -200,139 +191,8 @@ public class BookServiceImpl implements BookService {
try {
List<EsBookVO> bookList = new ArrayList<>(0);
return searchService.searchBook(params,page,pageSize);
//使用搜索引擎搜索
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("novel").addType("book")
.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("novel").addType("book").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));
}
}
}
}
}
}
}
PageInfo<EsBookVO> pageInfo = new PageInfo<>(bookList);
pageInfo.setTotal(total.longValue());
pageInfo.setPageNum(page);
pageInfo.setPageSize(pageSize);
return pageInfo;
}
}catch (Exception e){
log.error(e.getMessage(),e);
}
@ -479,8 +339,8 @@ public class BookServiceImpl implements BookService {
}
@Override
public void addVisitCount(Long bookId) {
bookMapper.addVisitCount(bookId, new Date());
public void addVisitCount(Long bookId, Integer visitCount) {
bookMapper.addVisitCount(bookId,visitCount);
}
@Override

View File

@ -0,0 +1,199 @@
package com.java2nb.novel.service.impl;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.java2nb.novel.core.enums.ResponseStatus;
import com.java2nb.novel.core.exception.BusinessException;
import com.java2nb.novel.core.utils.StringUtil;
import com.java2nb.novel.entity.Book;
import com.java2nb.novel.search.BookSP;
import com.java2nb.novel.service.SearchService;
import com.java2nb.novel.vo.EsBookVO;
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.List;
import java.util.Map;
/**
* @author 11797
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class SearchServiceImpl implements SearchService {
private final String INDEX = "novel";
private final String TYPE = "book";
private final JestClient jestClient;
@Override
@SneakyThrows
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 PageInfo searchBook(BookSP params, int page, int pageSize) {
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));
}
}
}
}
}
}
}
PageInfo<EsBookVO> pageInfo = new PageInfo<>(bookList);
pageInfo.setTotal(total.longValue());
pageInfo.setPageNum(page);
pageInfo.setPageSize(pageSize);
return pageInfo;
}
throw new BusinessException(ResponseStatus.ES_SEARCH_FAIL);
}
}

View File

@ -7,13 +7,27 @@ spring:
include: alipay
rabbitmq:
enable: 0
host: 127.0.0.1
username: guest
password: guest
virtual-host: /novel-plus
template:
# 缺省的交换机名称此处配置后发送消息如果不指定交换机就会使用这个
exchange: novel.exchange
publisher-confirms: false
elasticsearch:
#是否开启搜索引擎1开启0不开启
enable: 0
jest:
uris: http://127.0.0.1:9200
redisson:
singleServerConfig:
address: 127.0.0.1:6379
jwt:
secret: novel!#20191230

View File

@ -35,7 +35,7 @@
</select>
<update id="addVisitCount" >
update book set visit_count = visit_count + 1 , update_time = #{date}
update book set visit_count = visit_count + ${visitCount}
where id = #{bookId}
</update>

View File

@ -38,6 +38,8 @@
<jjwt.version>0.9.0</jjwt.version>
<elasticsearch.version>6.2.2</elasticsearch.version>
<jest.version>6.3.1</jest.version>
<redis.version>1.4.1.RELEASE</redis.version>
<redisson.version>3.12.5</redisson.version>
</properties>
<dependencyManagement>