Authored by wangnan9279

拼团购

package com.yoho.search.common;
import com.yoho.search.base.utils.ConvertUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
... ... @@ -16,25 +17,37 @@ public class BaseService {
/**
* 增加一个termsQuery
*/
protected BoolQueryBuilder boolQueryTermsQuery(Map<String, String> paramMap, BoolQueryBuilder boolFilter, String fieldName) {
protected void boolQueryTermsQuery(Map<String, String> paramMap, BoolQueryBuilder boolFilter, String fieldName) {
if (paramMap.containsKey(fieldName) && StringUtils.isNotBlank(paramMap.get(fieldName))) {
List<Integer> values = ConvertUtils.stringToIntList(paramMap.get(fieldName), ",");
if (values == null || values.isEmpty()) {
return boolFilter;
return;
}
boolFilter.must(QueryBuilders.termsQuery(fieldName, values));
}
return boolFilter;
}
/**
* 增加一个termQuery
*/
protected BoolQueryBuilder boolQueryTermQuery(Map<String, String> paramMap, BoolQueryBuilder boolFilter, String fieldName) {
protected void boolQueryTermQuery(Map<String, String> paramMap, BoolQueryBuilder boolFilter, String fieldName) {
if (paramMap.containsKey(fieldName) && StringUtils.isNotBlank(paramMap.get(fieldName))) {
boolFilter.must(QueryBuilders.termQuery(fieldName, paramMap.get(fieldName)));
}
return boolFilter;
}
/**
* 批量增加termQuery
*/
protected void addTermQueryBatch(Map<String, String> paramMap, BoolQueryBuilder boolFilter, List<String> paramsList) {
if (CollectionUtils.isEmpty(paramsList)) {
return;
}
paramsList.stream().forEach(p -> {
if (paramMap.containsKey(p) && StringUtils.isNotBlank(paramMap.get(p))) {
boolFilter.must(QueryBuilders.termQuery(p, paramMap.get(p)));
}
});
}
/**
... ...
... ... @@ -131,6 +131,22 @@ public class SearchRequestParams {
public static final String IMAGEREPERTORY_PARAM_ITEMTYPE = "itemType";
public static final String IMAGEREPERTORY_PARAM_PORT = "port";
//Collage index
public static final String COLLAGE_PARAM_ACTIVITYID = "activityId";
public static final String COLLAGE_PARAM_PRODUCTSKN = "productSkn";
public static final String COLLAGE_PARAM_ORDERBY = "orderBy";
public static final String COLLAGE_PARAM_OPENLIMIT = "openLimit";
public static final String COLLAGE_PARAM_JOINLIMIT = "joinLimit";
public static final String COLLAGE_PARAM_ISAUTO = "isAuto";
public static final String COLLAGE_PARAM_PAYLIMIT = "payLimit";
public static final String COLLAGE_PARAM_PRODUCTLIMIT = "productLimit";
public static final String COLLAGE_PARAM_STATUS = "status";
public static final String COLLAGE_PARAM_ACTIVITYSTATUS = "activityStatus";
public static final String COLLAGE_PARAM_BEGINTIME = "beginTime";
public static final String COLLAGE_PARAM_ENDTIME = "endTime";
public static final String SHOPS_PARAM_CUSTOMIZE_TAG = "customize_tag";
... ...
package com.yoho.search.restapi.others;
import com.yoho.search.aop.cache.SearchCacheAble;
import com.yoho.search.common.utils.HttpServletRequestUtils;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.scene.others.CollageService;
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;
/**
* @author wangnan
* @version 2018/9/4
*/
@Controller
public class CollageController {
@Autowired
private CollageService collageService;
/**
* 搜索拼团购
*/
@SearchCacheAble(cacheInMinute = 1, cacheName = "COLLAGE")
@RequestMapping(method = RequestMethod.GET, value = "/collage")
@ResponseBody
public SearchApiResult collage(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return collageService.searchList(paramMap);
}
}
... ...
package com.yoho.search.service.index;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.yoho.search.base.utils.ConvertUtils;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.common.BaseService;
import com.yoho.search.common.SearchCommonService;
import com.yoho.search.common.SearchRequestParams;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
... ... @@ -15,19 +16,20 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yoho.search.base.utils.ConvertUtils;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.common.SearchCommonService;
import com.yoho.search.common.SearchRequestParams;
import com.yoho.search.common.BaseService;
import java.util.*;
@Service
public class ShopsIndexBaseService extends BaseService {
private static final Logger logger = LoggerFactory.getLogger(ShopsIndexBaseService.class);
private static final List<String> paramsList = Arrays.asList(SearchRequestParams.SHOPS_PARAM_BLKSTATUS,
SearchRequestParams.SHOPS_PARAM_CHECKSTATUS,
SearchRequestParams.SHOPS_PARAM_EXAMINESTATUS,
SearchRequestParams.SHOPS_PARAM_OPERATIONSTATUS,
SearchRequestParams.SHOPS_PARAM_SHOPSTYPE,
SearchRequestParams.SHOPS_PARAM_STATUS);
@Autowired
private SearchCommonService searchCommonService;
... ... @@ -76,7 +78,7 @@ public class ShopsIndexBaseService extends BaseService {
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
for (Object shopId : shopIds) {
Map<String, Object> shop = shopMap.get(shopId.toString());
if (shop != null && MapUtils.getIntValue(shop, "status", 0)==1) {
if (shop != null && MapUtils.getIntValue(shop, "status", 0) == 1) {
results.add(shop);
}
}
... ... @@ -102,12 +104,7 @@ public class ShopsIndexBaseService extends BaseService {
List<Integer> shopsIds = ConvertUtils.stringToIntList(paramMap.get(SearchRequestParams.PARAM_SEARCH_SHOP), ",");
boolFilter.must(QueryBuilders.termsQuery("shopsId", shopsIds));
}
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.SHOPS_PARAM_BLKSTATUS);
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.SHOPS_PARAM_CHECKSTATUS);
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.SHOPS_PARAM_EXAMINESTATUS);
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.SHOPS_PARAM_OPERATIONSTATUS);
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.SHOPS_PARAM_SHOPSTYPE);
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.SHOPS_PARAM_STATUS);
addTermQueryBatch(paramMap, boolFilter, paramsList);
boolFilter = generalHandler(paramMap, boolFilter);
return boolFilter;
}
... ...
package com.yoho.search.service.scene.others;
import com.alibaba.fastjson.JSONObject;
import com.yoho.error.event.SearchEvent;
import com.yoho.search.base.utils.DateUtil;
import com.yoho.search.base.utils.EventReportEnum;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.common.BaseService;
import com.yoho.search.common.SearchCommonService;
import com.yoho.search.common.SearchRequestParams;
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.core.es.utils.IgnoreSomeException;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.recall.beans.cache.SknReturnInfoCacheBean;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
/**
* 拼团购
*
* @author wangnan
* @version 2018/9/4
*/
@Service
public class CollageService extends BaseService implements ApplicationEventPublisherAware {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private ApplicationEventPublisher publisher;
private static final List<String> paramsList = Arrays.asList(
SearchRequestParams.COLLAGE_PARAM_ACTIVITYID,
SearchRequestParams.COLLAGE_PARAM_PRODUCTSKN,
SearchRequestParams.COLLAGE_PARAM_ORDERBY,
SearchRequestParams.COLLAGE_PARAM_OPENLIMIT,
SearchRequestParams.COLLAGE_PARAM_JOINLIMIT,
SearchRequestParams.COLLAGE_PARAM_JOINLIMIT,
SearchRequestParams.COLLAGE_PARAM_ISAUTO,
SearchRequestParams.COLLAGE_PARAM_PAYLIMIT,
SearchRequestParams.COLLAGE_PARAM_PRODUCTLIMIT);
@Autowired
private SearchCommonService searchCommonService;
@Autowired
private SknReturnInfoCacheBean sknReturnInfoCacheBean;
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.publisher = applicationEventPublisher;
}
public SearchApiResult searchList(Map<String, String> paramMap) {
try {
logger.info("[class=CollageService][func=searchList][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
// 1.构造查询参数
SearchParam searchParam = new SearchParam();
int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
if (page < 1 || pageSize < 0) {
return new SearchApiResult().setCode(400).setMessage("分页参数错误");
}
if (pageSize > 100) {
pageSize = 100;
}
searchParam.setOffset((page - 1) * pageSize);
searchParam.setSize(pageSize);
//2.配置Filter
BoolQueryBuilder boolFilter = constructHelperFilterBuilder(paramMap);
searchParam.setFiter(boolFilter);
final String indexName = ISearchConstants.INDEX_NAME_COLLAGE;
//3.查询ES
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
SearchApiResult searchApiResult = new SearchApiResult();
if (searchResult == null) {
return searchApiResult.setCode(500);
}
//4.关联ProductIndex信息
List<Integer> productSknList = new ArrayList<>(searchResult.getResultList().size());
List<Map<String, Object>> tempList = getTempMap(searchResult.getResultList(), productSknList);
List<Map<String, Object>> sknInfoList = sknReturnInfoCacheBean.queryProductListBySkn(productSknList, productSknList.size());
Map<Integer, Map<String, Object>> sknInfoMap = new HashMap<>();
sknInfoList.stream().forEach(p -> sknInfoMap.put(MapUtils.getInteger(p, "product_skn"), p));
tempList.stream().forEach(p -> {
Integer productSkn = MapUtils.getInteger(p, "product_skn");
Map<String, Object> sknMap = sknInfoMap.get(productSkn);
p.put("product_name", MapUtils.getString(sknMap, "product_name", ""));
p.put("sales_price", MapUtils.getDoubleValue(sknMap, "sales_price"));
p.put("market_price", MapUtils.getDoubleValue(sknMap, "market_price"));
p.put("sales_num", MapUtils.getIntValue(sknMap, "sales_num"));
});
Map<Map<String, Object>, Integer> salesNumSknInfoMap = tempList.stream().collect(Collectors.toMap(p -> p, p -> MapUtils.getInteger(p, "sales_num")));
Map<Map<String, Object>,Integer> finalMap = new LinkedHashMap<>();
salesNumSknInfoMap.entrySet().stream().sorted(Map.Entry.comparingByValue()).forEachOrdered(e -> finalMap.put(e.getKey(), e.getValue()));
List<Map<String, Object>> finalList = new ArrayList<>(finalMap.size());
Iterator iterator = finalMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
finalList.add((Map<String, Object>) entry.getKey());
}
Collections.reverse(finalList);
// 4.构造返回结果并加入缓存
JSONObject dataMap = new JSONObject();
dataMap.put("total", searchResult.getTotal());
dataMap.put("page_size", searchParam.getSize());
dataMap.put("page_total", searchResult.getTotalPage());
dataMap.put("list", finalList);
return new SearchApiResult().setData(dataMap);
} catch (Exception e) {
publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_IMAGEREPERTORY.getEventName(), EventReportEnum.SEARCHCONTROLLER_IMAGEREPERTORY.getFunctionName(),
EventReportEnum.SEARCHCONTROLLER_IMAGEREPERTORY.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
return SearchApiResultUtils.errorSearchApiResult(logger, paramMap, e);
}
}
private BoolQueryBuilder constructHelperFilterBuilder(Map<String, String> paramMap) throws Exception {
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
boolFilter.must(QueryBuilders.termQuery(SearchRequestParams.COLLAGE_PARAM_STATUS, 1));
boolFilter.must(QueryBuilders.termQuery(SearchRequestParams.COLLAGE_PARAM_ACTIVITYSTATUS, 1));
boolFilter.must(QueryBuilders.rangeQuery(SearchRequestParams.COLLAGE_PARAM_BEGINTIME).lte(DateUtil.getCurrentTimeSecond()));
boolFilter.must(QueryBuilders.rangeQuery(SearchRequestParams.COLLAGE_PARAM_ENDTIME).gte(DateUtil.getCurrentTimeSecond()));
addTermQueryBatch(paramMap, boolFilter, paramsList);
boolFilter = generalHandler(paramMap, boolFilter);
return boolFilter;
}
private List<Map<String, Object>> getTempMap(List<Map<String, Object>> resultList, List<Integer> productSknList) {
List<Map<String, Object>> returnList = new ArrayList<Map<String, Object>>();
for (Map<String, Object> map : resultList) {
Map<String, Object> productMap = getTempMap(map, productSknList);
returnList.add(productMap);
}
return returnList;
}
private Map<String, Object> getTempMap(Map<String, Object> map, List<Integer> productSknList) {
Map<String, Object> resultMap = new LinkedHashMap<>();
Integer productSkn = MapUtils.getIntValue(map, "productSkn");
productSknList.add(productSkn);
resultMap.put("product_skn", productSkn);
resultMap.put("people_num", MapUtils.getIntValue(map, "peopleNum"));
return resultMap;
}
}
... ...
... ... @@ -4,14 +4,14 @@ import com.alibaba.fastjson.JSONObject;
import com.yoho.error.event.SearchEvent;
import com.yoho.search.base.utils.EventReportEnum;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.common.BaseService;
import com.yoho.search.common.SearchCommonService;
import com.yoho.search.common.SearchRequestParams;
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.core.es.utils.IgnoreSomeException;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.common.SearchCommonService;
import com.yoho.search.common.SearchRequestParams;
import com.yoho.search.common.BaseService;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
... ... @@ -27,10 +27,7 @@ import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* Created by wangnan on 2016/12/13.
... ... @@ -41,6 +38,13 @@ public class HelperService extends BaseService implements ApplicationEventPublis
private ApplicationEventPublisher publisher;
private static final List<String> paramsList = Arrays.asList(SearchRequestParams.HELPER_PARAM_FIRSTCATEGORYID,
SearchRequestParams.HELPER_PARAM_SECENDCATEGORYID,
SearchRequestParams.HELPER_PARAM_HELPERTYPE,
SearchRequestParams.HELPER_PARAM_ISFAQ,
SearchRequestParams.HELPER_PARAM_ISROBOTQA,
SearchRequestParams.HELPER_PARAM_SHOWPLATFORM);
@Autowired
private SearchCommonService searchCommonService;
... ... @@ -128,17 +132,12 @@ public class HelperService extends BaseService implements ApplicationEventPublis
private BoolQueryBuilder constructHelperFilterBuilder(Map<String, String> paramMap) throws Exception {
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
if (paramMap.containsKey(SearchRequestParams.HELPER_PARAM_ISCUSTOMERSERVICE) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.HELPER_PARAM_ISCUSTOMERSERVICE))) {
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.HELPER_PARAM_ISCUSTOMERSERVICE);
boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.HELPER_PARAM_ISCUSTOMERSERVICE);
} else if (!paramMap.containsKey("showCustomerService") || !StringUtils.isNotBlank(paramMap.get("showCustomerService")) || !paramMap.get("showCustomerService").equals("Y")) {
boolFilter.must(QueryBuilders.termQuery(SearchRequestParams.HELPER_PARAM_ISCUSTOMERSERVICE, "N"));
}
boolFilter.must(QueryBuilders.termQuery(SearchRequestParams.HELPER_PARAM_STATUS, 1));
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.HELPER_PARAM_FIRSTCATEGORYID);
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.HELPER_PARAM_SECENDCATEGORYID);
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.HELPER_PARAM_HELPERTYPE);
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.HELPER_PARAM_ISFAQ);
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.HELPER_PARAM_ISROBOTQA);
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.HELPER_PARAM_SHOWPLATFORM);
addTermQueryBatch(paramMap, boolFilter, paramsList);
boolFilter = generalHandler(paramMap, boolFilter);
return boolFilter;
}
... ...
... ... @@ -22,13 +22,11 @@ import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* 图库服务
*
* @Author wangnan
* @Date: Created in 2018/4/16
*/
... ... @@ -39,6 +37,16 @@ public class ImageRepertoryService extends BaseService implements ApplicationEve
private ApplicationEventPublisher publisher;
private static final List<String> paramsList = Arrays.asList(
SearchRequestParams.IMAGEREPERTORY_PARAM_ID,
SearchRequestParams.IMAGEREPERTORY_PARAM_CHANNEL,
SearchRequestParams.IMAGEREPERTORY_PARAM_SEASON,
SearchRequestParams.IMAGEREPERTORY_PARAM_IMAGESIZE,
SearchRequestParams.IMAGEREPERTORY_PARAM_IMAGENAME,
SearchRequestParams.IMAGEREPERTORY_PARAM_IMAGETYPE,
SearchRequestParams.IMAGEREPERTORY_PARAM_ITEMTYPE,
SearchRequestParams.IMAGEREPERTORY_PARAM_PORT);
@Autowired
private SearchCommonService searchCommonService;
... ... @@ -85,15 +93,8 @@ public class ImageRepertoryService extends BaseService implements ApplicationEve
private BoolQueryBuilder constructHelperFilterBuilder(Map<String, String> paramMap) throws Exception {
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_ID);
boolFilter = boolQueryTermsQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_ITEMID);
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_CHANNEL);
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_SEASON);
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_IMAGESIZE);
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_IMAGENAME);
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_IMAGETYPE);
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_ITEMTYPE);
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_PORT);
boolQueryTermsQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_ITEMID);
addTermQueryBatch(paramMap, boolFilter, paramsList);
boolFilter = generalHandler(paramMap, boolFilter);
return boolFilter;
}
... ...