Commit 78fec902 by 陈健智

搜索-增加历史记录

parent 553c2390
......@@ -115,6 +115,8 @@ public class RedisKeyPrefix {
public static final String YUQING_ANALYZE_HIGH_WORD = "BRANDKBS:YUQING:ANALYZE:HIGH:WORD:";
public static final String SEARCH_KEYWORD = "BRANDKBS:SEARCH:KEYWORD:";
public static String projectWarnHotTopKeyAll(String projectId, String type) {
return RedisKeyPrefix.generateRedisKey(RedisKeyPrefix.PROJECT_WARN_HOT_TOP, projectId, Tools.concat(type, "*"));
}
......
......@@ -2,6 +2,7 @@ package com.zhiwei.brandkbs2.controller.app;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhiwei.brandkbs2.aop.LogRecord;
import com.zhiwei.brandkbs2.auth.Auth;
......@@ -17,10 +18,12 @@ import com.zhiwei.brandkbs2.pojo.dto.*;
import com.zhiwei.brandkbs2.pojo.vo.ChannelListVO;
import com.zhiwei.brandkbs2.pojo.vo.PageVO;
import com.zhiwei.brandkbs2.service.*;
import com.zhiwei.brandkbs2.util.RedisUtil;
import com.zhiwei.brandkbs2.util.Tools;
import com.zhiwei.middleware.event.pojo.dto.BrandkbsEventSearchDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.lang3.tuple.Pair;
......@@ -33,9 +36,11 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
/**
* @author cjz
......@@ -63,6 +68,8 @@ public class AppSearchController extends BaseController {
@Value("${ef.search.url}")
private String getEfSearchUrl;
private static final int SEARCH_KEYWORD_CACHE_MAX_SIZE = 10;
@Resource(name = "markDataServiceImpl")
MarkDataService markDataService;
......@@ -84,6 +91,9 @@ public class AppSearchController extends BaseController {
@Resource(name = "extraServiceImpl")
ExtraService extraService;
@Resource(name = "redisUtil")
RedisUtil redisUtil;
@ApiOperation("搜索-查热点")
@LogRecord(values = "keyword", description = "查热点", arguments = true, entity = false)
@GetMapping("/hot/list")
......@@ -94,6 +104,7 @@ public class AppSearchController extends BaseController {
@RequestParam(value = "sort") String sort,
@RequestParam(value = "startTime", required = false) Long startTime,
@RequestParam(value = "endTime", required = false) Long endTime) {
cacheSearchKeyword(keyword, "hot");
ResponseEntity<JSONObject> jsonObjectResponseEntity = restTemplate.getForEntity(trendsSearchUrl, JSONObject.class, limit, page, type, keyword, sort, startTime, endTime);
JSONObject result = jsonObjectResponseEntity.getBody();
if (Objects.nonNull(result)) {
......@@ -109,6 +120,7 @@ public class AppSearchController extends BaseController {
public ResponseResult crisisSearch(@RequestParam(value = "page", defaultValue = "1") Integer page,
@RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize,
@RequestParam("keyword") String keyword) {
cacheSearchKeyword(keyword, "crisis");
ResponseEntity<String> responseEntity = restTemplate.getForEntity(crisisSearchUrl, String.class, page, pageSize, keyword);
Object result = JSON.parseObject(responseEntity.getBody()).get("data");
return ResponseResult.success(result);
......@@ -132,6 +144,7 @@ public class AppSearchController extends BaseController {
@RequestParam(value = "page", defaultValue = "1") Integer page,
@RequestParam(value = "pageSize", defaultValue = "20") Integer size) {
String name = keyword.trim();
cacheSearchKeyword(keyword, "event");
ResponseEntity<String> responseEntity = restTemplate.getForEntity(getEfSearchUrl, String.class, name, page, size);
JSONObject result = JSON.parseObject(responseEntity.getBody());
return ResponseResult.success(result);
......@@ -151,6 +164,7 @@ public class AppSearchController extends BaseController {
if (StringUtils.isNotEmpty(dto.getKeyword()) && Tools.checkUniteString(dto.getKeyword())){
return ResponseResult.failure("不支持特殊符号字段查询");
}
cacheSearchKeyword(dto.getKeyword(), "whole");
// 针对商业数据库做限制
if (dto.isExternalDataSource()) {
long time = DateUtils.addDays(Tools.truncDate(new Date(), Constant.DAY_PATTERN), -89).getTime();
......@@ -220,6 +234,7 @@ public class AppSearchController extends BaseController {
if (StringUtils.isNotEmpty(markSearchDTO.getKeyword()) && Tools.checkUniteString(markSearchDTO.getKeyword())){
return ResponseResult.failure("不支持特殊符号字段查询");
}
cacheSearchKeyword(markSearchDTO.getKeyword(), "yuqing");
PageVO<MarkFlowEntity> yuqingMarkList = markDataService.getYuqingMarkList(markSearchDTO);
// 仅第一页增加平台进量(声量)统计
if (1 == markSearchDTO.getPage()) {
......@@ -238,6 +253,7 @@ public class AppSearchController extends BaseController {
@LogRecord(values = "keyword", description = "查原始数据", arguments = true, entity = true)
@PostMapping("/origin/list")
public ResponseResult getOriginList(@RequestBody MarkSearchDTO markSearchDTO) {
cacheSearchKeyword(markSearchDTO.getKeyword(), "yuqing");
PageVO<MarkFlowEntity> originList = markDataService.getOriginList(markSearchDTO);
// 仅第一页增加平台进量(声量)统计
if (1 == markSearchDTO.getPage() && !Objects.equals("视频", markSearchDTO.getSearchType())) {
......@@ -276,6 +292,7 @@ public class AppSearchController extends BaseController {
@LogRecord(values = "keyword", description = "查渠道", arguments = true, entity = true)
@PostMapping(value = "/channel/channelList")
public ResponseResult getChannelList(@RequestBody ChannelSearchDTO channelSearchDTO) {
cacheSearchKeyword(channelSearchDTO.getKeyword(), "channel");
return ResponseResult.success(channelService.getChannelListNew(channelSearchDTO.getPage(), channelSearchDTO.getPageSize(),
channelSearchDTO.getKeyword(), channelSearchDTO.getPlatform(), channelSearchDTO.getEmotions(), channelSearchDTO.getMediaTypes(), channelSearchDTO.getArticlesCount(), channelSearchDTO.getSorter()));
}
......@@ -314,6 +331,7 @@ public class AppSearchController extends BaseController {
@LogRecord(values = "keyword", description = "查事件", arguments = true, entity = true)
@PostMapping("/event/newList")
public ResponseResult getEventListMiddleware(@RequestBody BrandkbsEventSearchDTO dto){
cacheSearchKeyword(dto.getKeyword(), "event");
return ResponseResult.success(eventService.getEventListMiddleware(dto));
}
......@@ -324,6 +342,7 @@ public class AppSearchController extends BaseController {
if (StringUtils.isNotEmpty(markSearchDTO.getKeyword()) && Tools.checkUniteString(markSearchDTO.getKeyword())){
return ResponseResult.failure("不支持特殊符号字段查询");
}
cacheSearchKeyword(markSearchDTO.getKeyword(), "contend");
return ResponseResult.success(markDataService.getContendSearchList(markSearchDTO));
}
......@@ -333,4 +352,43 @@ public class AppSearchController extends BaseController {
return ResponseResult.success(markDataService.getContendSearchCriteria(contendId));
}
@ApiOperation("搜索-搜索关键词历史记录")
@GetMapping("/keyword/cache")
public ResponseResult getSearchKeywordCache(@ApiParam(name = "searchType",
value = "查舆情:yuqing,查渠道:channel,查事件:event,查热点:hot,查危机:crisis,查竞品:contend,全网搜:whole", required = true)
@RequestParam(value = "searchType", defaultValue = "yuqing") String searchType) {
return ResponseResult.success(getKeywordCache(searchType));
}
/**
* 获取搜索关键词历史记录
* @param searchType
* @return
*/
private List<String> getKeywordCache(String searchType){
String key = RedisUtil.getSearchKeywordCacheKey(UserThreadLocal.getProjectId(), UserThreadLocal.getUserId(), searchType);
String value = redisUtil.get(key);
return JSONArray.parseArray(value, String.class);
}
/**
* 记录搜索关键词历史记录
* @param keyword
* @param searchType
*/
private void cacheSearchKeyword(String keyword, String searchType){
String key = RedisUtil.getSearchKeywordCacheKey(UserThreadLocal.getProjectId(), UserThreadLocal.getUserId(), searchType);
String value = redisUtil.get(key);
List<String> keywords = Objects.isNull(JSONArray.parseArray(value, String.class))
? new ArrayList<>(SEARCH_KEYWORD_CACHE_MAX_SIZE)
: JSONArray.parseArray(value, String.class);
if (StringUtils.isNotBlank(keyword)){
keywords.remove(keyword);
keywords.add(0, keyword);
if (keywords.size() > SEARCH_KEYWORD_CACHE_MAX_SIZE){
keywords.remove(SEARCH_KEYWORD_CACHE_MAX_SIZE);
}
}
redisUtil.setExpire(key, JSONObject.toJSONString(keywords), 7, TimeUnit.DAYS);
}
}
......@@ -119,6 +119,10 @@ public class RedisUtil {
return RedisKeyPrefix.YUQING_ANALYZE_HIGH_WORD + Tools.concat(projectId, contendId, planId, startTime, endTime);
}
public static String getSearchKeywordCacheKey(String projectId, String userId, String searchType){
return RedisKeyPrefix.SEARCH_KEYWORD + Tools.concat(projectId, userId, searchType);
}
public void setExpire(String key, String value, long timeout, TimeUnit unit) {
stringRedisTemplate.opsForValue().set(key, value, timeout, unit);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment