Authored by hugufei

找相似使用APO实现缓存

... ... @@ -74,7 +74,7 @@ public class ProductIndexBaseService {
productIndexIncludeFields.add(ProductIndexEsField.gender);
productIndexIncludeFields.add(ProductIndexEsField.ageLevel);
//productIndexIncludeFields.add(ProductIndexEsField.salesPhrase);
// productIndexIncludeFields.add(ProductIndexEsField.phrase);
//productIndexIncludeFields.add(ProductIndexEsField.phrase);
productIndexIncludeFields.add(ProductIndexEsField.status);
productIndexIncludeFields.add(ProductIndexEsField.goodsList);
... ...
... ... @@ -25,7 +25,6 @@ import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.models.SearchFieldBoost;
import com.yoho.search.service.base.SearchCacheService;
import com.yoho.search.service.base.SearchCommonService;
import com.yoho.search.service.base.index.ProductIndexBaseService;
import com.yoho.search.service.helper.FunctionScoreSearchHelper;
... ... @@ -57,9 +56,6 @@ public class SearchLikeHelper {
}
}
@Autowired
private SearchCacheService searchCacheService;
@PostConstruct
void init() {
// 品类权重
... ... @@ -101,7 +97,7 @@ public class SearchLikeHelper {
* @param productSkn
* @return
*/
protected JSONObject getProductInfoInEs(String productSkn) {
public JSONObject getProductInfoInEs(String productSkn) {
SearchParam searchParam = new SearchParam();
searchParam.setQuery(QueryBuilders.matchAllQuery());
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
... ... @@ -109,7 +105,10 @@ public class SearchLikeHelper {
searchParam.setFiter(boolFilter);
searchParam.setAggregationBuilders(null);
searchParam.setSize(1);
// 设置返回参数,节省带宽
List<String> includeFields = productIndexBaseService.getProductIndexIncludeFields();
searchParam.setIncludeFields(includeFields);
String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
SearchResult searchResult = searchCommonService.doSearch(productIndexName, searchParam);
if (searchResult == null) {
... ... @@ -151,12 +150,12 @@ public class SearchLikeHelper {
boolFilter.mustNot(QueryBuilders.termsQuery(ProductIndexEsField.isSeckill, "Y"));
boolFilter.mustNot(QueryBuilders.termQuery(ProductIndexEsField.isFobbiden, 1));
boolFilter.mustNot(QueryBuilders.termQuery(ProductIndexEsField.attribute, 2));
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.status, 1));
boolFilter.must(QueryBuilders.rangeQuery(ProductIndexEsField.storageNum).gte(1));
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.isOutlets, 2));
if (notProductSkns != null && !notProductSkns.isEmpty()) {
boolFilter.mustNot(QueryBuilders.termsQuery("productSkn", notProductSkns));
}
... ... @@ -222,13 +221,13 @@ public class SearchLikeHelper {
this.append(query, productInfoInEs.getString("pattern"));
return query;
}
public String genYohoQueryStringWithBrandName(JSONObject productInfoInEs) {
StringBuilder query = this.genYohoDefaultQueryString(productInfoInEs);
this.append(query, productInfoInEs.getString("brandName"));
return query.toString();
}
public String genYohoQueryStringWithOutBrandName(JSONObject productInfoInEs) {
StringBuilder query = this.genYohoDefaultQueryString(productInfoInEs);
String queryString = query.toString();
... ... @@ -248,11 +247,6 @@ public class SearchLikeHelper {
return query.toString();
}
public List<Map<String, Object>> queryProductList(SearchParam searchParam) {
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
return searchResult.getResultList();
}
public List<Map<String, Object>> queryProductList(List<SearchParam> searchParams) {
List<SearchResult> searchResults = searchCommonService.doMutiSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParams);
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
... ...
... ... @@ -18,12 +18,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.common.cache.model.SearchCache;
import com.yoho.search.common.cache.aop.SearchCacheAble;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.base.SearchRequestParams;
import com.yoho.search.service.base.index.ProductIndexBaseService;
import com.yoho.search.service.scene.common.AbstractCacheAbleService;
/**
* 店铺内找相似
... ... @@ -31,7 +30,7 @@ import com.yoho.search.service.scene.common.AbstractCacheAbleService;
* @author gufei.hu
*/
@Service
public class SearchLikeInShopService extends AbstractCacheAbleService {
public class SearchLikeInShopService {
private static final Logger logger = LoggerFactory.getLogger(SearchLikeInShopService.class);
... ... @@ -39,18 +38,14 @@ public class SearchLikeInShopService extends AbstractCacheAbleService {
private SearchLikeHelper searchLikeHelper;
@Autowired
private ProductIndexBaseService productIndexBaseService;
@Override
public SearchCache getSearchCache() {
return searchCacheFactory.getSearchLikeSearchCache();
}
/**
* 店铺内推荐
*
* @param paramMap
* @return
*/
@SearchCacheAble(cacheInMinute = 600, cacheName = "SEARCH_LIKE_IN_SHOP", includeParams = { "product_skn", "viewNum" })
public SearchApiResult searchLikeInShop(Map<String, String> paramMap) {
try {
// 1、获取参数
... ... @@ -61,30 +56,23 @@ public class SearchLikeInShopService extends AbstractCacheAbleService {
// 2、检测分页参数【默认30条,最多60条】
int pageSize = searchLikeHelper.getPageSize(paramMap);
// 3、从缓存中获取,找到则直接返回
String redisCacheKey = searchCacheKeyHelper.getSearchLikeInShopKeyCache(productSkn, pageSize);
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(this.searchCache, redisCacheKey);
if (cacheObject != null) {
return new SearchApiResult().setData(cacheObject);
}
// 4、获取当前查询的SKN的基本信息
// 3、获取当前查询的SKN的基本信息
JSONObject productInfoInEs = searchLikeHelper.getProductInfoInEs(productSkn);
if (productInfoInEs == null) {
return new SearchApiResult().setCode(400).setMessage("SKN不存在");
}
// 5、设置SearchParams
// 4、设置SearchParams
List<SearchParam> searchParams = new ArrayList<SearchParam>();
searchParams.add(this.builderSearchParam(productInfoInEs, Arrays.asList(productSkn), pageSize));
// 6、获取搜索结果[截取条数]
// 5、获取搜索结果[截取条数]
List<Map<String, Object>> tempProductList = searchLikeHelper.queryProductList(searchParams);
if (tempProductList.size() > pageSize) {
tempProductList = tempProductList.subList(0, pageSize);
}
// 7、构造真实返回结果
// 6、构造真实返回结果
List<Map<String, Object>> productListResults = new ArrayList<Map<String, Object>>();
if (!tempProductList.isEmpty()) {
productListResults = productIndexBaseService.getProductListWithPricePlan(tempProductList);
... ... @@ -96,9 +84,6 @@ public class SearchLikeInShopService extends AbstractCacheAbleService {
result.put("total", productListResults.size());
result.put("product_info", searchLikeHelper.genProductInfoResult(productInfoInEs));
result.put("product_list", productListResults);
// 8、结果加入缓存
searchCacheService.addJSONObjectToCache(this.searchCache, redisCacheKey, result);
return new SearchApiResult().setData(result);
} catch (Exception e) {
logger.error(e.getMessage(), e);
... ...
... ... @@ -18,13 +18,12 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.common.cache.model.SearchCache;
import com.yoho.search.common.cache.aop.SearchCacheAble;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.base.SearchDynamicConfigService;
import com.yoho.search.service.base.SearchRequestParams;
import com.yoho.search.service.base.index.ProductIndexBaseService;
import com.yoho.search.service.scene.common.AbstractCacheAbleService;
/**
* 店铺外找相似
... ... @@ -33,7 +32,7 @@ import com.yoho.search.service.scene.common.AbstractCacheAbleService;
*
*/
@Service
public class SearchLikeNotInShopService extends AbstractCacheAbleService {
public class SearchLikeNotInShopService{
private static final Logger logger = LoggerFactory.getLogger(SearchLikeNotInShopService.class);
... ... @@ -44,17 +43,13 @@ public class SearchLikeNotInShopService extends AbstractCacheAbleService {
@Autowired
private SearchDynamicConfigService searchDynamicConfigService;
@Override
public SearchCache getSearchCache() {
return searchCacheFactory.getSearchLikeSearchCache();
}
/**
* 店铺外推荐
*
* @param paramMap
* @return
*/
@SearchCacheAble(cacheInMinute = 600, cacheName = "SEARCH_LIKE_NOT_IN_SHOP", includeParams = { "product_skn", "viewNum" })
public SearchApiResult searchLikeNotInShop(Map<String, String> paramMap) {
try {
// 1、获取参数
... ... @@ -65,32 +60,25 @@ public class SearchLikeNotInShopService extends AbstractCacheAbleService {
// 2、检测分页参数【默认30条,最多60条】
int pageSize = searchLikeHelper.getPageSize(paramMap);
// 3、从缓存中获取,找到则直接返回
String redisCacheKey = searchCacheKeyHelper.getSearchLikeNotInShopKeyCache(productSkn, pageSize);
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(this.searchCache, redisCacheKey);
if (cacheObject != null) {
return new SearchApiResult().setData(cacheObject);
}
// 4、获取当前查询的SKN的基本信息
// 2、获取当前查询的SKN的基本信息
JSONObject productInfoInEs = searchLikeHelper.getProductInfoInEs(productSkn);
if (productInfoInEs == null) {
return new SearchApiResult().setCode(400).setMessage("SKN不存在");
}
// 5、设置SearchParams
// 3、设置SearchParams
List<SearchParam> searchParams = new ArrayList<SearchParam>();
int sameSortCount = searchDynamicConfigService.getSearchLikeNotInShopSameSortPercent() * pageSize / 100;
searchParams.add(this.builderSearchParam(productInfoInEs, Arrays.asList(productSkn), sameSortCount, true));
searchParams.add(this.builderSearchParam(productInfoInEs, Arrays.asList(productSkn), pageSize - sameSortCount, false));
// 6、获取搜索结果[截取条数]
// 4、获取搜索结果[截取条数]
List<Map<String, Object>> tempProductList = searchLikeHelper.queryProductList(searchParams);
if (tempProductList.size() > pageSize) {
tempProductList = tempProductList.subList(0, pageSize);
}
// 7、构造真实返回结果
// 5、构造真实返回结果
List<Map<String, Object>> productListResults = new ArrayList<Map<String, Object>>();
if (!tempProductList.isEmpty()) {
productListResults = productIndexBaseService.getProductListWithPricePlan(tempProductList);
... ... @@ -102,17 +90,14 @@ public class SearchLikeNotInShopService extends AbstractCacheAbleService {
result.put("total", productListResults.size());
result.put("product_info", searchLikeHelper.genProductInfoResult(productInfoInEs));
result.put("product_list", productListResults);
// 8、结果加入缓存
searchCacheService.addJSONObjectToCache(this.searchCache, redisCacheKey, result);
return new SearchApiResult().setData(result);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new SearchApiResult().setData(null).setMessage("searchLikeNotInShop Exception").setCode(500);
}
}
private SearchParam builderSearchParam(JSONObject productInfoInEs, List<String> productSkns, int pageSize, boolean inSameSort) {
// 1、设置SearchParam
SearchParam searchParam = new SearchParam();
... ...
... ... @@ -19,12 +19,11 @@ import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.common.cache.model.SearchCache;
import com.yoho.search.common.cache.aop.SearchCacheAble;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.base.SearchRequestParams;
import com.yoho.search.service.base.index.ProductIndexBaseService;
import com.yoho.search.service.scene.common.AbstractCacheAbleService;
/**
* 全站找相似功能
... ... @@ -33,7 +32,7 @@ import com.yoho.search.service.scene.common.AbstractCacheAbleService;
*
*/
@Service
public class SearchLikeSceneService extends AbstractCacheAbleService {
public class SearchLikeSceneService {
private static final Logger logger = LoggerFactory.getLogger(SearchLikeSceneService.class);
... ... @@ -42,14 +41,10 @@ public class SearchLikeSceneService extends AbstractCacheAbleService {
@Autowired
private ProductIndexBaseService productIndexBaseService;
@Override
public SearchCache getSearchCache() {
return searchCacheFactory.getSearchLikeSearchCache();
}
/**
* @找相似功能
* @全站找相似功能
*/
@SearchCacheAble(cacheInMinute = 600, cacheName = "SEARCH_LIKE", includeParams = { "product_skn", "viewNum" })
public SearchApiResult searchLike(Map<String, String> paramMap) {
try {
// 1、获取参数
... ... @@ -60,33 +55,26 @@ public class SearchLikeSceneService extends AbstractCacheAbleService {
// 2、检测分页参数【默认30条,最多60条】
int pageSize = searchLikeHelper.getPageSize(paramMap);
// 3、从缓存中获取,找到则直接返回
String redisCacheKey = searchCacheKeyHelper.getSearchLikeKeyCache(productSkn, pageSize);
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(this.searchCache, redisCacheKey);
if (cacheObject != null) {
return new SearchApiResult().setData(cacheObject);
}
// 4、获取当前查询的SKN的基本信息
// 3、获取当前查询的SKN的基本信息
JSONObject productInfoInEs = searchLikeHelper.getProductInfoInEs(productSkn);
if (productInfoInEs == null) {
return new SearchApiResult().setCode(400).setMessage("SKN不存在");
}
// 5、设置第一步SearchParam
// 4、设置第一步SearchParam
String isGlobalInEs = productInfoInEs.getString("isGlobal");
boolean isGlobal = "Y".equalsIgnoreCase(isGlobalInEs);
List<SearchParam> searchParams = new ArrayList<SearchParam>();
searchParams.add(this.buildSearchParam(productInfoInEs, 5, true, isGlobal));// 固定取同品牌的5个商品
searchParams.add(this.buildSearchParam(productInfoInEs, pageSize, false, isGlobal));
// 6、获取搜索结果[并截取条数]
// 5、获取搜索结果[并截取条数]
List<Map<String, Object>> tempProductList = searchLikeHelper.queryProductList(searchParams);
if (tempProductList.size() > pageSize) {
tempProductList = tempProductList.subList(0, pageSize);
}
// 7、构造真实返回结果
// 6、构造真实返回结果
List<Map<String, Object>> productListResults = new ArrayList<Map<String, Object>>();
if (!tempProductList.isEmpty()) {
productListResults = productIndexBaseService.getProductListWithPricePlan(tempProductList);
... ... @@ -98,9 +86,6 @@ public class SearchLikeSceneService extends AbstractCacheAbleService {
jsonObject.put("total", productListResults.size());
jsonObject.put("product_info", searchLikeHelper.genProductInfoResult(productInfoInEs));
jsonObject.put("product_list", productListResults);
// 8、结果加入缓存
searchCacheService.addJSONObjectToCache(this.searchCache, redisCacheKey, jsonObject);
return new SearchApiResult().setData(jsonObject);
} catch (Exception e) {
logger.error(e.getMessage(), e);
... ...