手机端-搜索-查询文档库搜索中心数据接口开发。

This commit is contained in:
wangzhijiang
2023-06-15 15:23:49 +08:00
parent 95c6524801
commit 3ac0affc8a
3 changed files with 248 additions and 0 deletions
@@ -34,4 +34,11 @@ public class SearchCenterController {
public Result<Map<String, Object>> getWdkCollectAndSubscribeInfoByUser(@RequestParam Map<String,Object> params) {
return this.searchCenterService.getWdkCollectAndSubscribeInfoByUser(params);
}
@AutoLog(value = "手机端-搜索-查询文档库搜索中心数据")
@ApiOperation(value = "手机端-搜索-查询文档库搜索中心数据", notes = "手机端-搜索-查询文档库搜索中心数据")
@GetMapping(value = "/getWdkPage")
public Result<?> getWdkPage(@RequestParam Map<String,Object> params) {
return this.searchCenterService.getWdkPage(params);
}
}
@@ -9,4 +9,6 @@ public interface ISearchCenterService {
Result<Map<String, Object>> getDataCount();
Result<Map<String, Object>> getWdkCollectAndSubscribeInfoByUser(Map<String,Object> params);
Result<?> getWdkPage(Map<String, Object> params);
}
@@ -1,13 +1,18 @@
package com.jero.modules.phone.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jero.common.api.vo.Result;
import com.jero.common.constant.enums.CutEnum;
import com.jero.common.es.JeroElasticsearchTemplate;
import com.jero.common.system.vo.LoginUser;
import com.jero.common.util.DateUtils;
import com.jero.modules.collection.entity.OnlCgformCollection;
import com.jero.modules.collection.service.IOnlCgformCollectionService;
import com.jero.modules.document.enums.SearchEnum;
import com.jero.modules.document.service.IBussDocumentLibraryEOService;
import com.jero.modules.phone.service.ISearchCenterService;
import com.jero.modules.problemKnowledgeBase.entity.ProblemKnowledgeBaseEO;
@@ -18,10 +23,15 @@ import com.jero.modules.report.service.ILawsMonthlyReportManageEOService;
import com.jero.modules.subscribe.entity.OnlCgformSubscribe;
import com.jero.modules.subscribe.service.IOnlCgformSubscribeService;
import com.jero.modules.system.service.ISysUserService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -41,6 +51,9 @@ public class SearchCenterServiceImpl implements ISearchCenterService {
private IOnlCgformCollectionService onlCgformCollectionService;
@Autowired
private IOnlCgformSubscribeService onlCgformSubscribeService;
@Autowired
private JeroElasticsearchTemplate jeroElasticsearchTemplate;
public static final String SEARCH_FLAG = "";//标识(es数据带此标识的代表全文和段落的数据,不带此标识的代表列表数据)
@Override
public Result<Map<String, Object>> getDataCount() {
@@ -101,4 +114,230 @@ public class SearchCenterServiceImpl implements ISearchCenterService {
result.put("collectionList", collectionList);
return Result.OK(result);
}
@Override
public Result<?> getWdkPage(Map<String, Object> params) {
boolean indexExistsFlag = true;
String cut = (String) params.get("cut");
Integer pageNo = Integer.parseInt(params.get("pageNo").toString());
Integer pageSize = Integer.parseInt(params.get("pageSize").toString());
if (StringUtils.equals(cut, CutEnum.CN.getValue())) {
indexExistsFlag = jeroElasticsearchTemplate.indexExists(SearchEnum.FULL_TEXT_SEARCH_CN.getValue());
} else if (StringUtils.equals(cut, CutEnum.EN.getValue())) {
indexExistsFlag = jeroElasticsearchTemplate.indexExists(SearchEnum.FULL_TEXT_SEARCH_EN.getValue());
}
if (!indexExistsFlag) {
Page page = new Page(pageNo, pageSize);
return Result.OK(page);
}
// 定义需要查询的字段数组
List<String> fieldList = new ArrayList<>();
fieldList.add("serial_number");
fieldList.add("title");
fieldList.add("content");
JSONArray queryMapJsonAll = new JSONArray();
JSONArray queryMapInputJson = new JSONArray();
Map<String, Object> mapHighlight = new HashMap<>();
Map<String, Object> mapHighlight1 = new HashMap<>();
String selectValue = (String) params.get("selectValue");
if (StringUtils.isNotBlank(selectValue)) {
for (String field : fieldList) {
this.setQueryMapJson(queryMapInputJson, selectValue, field);
//高亮
this.wdkHighlight(mapHighlight, field);
}
mapHighlight1.put("fields", mapHighlight);
if (CollectionUtils.isNotEmpty(queryMapInputJson)) {
JSONObject jsonObject = jeroElasticsearchTemplate.buildBoolQuery(null, null, queryMapInputJson);
queryMapJsonAll.add(jsonObject);
}
}
//指定module_type_flag= WDK
JSONArray moduleTypeFlagQueryJsonArrMust = new JSONArray();
Map<String, Object> moduleTypeFlagValueMap = new HashMap<>();
Map<String, Object> moduleTypeFlagFieldMap = new HashMap<>();
Map<String, Object> moduleTypeFlagMatchMap = new HashMap<>();
moduleTypeFlagValueMap.put("query", "WDK");
moduleTypeFlagFieldMap.put("module_type_flag", moduleTypeFlagValueMap);
moduleTypeFlagMatchMap.put("match", moduleTypeFlagFieldMap);
moduleTypeFlagQueryJsonArrMust.add(moduleTypeFlagMatchMap);
JSONObject moduleTypeFlagQueryJson = jeroElasticsearchTemplate.buildBoolQuery(moduleTypeFlagQueryJsonArrMust, null, null);
queryMapJsonAll.add(moduleTypeFlagQueryJson);
JSONObject querySort = new JSONObject();
if (StringUtils.isEmpty(selectValue) || StringUtils.equals(selectValue, SEARCH_FLAG)) {
Map<String, Object> createTime = new HashMap<>();
Map<String, Object> createTime1 = new HashMap<>();
createTime.put("order", "desc");
createTime1.put("create_time", createTime);
querySort.putAll(createTime1);
} else {
Map<String, Object> score = new HashMap<>();
Map<String, Object> score1 = new HashMap<>();
score.put("order", "desc");
score1.put("_score", score);
querySort.putAll(score1);
}
IPage page = this.getWdkPage(
queryMapJsonAll,
mapHighlight1,
pageNo,
pageSize,
cut,
querySort,
null,
null
);
return Result.OK(page);
}
@NotNull
private IPage getWdkPage(JSONArray queryMapJson,
Map<String,Object> highlightMap,
Integer pageNo,
Integer pageSize,
String cut,
JSONObject querySort,
JSONArray queryMustNot,
JSONArray queryShould) {
JSONObject jsonObject = jeroElasticsearchTemplate.buildBoolQuery(queryMapJson, queryMustNot, queryShould);
JSONArray jsonArraySort = new JSONArray();
jsonArraySort.add(querySort);
//1. 条件,分页
JSONObject queryObject = jeroElasticsearchTemplate.buildQuery(
null,
jsonObject,
highlightMap,
jsonArraySort,
pageNo - 1,
pageSize
);
//2. 数据查询
JSONObject search = new JSONObject();
if (CutEnum.CN.getValue().equals(cut)) {
search = jeroElasticsearchTemplate.search(
SearchEnum.FULL_TEXT_SEARCH_CN.getValue(),
SearchEnum.FULL_TEXT_SEARCH_CN.getValue(),
queryObject
);
} else {
search = jeroElasticsearchTemplate.search(
SearchEnum.FULL_TEXT_SEARCH_EN.getValue(),
SearchEnum.FULL_TEXT_SEARCH_EN.getValue(),
queryObject
);
}
List<Map<String, Object>> list = (List<Map<String, Object>>) (((Map) search.get("hits")).get("hits"));
List<Map<String, Object>> mapList = new ArrayList<>();
for (Map<String, Object> map : list) {
Map<String, Object> mapSource = (Map<String, Object>) map.get("_source");
Map<String, Object> mapHighlight = (Map<String, Object>) map.get("highlight");
if(ObjectUtils.isNotEmpty(mapHighlight)){
for (Map.Entry<String, Object> entry : mapHighlight.entrySet()) {
String key = entry.getKey();
List<String> value = (List<String>) entry.getValue();
String fieldConyent= "";
for (String s : value) {
fieldConyent += s;
}
mapSource.put(key,fieldConyent);
}
}
String create_time_str = "";
if(ObjectUtils.isNotEmpty(mapSource.get("create_time"))){
create_time_str = DateUtils.formatTime((Long) mapSource.get("create_time"));
}
mapSource.put("create_time_str",create_time_str);
mapList.add(mapSource);
}
//处理分页
IPage page = new Page(pageNo, pageSize);
page.setTotal(Long.parseLong(String.valueOf(((Map) search.get("hits")).get("total"))));
page.setRecords(mapList);
return page;
}
/**
* 设置文档库高亮字段
* @param mapHighlight
* @param key
*/
private void wdkHighlight(Map<String, Object> mapHighlight, String key) {
Map<String, Object> mapTemp = new HashMap<>();
List<String> list = new ArrayList<>();
list.add("<text class='highlight-class'>");
List<String> list1 = new ArrayList<>();
list1.add("</text>");
mapTemp.put("pre_tags", list);
mapTemp.put("post_tags", list1);
mapTemp.put("fragment_size", 320);
mapTemp.put("number_of_fragments", 1);
mapTemp.put("type", "plain");
mapHighlight.put(key, mapTemp);
}
private void setQueryMapJson(JSONArray queryMapJson, String selectValue, String field) {
// 前缀匹配 缺点是前缀一定不能断开
// 情况举例:用户只记得前面那段字
Map<String, Object> map21 = new HashMap<>();
Map<String, Object> map31 = new HashMap<>();
Map<String, Object> map41 = new HashMap<>();
if ("title".equals(field)) {
map31.put("boost", 10);
} else if ("content".equals(field)) {
map31.put("boost", 0.01);
}
map31.put("value", selectValue);
map21.put(field + ".keyword", map31);
map41.put("prefix", map21);
queryMapJson.add(map41);
// match_phrase_prefix 词组匹配查询,允许最后词组与文中的任意分词前缀匹配
Map<String, Object> map22 = new HashMap<>();
Map<String, Object> map32 = new HashMap<>();
Map<String, Object> map42 = new HashMap<>();
if ("title".equals(field)) {
map32.put("boost", 10);
} else if ("content".equals(field)) {
map32.put("boost", 0.01);
}
map32.put("query", selectValue);
map22.put(field, map32);
map42.put("match_phrase_prefix", map22);
queryMapJson.add(map42);
// match分词匹配查询
// 情况举例:用户可能他知道开头的前缀几个字,知道中间的几个字
Map<String, Object> map23 = new HashMap<>();
Map<String, Object> map33 = new HashMap<>();
Map<String, Object> map43 = new HashMap<>();
if ("title".equals(field)) {
map33.put("boost", 10);
} else if ("content".equals(field)) {
map33.put("boost", 0.01);
}
map33.put("query", selectValue);
map23.put(field, map33);
map43.put("match", map23);
queryMapJson.add(map43);
// wildcard模糊查询
// 情况举例:用户只记得中间那段字
Map<String, Object> map24 = new HashMap<>();
Map<String, Object> map44 = new HashMap<>();
map24.put(field, "*" + selectValue + "*");
map44.put("wildcard", map24);
queryMapJson.add(map44);
}
}