|
@@ -11,9 +11,7 @@ import com.aliyun.opensearch.sdk.generated.search.*;
|
|
|
import com.caimei365.commodity.components.SearchOpenService;
|
|
|
import com.caimei365.commodity.mapper.SearchMapper;
|
|
|
import com.caimei365.commodity.model.ResponseJson;
|
|
|
-import com.caimei365.commodity.model.vo.PageVo;
|
|
|
-import com.caimei365.commodity.model.vo.ProductListVo;
|
|
|
-import com.caimei365.commodity.model.vo.ShopListVo;
|
|
|
+import com.caimei365.commodity.model.vo.*;
|
|
|
import com.caimei365.commodity.service.SearchQueryService;
|
|
|
import com.caimei365.commodity.utils.Json2PojoUtil;
|
|
|
import com.github.pagehelper.PageHelper;
|
|
@@ -47,7 +45,7 @@ public class SearchQueryServiceImpl implements SearchQueryService {
|
|
|
private SearchOpenService searchOpenService;
|
|
|
|
|
|
/**
|
|
|
- * 获取索引Id
|
|
|
+ * 获取阿里云索引Id
|
|
|
*
|
|
|
* @param docType 文档类型
|
|
|
* @param docId 文档ID
|
|
@@ -67,16 +65,16 @@ public class SearchQueryServiceImpl implements SearchQueryService {
|
|
|
SearchParams searchParams = new SearchParams(config);
|
|
|
if (null != docId) {
|
|
|
if("product".equals(docType)) {
|
|
|
- // 根据商品Id获取索引Id
|
|
|
+ // 根据商品Id获取阿里云索引Id
|
|
|
searchParams.setQuery("p_id:'" + docId + "'");
|
|
|
} else if("shop".equals(docType)) {
|
|
|
- // 根据供应商Id获取索引Id
|
|
|
+ // 根据供应商Id获取阿里云索引Id
|
|
|
searchParams.setQuery("s_id:'" + docId + "'");
|
|
|
} else if("equipment".equals(docType)) {
|
|
|
- // 根据项目仪器Id获取索引Id
|
|
|
+ // 根据项目仪器Id获取阿里云索引Id
|
|
|
searchParams.setQuery("e_id:'" + docId + "'");
|
|
|
} else if("article".equals(docType)) {
|
|
|
- // 根据文章Id获取索引Id
|
|
|
+ // 根据文章Id获取阿里云索引Id
|
|
|
searchParams.setQuery("a_id:'" + docId + "'");
|
|
|
}
|
|
|
} else {
|
|
@@ -98,13 +96,13 @@ public class SearchQueryServiceImpl implements SearchQueryService {
|
|
|
return -1;
|
|
|
}
|
|
|
} catch (OpenSearchClientException | OpenSearchException | JSONException e) {
|
|
|
- log.error("查询文档索引Id异常:" + e);
|
|
|
+ log.error("查询文档阿里云索引Id异常:" + e);
|
|
|
return -1;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 根据文档类型获取索引Id集合
|
|
|
+ * 根据文档类型获取阿里云索引Id集合
|
|
|
*
|
|
|
* @param docType 文档类型
|
|
|
* @param pageNum 页码
|
|
@@ -153,7 +151,7 @@ public class SearchQueryServiceImpl implements SearchQueryService {
|
|
|
}
|
|
|
return idMap;
|
|
|
} catch (OpenSearchClientException | OpenSearchException | JSONException e) {
|
|
|
- log.error("查询文档索引Id异常:" + e);
|
|
|
+ log.error("查询文档阿里云索引Id异常:" + e);
|
|
|
return idMap;
|
|
|
}
|
|
|
}
|
|
@@ -204,7 +202,7 @@ public class SearchQueryServiceImpl implements SearchQueryService {
|
|
|
return -1;
|
|
|
}
|
|
|
} catch (OpenSearchClientException | OpenSearchException | JSONException e) {
|
|
|
- log.error("查询文档索引Id异常:" + e);
|
|
|
+ log.error("查询文档阿里云索引Id异常:" + e);
|
|
|
return -1;
|
|
|
}
|
|
|
}
|
|
@@ -303,9 +301,268 @@ public class SearchQueryServiceImpl implements SearchQueryService {
|
|
|
if (StringUtils.isEmpty(keyword)) {
|
|
|
queryStr = "e_all:'1'";
|
|
|
}
|
|
|
- return queryEquipment(queryStr, pageNum, pageSize);
|
|
|
+ // 阿里云搜索
|
|
|
+ ResponseJson<String> result = queryEquipment(queryStr, pageNum, pageSize);
|
|
|
+ if (0 == result.getCode()) {
|
|
|
+ return result;
|
|
|
+ } else {
|
|
|
+ // 阿里云搜索失败,再次从数据库搜索
|
|
|
+ return queryEquipmentFromDatabase(pageNum, pageSize, keyword);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据关键词搜索文章
|
|
|
+ *
|
|
|
+ * @param keyword 搜索关键字
|
|
|
+ * @param pageNum 页码
|
|
|
+ * @param pageSize 每页数量
|
|
|
+ * @return JsonStr(list)
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseJson<String> queryArticleByKeyword(String keyword, int pageNum, int pageSize) {
|
|
|
+ String queryStr = "article:'" + keyword + "'";
|
|
|
+ ArrayList<String> fetchFields = Lists.newArrayList("id", "a_id", "a_title", "a_image", "a_publisher", "a_publish_date", "a_intro", "a_pv", "a_type_id", "a_type_text", "a_label_ids", "a_label_text");
|
|
|
+ List<Map<String, String>> summaryList = new ArrayList<>(2);
|
|
|
+ Map<String, String> summaryField = new HashMap<>(2);
|
|
|
+ summaryField.put("length", "100");
|
|
|
+ summaryField.put("field", "a_title");
|
|
|
+ summaryList.add(summaryField);
|
|
|
+ Map<String, String> summaryField2 = new HashMap<>(2);
|
|
|
+ summaryField2.put("length", "200");
|
|
|
+ summaryField2.put("field", "a_intro");
|
|
|
+ summaryList.add(summaryField2);
|
|
|
+ // 阿里云搜索
|
|
|
+ ResponseJson<String> result = queryArticleForParams(queryStr, fetchFields, summaryList, pageNum, pageSize, null, 1);
|
|
|
+ if (0 == result.getCode()) {
|
|
|
+ return result;
|
|
|
+ } else {
|
|
|
+ // 阿里云搜索失败,再次从数据库搜索
|
|
|
+ return queryArticleFromDatabase(pageNum, pageSize, keyword, null, null, null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据文章ID搜索文章
|
|
|
+ *
|
|
|
+ * @param id 文章id
|
|
|
+ * @return JsonStr(list)
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseJson<String> queryArticleByArticleId(Integer id) {
|
|
|
+ String queryStr = "a_id:'" + id + "'";
|
|
|
+ ArrayList<String> fetchFields = Lists.newArrayList("id", "a_id", "a_title", "a_image", "a_publisher", "a_publish_date", "a_intro", "a_pv", "a_type_id", "a_type_text", "a_label_ids", "a_label_text");
|
|
|
+ // 阿里云搜索
|
|
|
+ ResponseJson<String> result = queryArticleForParams(queryStr, fetchFields, null, 1, 1, null, 1);
|
|
|
+ if (0 == result.getCode()) {
|
|
|
+ return result;
|
|
|
+ } else {
|
|
|
+ // 阿里云搜索失败,再次从数据库搜索
|
|
|
+ return queryArticleFromDatabase(1, 1, null, id, null, null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据分类搜索文章
|
|
|
+ *
|
|
|
+ * @param id 分类id
|
|
|
+ * @param pageNum 页码
|
|
|
+ * @param pageSize 每页数量
|
|
|
+ * @return JsonStr(list)
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseJson<String> queryArticleByType(Integer id, int pageNum, int pageSize) {
|
|
|
+ String queryStr = "a_type:'" + id + "'";
|
|
|
+ ArrayList<String> fetchFields = Lists.newArrayList("id", "a_id", "a_title", "a_image", "a_publisher", "a_publish_date", "a_intro", "a_pv", "a_type_id", "a_type_text", "a_label_ids", "a_label_text");
|
|
|
+ // 阿里云搜索
|
|
|
+ ResponseJson<String> result = scrollArticleForParams(queryStr, fetchFields, pageNum, pageSize, "a_id", 1);
|
|
|
+ if (0 == result.getCode()) {
|
|
|
+ return result;
|
|
|
+ } else {
|
|
|
+ // 阿里云搜索失败,再次从数据库搜索
|
|
|
+ return queryArticleFromDatabase(pageNum, pageSize, null, null, id, null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据标签搜索文章
|
|
|
+ *
|
|
|
+ * @param id 标签id
|
|
|
+ * @param pageNum 页码
|
|
|
+ * @param pageSize 每页数量
|
|
|
+ * @return JsonStr(list)
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseJson<String> queryArticleByLabel(Integer id, int pageNum, int pageSize) {
|
|
|
+ String queryStr = "a_label:'" + id + "'";
|
|
|
+ ArrayList<String> fetchFields = Lists.newArrayList("id", "a_id", "a_title", "a_image", "a_publisher", "a_publish_date", "a_intro", "a_pv", "a_type_id", "a_type_text", "a_label_ids", "a_label_text");
|
|
|
+ // 阿里云搜索
|
|
|
+ ResponseJson<String> result = scrollArticleForParams(queryStr, fetchFields, pageNum, pageSize, "a_id", 1);
|
|
|
+ if (0 == result.getCode()) {
|
|
|
+ return result;
|
|
|
+ } else {
|
|
|
+ // 阿里云搜索失败,再次从数据库搜索
|
|
|
+ return queryArticleFromDatabase(pageNum, pageSize, null, null, null, id);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 文章查询(数据库)
|
|
|
+ *
|
|
|
+ * @param keyword 关键词
|
|
|
+ * @param articleId 文章Id
|
|
|
+ * @param typeId 分类Id
|
|
|
+ * @param labelId 标签Id
|
|
|
+ * @param num 页码
|
|
|
+ * @param size 每页数量
|
|
|
+ */
|
|
|
+ private ResponseJson<String> queryArticleFromDatabase(int num, int size, String keyword, Integer articleId, Integer typeId, Integer labelId) {
|
|
|
+ String labelText = null;
|
|
|
+ if (null != labelId){
|
|
|
+ labelText = searchMapper.findLabelNameById(labelId);
|
|
|
+ }
|
|
|
+ PageHelper.startPage(num, size);
|
|
|
+ List<ArticleListVo> articleList = searchMapper.queryArticle(keyword, articleId, typeId, labelText);
|
|
|
+ PageVo<ArticleListVo> pageData = new PageVo<>(articleList);
|
|
|
+ int total = pageData.getTotalRecord();
|
|
|
+ JSONObject pageObj = new JSONObject();
|
|
|
+ pageObj.put("total", total);
|
|
|
+ pageObj.put("items", articleList);
|
|
|
+ log.info(">>>>>>数据库容错查询: pageSize(" + size +"),pageNum("+size+"),total("+total+")");
|
|
|
+ if (articleList.size()>0) {
|
|
|
+ return ResponseJson.success(pageObj.toString());
|
|
|
+ } else {
|
|
|
+ return ResponseJson.error("未查询到文档记录(数据库)", null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 文章查询
|
|
|
+ *
|
|
|
+ * @param queryStr 查询类型及参数
|
|
|
+ * @param fetchFields 返回字段
|
|
|
+ * @param summaryFields 摘要字段
|
|
|
+ * @param num 页码
|
|
|
+ * @param size 每页数量
|
|
|
+ */
|
|
|
+ private ResponseJson<String> queryArticleForParams(String queryStr, ArrayList<String> fetchFields, List<Map<String, String>> summaryFields, int num, int size, String sortField, Integer sortType) {
|
|
|
+ SearchParams searchParams;
|
|
|
+ int requestSize = num*size;
|
|
|
+ // 由于阿里云搜索机制问题(分页数据重复),搜索500条以内数据手动分页。
|
|
|
+ if (requestSize > SEARCH_MAX_NUM){
|
|
|
+ searchParams = searchOpenService.getSearchParams(queryStr, fetchFields, summaryFields, "article", num, size, sortField, sortType);
|
|
|
+ } else {
|
|
|
+ searchParams = searchOpenService.getSearchParams(queryStr, fetchFields, summaryFields, "article", 1, requestSize, sortField, sortType);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ // 查询推送
|
|
|
+ JSONObject result = searchOpenService.getResultJsonObject(queryStr, searchParams);
|
|
|
+ if (1 == result.getInt("status")) {
|
|
|
+ int total = result.getInt("total");
|
|
|
+ int totalPage = (int) Math.ceil( (double)total/size);
|
|
|
+ JSONArray resultArr = result.getJSONArray("items");
|
|
|
+ JSONArray pageData = new JSONArray();
|
|
|
+ if (requestSize > SEARCH_MAX_NUM) {
|
|
|
+ pageData = resultArr;
|
|
|
+ } else {
|
|
|
+ if (resultArr.length()>0){
|
|
|
+ int baseNum = (num-1)*size;
|
|
|
+ for (int i=0; i<size; i++) {
|
|
|
+ int index = baseNum + i;
|
|
|
+ int currentTotal = requestSize;
|
|
|
+ if (totalPage == num){
|
|
|
+ currentTotal = total;
|
|
|
+ }
|
|
|
+ if (index<currentTotal){
|
|
|
+ JSONObject item = resultArr.getJSONObject(index);
|
|
|
+ pageData.put(i, item);
|
|
|
+ } else {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 阿里云查询出json转换成文章pojo
|
|
|
+ List<ArticleListVo> articleList = Json2PojoUtil.toArticleList(pageData);
|
|
|
+ JSONObject pageObj = new JSONObject();
|
|
|
+ pageObj.put("total", total);
|
|
|
+ pageObj.put("items", articleList);
|
|
|
+ log.info(">>>>>>pageResult("+queryStr+"): pageSize(" + size +"),pageNum("+num+"),total("+total+")");
|
|
|
+ return ResponseJson.success(pageObj.toString());
|
|
|
+ } else {
|
|
|
+ return ResponseJson.error("未查询到文档记录", null);
|
|
|
+ }
|
|
|
+ } catch (OpenSearchClientException | OpenSearchException | JSONException e) {
|
|
|
+ log.error("查询文档异常:" + e.getMessage());
|
|
|
+ return ResponseJson.error("查询文档异常:" + e.getMessage(), null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 滚动查询文章数据
|
|
|
+ *
|
|
|
+ * @param queryStr 查询类型及参数
|
|
|
+ * @param pageNum 页码
|
|
|
+ * @param pageSize 每页数量
|
|
|
+ * @param sortField 排序字段
|
|
|
+ * @param sortType 升降序
|
|
|
+ */
|
|
|
+ private ResponseJson<String> scrollArticleForParams(String queryStr, ArrayList<String> fetchFields, int pageNum, int pageSize, String sortField, Integer sortType) {
|
|
|
+ int requestSize = pageNum*pageSize;
|
|
|
+ int requestPageSize = requestSize;
|
|
|
+ if (requestSize > SEARCH_MAX_NUM){
|
|
|
+ requestPageSize = (int) Math.floor(SEARCH_MAX_NUM/pageSize) * pageSize;
|
|
|
+ }
|
|
|
+ SearchParams searchParams = searchOpenService.getScrollParams(queryStr, fetchFields, requestPageSize, sortField, sortType);
|
|
|
+ ResponseJson<String> result;
|
|
|
+ try {
|
|
|
+ JSONObject pageObj = searchOpenService.deepPagingForParams(searchParams, queryStr, SEARCH_MAX_NUM, requestPageSize, pageNum, pageSize);
|
|
|
+ if (1 == pageObj.getInt("status")){
|
|
|
+ JSONArray pageData = pageObj.getJSONArray("items");
|
|
|
+ // 阿里云查询出json转换成商品pojo
|
|
|
+ List<ArticleListVo> articleList = Json2PojoUtil.toArticleList(pageData);
|
|
|
+ pageObj.put("items", articleList);
|
|
|
+ result = ResponseJson.success(pageObj.toString());
|
|
|
+ } else {
|
|
|
+ result = ResponseJson.error("未查询到文档记录", null);
|
|
|
+ }
|
|
|
+ } catch (OpenSearchClientException | OpenSearchException | JSONException e) {
|
|
|
+ log.error("查询文档异常:" + e.getMessage());
|
|
|
+ result = ResponseJson.error("查询文档异常:" + e.getMessage(), null);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取项目仪器(数据库)
|
|
|
+ *
|
|
|
+ * @param num 页码
|
|
|
+ * @param size 每页数量
|
|
|
+ * @param keyword 关键词
|
|
|
+ */
|
|
|
+ private ResponseJson<String> queryEquipmentFromDatabase(int num, int size, String keyword) {
|
|
|
+ PageHelper.startPage(num, size);
|
|
|
+ List<EquipmentListVo> equipmentList = searchMapper.queryEquipment(keyword);
|
|
|
+ PageVo<EquipmentListVo> pageData = new PageVo<>(equipmentList);
|
|
|
+ int total = pageData.getTotalRecord();
|
|
|
+ JSONObject pageObj = new JSONObject();
|
|
|
+ pageObj.put("total", total);
|
|
|
+ pageObj.put("items", equipmentList);
|
|
|
+ log.info(">>>>>>数据库容错查询: pageSize(" + size +"),pageNum("+size+"),total("+total+")");
|
|
|
+ if (equipmentList.size()>0) {
|
|
|
+ return ResponseJson.success(pageObj.toString());
|
|
|
+ } else {
|
|
|
+ return ResponseJson.error("未查询到文档记录(数据库)", null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询项目仪器(阿里云搜索)
|
|
|
+ *
|
|
|
+ * @param queryStr 查询类型及参数
|
|
|
+ * @param pageNum 页码
|
|
|
+ * @param pageSize 每页数量
|
|
|
+ */
|
|
|
private ResponseJson<String> queryEquipment(String queryStr, int pageNum, int pageSize) {
|
|
|
ArrayList<String> fetchFields = Lists.newArrayList("id", "e_id", "e_name", "e_image");
|
|
|
List<Map<String, String>> summaryList = new ArrayList<>(1);
|
|
@@ -321,7 +578,6 @@ public class SearchQueryServiceImpl implements SearchQueryService {
|
|
|
} else {
|
|
|
searchParams = searchOpenService.getSearchParams(queryStr, fetchFields, summaryList, "equipment", 1, requestSize, null, null);
|
|
|
}
|
|
|
- // 查询推送
|
|
|
try {
|
|
|
// 查询推送
|
|
|
JSONObject result = searchOpenService.getResultJsonObject(queryStr, searchParams);
|
|
@@ -350,9 +606,11 @@ public class SearchQueryServiceImpl implements SearchQueryService {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ // 阿里云查询出json转换项目仪器pojo
|
|
|
+ List<EquipmentListVo> equipmentList = Json2PojoUtil.toEquipmentList(pageData);
|
|
|
JSONObject pageObj = new JSONObject();
|
|
|
pageObj.put("total", total);
|
|
|
- pageObj.put("items", pageData);
|
|
|
+ pageObj.put("items", equipmentList);
|
|
|
log.info(">>>>>>pageResult("+queryStr+"): pageSize(" + pageSize +"),pageNum("+pageNum+"),total("+total+")");
|
|
|
return ResponseJson.success(pageObj.toString());
|
|
|
} else {
|
|
@@ -364,8 +622,6 @@ public class SearchQueryServiceImpl implements SearchQueryService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
/**
|
|
|
* 获取供应商(数据库)
|
|
|
*
|
|
@@ -413,7 +669,6 @@ public class SearchQueryServiceImpl implements SearchQueryService {
|
|
|
} else {
|
|
|
searchParams = searchOpenService.getSearchParams(queryStr, fetchFields, null, "supplier", 1, requestSize, "s_sort", 1);
|
|
|
}
|
|
|
- // 查询推送
|
|
|
try {
|
|
|
// 查询推送
|
|
|
JSONObject result = searchOpenService.getResultJsonObject(queryStr, searchParams);
|