Authored by hugufei

找相似优化

package com.yoho.search.restapi;
import com.yoho.search.common.utils.HttpServletRequestUtils;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.service.IProductListWithSupplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
* Created by wangnan on 2017/5/26.
*/
@Controller
public class ProductListWithSupplyController {
@Autowired
private IProductListWithSupplyService productListWithSupplyService;
/**
* 获取商品列表,支持如果传入skn无效,补充相似skn[使用本地缓存]
*/
@RequestMapping(method = RequestMethod.GET, value = "/productindex/productListWithSupply")
@ResponseBody
public SearchApiResult productList(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return productListWithSupplyService.productListWithSupply(paramMap);
}
}
package com.yoho.search.restapi;
import com.yoho.search.common.utils.HttpServletRequestUtils;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.service.ISimilarProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
* Created by wangnan on 2017/4/24.
*/
@Controller
public class SimilarProductListController {
@Autowired
private ISimilarProductService similarProductService;
/**
* 根据一堆skn找相似的skn
*
* @param request
* @return
*/
@RequestMapping(method = RequestMethod.GET, value = "/productindex/similarProductList")
@ResponseBody
public SearchApiResult similarProductList(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return similarProductService.similarProductList(paramMap);
}
}
... ... @@ -15,6 +15,8 @@ import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.scene.searchlike.SearchLikeInShopService;
import com.yoho.search.service.scene.searchlike.SearchLikeNotInShopService;
import com.yoho.search.service.scene.searchlike.SearchLikeSceneService;
import com.yoho.search.service.service.IProductListWithSupplyService;
import com.yoho.search.service.service.ISimilarProductService;
/**
* 找相似相关功能
... ... @@ -31,6 +33,10 @@ public class SearchLikeSecneController {
private SearchLikeInShopService searchLikeInShopService;
@Autowired
private SearchLikeNotInShopService searchLikeNotInShopService;
@Autowired
private IProductListWithSupplyService productListWithSupplyService;
@Autowired
private ISimilarProductService similarProductService;
@RequestMapping(method = RequestMethod.GET, value = "/searchLike")
@ResponseBody
... ... @@ -45,12 +51,35 @@ public class SearchLikeSecneController {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return searchLikeInShopService.searchLikeInShop(paramMap);
}
@RequestMapping(method = RequestMethod.GET, value = "/searchLikeNotInShop")
@ResponseBody
public SearchApiResult searchLikeNotInShop(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return searchLikeNotInShopService.searchLikeNotInShop(paramMap);
}
/**
* 获取商品列表,支持如果传入skn无效,补充相似skn
*/
@RequestMapping(method = RequestMethod.GET, value = "/productindex/productListWithSupply")
@ResponseBody
public SearchApiResult productList(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return productListWithSupplyService.productListWithSupply(paramMap);
}
/**
* 根据一堆skn找相似的skn
*
* @param request
* @return
*/
@RequestMapping(method = RequestMethod.GET, value = "/productindex/similarProductList")
@ResponseBody
public SearchApiResult similarProductList(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return similarProductService.similarProductList(paramMap);
}
}
... ...
... ... @@ -8,10 +8,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.PostConstruct;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.slf4j.Logger;
... ... @@ -22,19 +19,15 @@ import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.common.cache.SearchCacheFactory;
import com.yoho.search.common.cache.SearchCacheMatchLogger;
import com.yoho.search.common.cache.model.SearchCache;
import com.yoho.search.common.cache.aop.SearchCacheAble;
import com.yoho.search.common.utils.SearchApiResultUtils;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.base.SearchCacheService;
import com.yoho.search.service.base.SearchCommonService;
import com.yoho.search.service.base.SearchRequestParams;
import com.yoho.search.service.base.index.ProductIndexBaseService;
import com.yoho.search.service.helper.SearchCommonHelper;
import com.yoho.search.service.helper.SearchKeyWordHelper;
import com.yoho.search.service.helper.SearchParamHelper;
import com.yoho.search.service.helper.SearchSortHelper;
import com.yoho.search.service.service.IProductListWithSupplyService;
... ... @@ -55,81 +48,47 @@ public class ProductListWithSupplyServiceImpl implements IProductListWithSupplyS
@Autowired
private SearchCommonService searchCommonService;
@Autowired
private SearchKeyWordHelper searchKeyWordService;
@Autowired
private ProductIndexBaseService productIndexBaseService;
@Autowired
private ISimilarProductService similarProductService;
@Autowired
private SearchCacheService searchCacheService;
@Autowired
private SearchCacheFactory searchCacheFactory;
@Autowired
private SearchParamHelper searchParamHelper;
private SearchCache productListSearchCache;
@PostConstruct
void init() {
productListSearchCache = searchCacheFactory.getProductListSearchCache();
}
/**
* 不支持分页,最大传入skn数100,大于100只返回100条
* 不支持分页,最多返回100个
*/
@Override
@SearchCacheAble(cacheInMinute = 30, cacheName = "PRODUCT_LIST_WITH_SUPPLY")
public SearchApiResult productListWithSupply(Map<String, String> paramMap) {
try {
SearchApiResult searchResult = innerProductList(paramMap);
return searchResult;
} catch (Exception e) {
logger.error("[func=productListWithSupply][params=" + paramMap + "]", e);
return SearchApiResultUtils.errorSearchApiResult("productListWithSupply", paramMap, e);
}
}
// 1)构建基本查询参数
SearchParam searchParam = searchParamHelper.buildDefault(paramMap);
searchParam.setAggregationBuilders(null);
searchParam.setOffset(0);
searchParam.setSize(100);
// 2)查询ES
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
if (searchResult == null) {
return new SearchApiResult().setCode(500).setMessage("execption");
}
private SearchApiResult innerProductList(Map<String, String> paramMap) throws Exception {
long begin = System.currentTimeMillis();
logger.info("[func=productListWithSupply][param={}][begin={}]", paramMap, begin);
// 5)补充SKN逻辑:记录传入的skn列表,调用搜索,把返回的product列表中的skn记录到一个列表,找出无效的skn,用这些skn查找相似的,加入到之前的返回列表中返回
List<Map<String, Object>> productList = supplySkn(paramMap, searchResult, searchParam);
// 1)构造搜索参数
SearchParam searchParam = buildProductListSearchParam(paramMap);
// 7)构造返回结果
JSONObject dataMap = new JSONObject();
dataMap.put("total", productList.size());
dataMap.put("page", 1);
dataMap.put("page_size", searchParam.getSize());
dataMap.put("page_total", 1);
dataMap.put("product_list", productList);
// 2)从缓存中获取数据
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(productListSearchCache, indexName, searchParam);
if (cacheObject != null) {
SearchCacheMatchLogger.doSearchCacheMatchLog("/productindex/productListWithSupply.json", paramMap);
return new SearchApiResult().setData(cacheObject);
}
// 3)查询ES
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
if (searchResult == null) {
return new SearchApiResult().setCode(500).setMessage("execption");
}
// 4)记录关键字对应的查询结果
String queryWord = paramMap.get("query");
if (!StringUtils.isBlank(queryWord) && !searchCommonHelper.isQuerySknOrSku(queryWord)) {
searchKeyWordService.recordKeyWordByResultCount(queryWord, searchResult.getTotal());
return new SearchApiResult().setData(dataMap);
} catch (Exception e) {
logger.error("[func=productListWithSupply][params=" + paramMap + "]", e);
return SearchApiResultUtils.errorSearchApiResult("productListWithSupply", paramMap, e);
}
// 5)补充SKN逻辑:记录传入的skn列表,调用搜索,把返回的product列表中的skn记录到一个列表,找出无效的skn,用这些skn查找相似的,加入到之前的返回列表中返回
List<Map<String, Object>> productList = supplySkn(paramMap, searchResult, searchParam);
// 6)构造返回结果
JSONObject dataMap = new JSONObject();
dataMap.put("total", productList.size());
dataMap.put("page", 1);
dataMap.put("page_size", searchParam.getSize());
dataMap.put("page_total", 1);
dataMap.put("product_list", productList);
// 7)将结果存进缓存
searchCacheService.addJSONObjectToCache(productListSearchCache, indexName, searchParam, dataMap);
logger.info("[func=productListWithSupply][cost={}]", System.currentTimeMillis() - begin);
return new SearchApiResult().setData(dataMap);
}
/**
... ... @@ -216,24 +175,4 @@ public class ProductListWithSupplyServiceImpl implements IProductListWithSupplyS
}
return trueInvalidSknList;
}
private SearchParam buildProductListSearchParam(Map<String, String> paramMap) throws Exception {
// 1)验证查询条数
int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 100 : Integer.parseInt(paramMap.get("viewNum"));
int page = 1;
if (page < 1 || pageSize < 0) {
throw new IllegalArgumentException("分页参数不合法");
}
if (pageSize > 100) {
pageSize = 100;
}
// 2)构建基本查询参数
SearchParam searchParam = searchParamHelper.buildDefault(paramMap);
searchParam.setAggregationBuilders(null);
searchParam.setOffset((page - 1) * pageSize);
searchParam.setSize(pageSize);
// 3)设置排序字段
searchParam.setSortBuilders(searchSortHelper.buildSortList(paramMap));
return searchParam;
}
}
... ...