1. 需求分析和介面檔案
當添加一個檔案時,檔案存放在mongodb中(資料庫),同時將檔案存放在elasticsearch中用于根據檔案名稱和內容進行全文檢索,
需求分析:查詢檔案串列
① 當選擇多個分類時,需要查詢這些分類下的檔案,沒有選擇分類時,查詢全部檔案,
② 當輸入查詢關鍵字時,需要根據檔案的名稱和檔案的內容進行全文檢索,
③ 當選擇檔案標簽時,需要精確匹配標簽,標簽可以選擇多個,
④ 當選擇最近更新時間時,需要根據最近更新時間查詢,
根據更新時間查詢時,如果是根據最近(24小時,30天等)查詢:請求url
GET http://10.64.2.27:8129/ngsoc/KNOWLEDGE/api/v1/docs?
categoryIds=61669a53b8238341868cb2f2&categoryIds=616686c5057866695bda3c23
&keyword=演講&tag=歷史記錄&tag=演講系列&timeScope=last7d&start=1&limit=5
根據更新時間查詢時,如果是根據自定義時間查詢:請求url
GET http://10.64.2.27:8129/ngsoc/KNOWLEDGE/api/v1/docs?
categoryIds=61669a53b8238341868cb2f2&categoryIds=616686c5057866695bda3c23
&keyword=演講&tag=歷史記錄&tag=演講系列&timeScope=1635696000:1635868799:custom_time&start=1&limit=5
| 引數名 | 描述 | 型別 | 是否必填 | 備注 |
|---|---|---|---|---|
| categoryIds | 分類id集合 | array | 否 | 獲取指定分類下的檔案串列,沒有選擇分類時,分頁顯示所有檔案,Set集合 |
| keyword | 搜索關鍵字 | String | 否 | 根據檔案名稱和檔案內容進行全文搜索 |
| tags | 知識檔案標簽 | array | 否 | 根據知識檔案標簽查詢 |
| timeScope | 時間區間 | Object | 否 | 根據最近更新時間進行查詢,last24h(最近24小時),last7d(最近7天),last14d(最近14天),last30d(最近30天),custom_time(自定義時間段) |
| start | 當前頁的起始行 | int | 否 | 默認值為0 |
| limit | 每頁顯示數量 | int | 否 | 默認值為5 |
2. 查詢檔案串列介面實作
@Slf4j
@Service
public class ElasticSearchImpl implements ElasticSearchService, CommonConstant {
@Autowired
private RestHighLevelClient restHighLevelClient;
private SearchRequest buildSearchRequest(DocSearchReqVo docSearchReqVo) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
// 根據分類查詢
List<String> categoryIds = docSearchReqVo.getCategoryIds();
if (!CollectionUtils.isEmpty(categoryIds)) {
boolQueryBuilder.filter(QueryBuilders.termsQuery("categoryId", categoryIds));
}
// 對content和name采用全文檢索
String keyword = docSearchReqVo.getKeyword();
if (StringUtils.isNotEmpty(keyword)) {
boolQueryBuilder.must(QueryBuilders.multiMatchQuery(keyword, "content", "name"));
}
// 對tags采用精確查詢
List<String> tags = docSearchReqVo.getTags();
if (!CollectionUtils.isEmpty(tags)) {
boolQueryBuilder.must(QueryBuilders.termsQuery("tags", tags));
}
// 根據updateTime的時間區間查詢
TimeRange time = TimeUtil.getTimeRange(docSearchReqVo.getTimeScope());
if (time != null) {
// 校驗并初始化查詢引數
TimeUtil.formatAndCheckQueryParam(time, new Date());
Long timeFrom = time.getTimeFrom();
Long timeTo = time.getTimeTo();
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("updateTime").from(timeFrom).to(timeTo);
boolQueryBuilder.filter(rangeQueryBuilder);
}
searchSourceBuilder.query(boolQueryBuilder);
// 分頁和排序
searchSourceBuilder.from(docSearchReqVo.getStart());
searchSourceBuilder.size(docSearchReqVo.getLimit());
searchSourceBuilder.sort("updateTime", SortOrder.DESC);
return new SearchRequest(new String[] { ELASTICSEARCH_KNOWLEDGE_INDEX }, searchSourceBuilder);
}
}
3. 根據updateTime的時間區間查詢
① 定義列舉類:
@AllArgsConstructor
@Getter
public enum TimeScopeEnum {
/**
* 最近24小時
*/
LAST_24_HOURS("last24h", 1),
/**
* 最近7天
*/
LAST_7_DAYS("last7d", 7),
/**
* 最近14天
*/
LAST_14_DAYS( "last14d", 14),
/**
* 最近30天
*/
LAST_30_DAYS("last30d", 30);
/**
* 前端傳入引數名稱
*/
private final String name;
/**
* 對應天數
*/
private final Integer value;
}
② 定義時間區間類TimeRange:
@Data
public class TimeRange {
@ApiModelProperty(value = "起始時間")
private Long timeFrom;
@ApiModelProperty(value = "結束時間")
private Long timeTo;
@ApiModelProperty(value = "傳入的查詢時間")
private String timeRange;
}
③ 將前端傳過來的時間引數資料封裝成TimeRange物件:
自定義:timeScope=1635696000:1635868799:custom_time
最近更新時間:timeScope=last7d
public class TimeUtil implements CommonConstant {
/**
* 自定義時間時,需要對時間進行分割處理并封裝成timeRange物件
* @param timeScope 時間引數
* @return 時間物件
*/
public static TimeRange getTimeRange(String timeScope) {
TimeRange time = null;
if (StringUtils.isNotEmpty(timeScope)) {
time = new TimeRange();
String[] split = timeScope.split(":");
if (split.length == 1) {
// 說明不是自定義時間,將其封裝成timeRange物件
time = TimeUtil.fetchTimeRangeByObj(timeScope);
} else {
time.setTimeFrom(Long.valueOf(split[0]));
time.setTimeTo(Long.valueOf(split[1]));
time.setTimeRange(split[2]);
}
}
return time;
}
/**
* 獲取時間查詢的引數,即封裝timeRange物件
*
* @param alarmRange 引數
* @return TimeRange 時間引數
*/
public static TimeRange fetchTimeRangeByObj(Object alarmRange) {
TimeRange timeRange = new TimeRange();
if (alarmRange instanceof String) {
timeRange.setTimeRange((String) alarmRange);
} else {
timeRange = new ObjectMapper().convertValue(alarmRange, new TypeReference<>() {
});
}
return timeRange;
}
}
④ 校驗并初始化TimeRange物件的時間引數資料:
public class TimeUtil implements CommonConstant {
/**
* 校驗并初始化查詢引數,初始化TimeRange物件
*
* @param timeRange 時間范圍查詢標識
* @param now 當前時間
*/
public static void formatAndCheckQueryParam(TimeRange timeRange, Date now) {
String rangeTimeRange = timeRange.getTimeRange();
// 查詢過去30天的檔案,包括當天的檔案
if (TimeScopeEnum.LAST_30_DAYS.getName().equals(rangeTimeRange)) {
// 計算開始時間
Date temp = DateUtils.addDays(now, -(THIRTY - 1));
Date from = DateUtil.timeStr2Date(DateUtil.date2Str(temp, YYYYMMDD), YYYYMMDDHHMMSS);
// 設定開始時間
timeRange.setTimeFrom(from.getTime());
// 設定結束時間
timeRange.setTimeTo(now.getTime());
return;
}
// 查詢過去14天的檔案,包括當天的檔案
if (TimeScopeEnum.LAST_14_DAYS.getName().equals(rangeTimeRange)) {
Date temp = DateUtils.addDays(now, -(FOERTRRN - 1));
Date from = DateUtil.timeStr2Date(DateUtil.date2Str(temp, YYYYMMDD), YYYYMMDDHHMMSS);
timeRange.setTimeFrom(from.getTime());
timeRange.setTimeTo(now.getTime());
return;
}
// 查詢過去7天的檔案,包括當天的檔案
if (TimeScopeEnum.LAST_7_DAYS.getName().equals(rangeTimeRange)) {
Date temp = DateUtils.addDays(now, -(SEVEN - 1));
Date from = DateUtil.timeStr2Date(DateUtil.date2Str(temp, YYYYMMDD), YYYYMMDDHHMMSS);
timeRange.setTimeFrom(from.getTime());
timeRange.setTimeTo(now.getTime());
return;
}
// 查詢過去24小時檔案
if (TimeScopeEnum.LAST_24_HOURS.getName().equals(rangeTimeRange)) {
Date temp = DateUtils.addHours(now, -(TWENTY_FOUR - 1));
Date from = DateUtil.timeStr2Date(DateUtil.date2Str(temp, YYYYMMDDHH), YYYYMMDDHHMMSS);
timeRange.setTimeFrom(from.getTime());
timeRange.setTimeTo(now.getTime());
return;
}
// 自定義查詢范圍
if (ALARM_OVERVIEW_QUERY_SELF.equals(rangeTimeRange)) {
verifiyTimeRange(timeRange);
}
}
/**
* 校驗時間引數
*
* @param timeRange 時間引數
* @throw CommonException 例外
*/
private static void verifiyTimeRange(TimeRange timeRange) {
Long start = timeRange.getTimeFrom();
Long end = timeRange.getTimeTo();
// 起始時間和結束時間任意一個為空
if (Objects.isNull(start) || Objects.isNull(end)) {
String msg = I18nUtils.i18n("query.time.no.empty");
throw new CommonException(msg);
}
}
}
⑤ 根據時間區間查詢:
@Slf4j
@Service
public class ElasticSearchImpl implements ElasticSearchService, CommonConstant {
@Autowired
private RestHighLevelClient restHighLevelClient;
private SearchRequest buildSearchRequest(DocSearchReqVo docSearchReqVo) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
// 根據updateTime的時間區間查詢
TimeRange time = TimeUtil.getTimeRange(docSearchReqVo.getTimeScope());
if (time != null) {
// 校驗并初始化查詢引數
TimeUtil.formatAndCheckQueryParam(time, new Date());
Long timeFrom = time.getTimeFrom();
Long timeTo = time.getTimeTo();
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("updateTime").from(timeFrom).to(timeTo);
boolQueryBuilder.filter(rangeQueryBuilder);
}
searchSourceBuilder.query(boolQueryBuilder);
return new SearchRequest(new String[] { ELASTICSEARCH_KNOWLEDGE_INDEX }, searchSourceBuilder);
}
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/352147.html
標籤:其他
上一篇:kafka 安裝和啟動
