Authored by 胡古飞

重构constructFilterBuilder方法

... ... @@ -181,6 +181,16 @@ public class SearchServiceHelper {
}
}
private boolean checkParamNotFiltered(Map<String, String> paramMap, String filterParamName, String paramName) {
if (!paramMap.containsKey(paramName) || StringUtils.isBlank(paramMap.get(paramName))) {
return false;
}
if (StringUtils.isNotBlank(filterParamName) && paramName.equals(filterParamName)) {
return false;
}
return true;
}
/**
* 构造通用的过滤条件
*
... ... @@ -192,43 +202,54 @@ public class SearchServiceHelper {
*/
public BoolQueryBuilder constructFilterBuilder(Map<String, String> paramMap, String filterParamName) throws Exception {
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
// 品牌
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_BRAND) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_BRAND))
&& !SearchRequestParams.PARAM_SEARCH_BRAND.equals(filterParamName)) {
// //////////////////////////////////////////支持传多个参数,以逗号分隔///////////////////////////////////////////////////////////
// 性别
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_GENDER)) {
int[] genders = ConvertUtils.stringToIntArray(paramMap.get(SearchRequestParams.PARAM_SEARCH_GENDER), ",");
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.gender, genders));
}
// 过滤品牌
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_BRAND)) {
int[] brandids = ConvertUtils.stringToIntArray(paramMap.get(SearchRequestParams.PARAM_SEARCH_BRAND), ",");
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.brandId, brandids));
}
if ("Y".equals(paramMap.get(SearchRequestParams.PARAM_SEARCH_GLOBAL_FILTER_BRAND)) && StringUtils.isNotBlank(paramMap.get("pageId"))) {
boolFilter.mustNot(QueryBuilders.termQuery(ProductIndexEsField.forbiddenPageIds, paramMap.get("pageId")));
// 人群
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_AGELEVEL)) {
int[] ageLevels = ConvertUtils.stringToIntArray(paramMap.get(SearchRequestParams.PARAM_SEARCH_AGELEVEL), ",");
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.ageLevel, ageLevels));
}
// 店铺
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_SHOP) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_SHOP))
&& !SearchRequestParams.PARAM_SEARCH_SHOP.equals(filterParamName)) {
int[] shopids = ConvertUtils.stringToIntArray(paramMap.get(SearchRequestParams.PARAM_SEARCH_SHOP), ",");
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.shopId, shopids));
// 颜色
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_COLOR)) {
int[] colorids = ConvertUtils.stringToIntArray(paramMap.get(SearchRequestParams.PARAM_SEARCH_COLOR), ",");
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.colorIds, colorids));
}
// 大分类
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_MAXSORT) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_MAXSORT))
&& !SearchRequestParams.PARAM_SEARCH_MAXSORT.equals(filterParamName)) {
// 尺码
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_SIZE)) {
int[] sizeids = ConvertUtils.stringToIntArray(paramMap.get(SearchRequestParams.PARAM_SEARCH_SIZE), ",");
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.sizeIds, sizeids));
}
// 风格
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_STYLE)) {
int[] styleIds = ConvertUtils.stringToIntArray(paramMap.get(SearchRequestParams.PARAM_SEARCH_STYLE), ",");
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.styleIds, styleIds));
}
// 过滤大分类
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_MAXSORT)) {
int[] maxsortids = ConvertUtils.stringToIntArray(paramMap.get(SearchRequestParams.PARAM_SEARCH_MAXSORT), ",");
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.maxSortId, maxsortids));
}
// 中类
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_MIDDLESORT) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_MIDDLESORT))
&& !SearchRequestParams.PARAM_SEARCH_MAXSORT.equals(filterParamName)) {
// 过滤中分类
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_MIDDLESORT)) {
int[] middlesortids = ConvertUtils.stringToIntArray(paramMap.get(SearchRequestParams.PARAM_SEARCH_MIDDLESORT), ",");
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.middleSortId, middlesortids));
}
// 小类
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_SMALLSORT) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_SMALLSORT))) {
// 过滤小分类
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_SMALLSORT)) {
int[] smallsortids = ConvertUtils.stringToIntArray(paramMap.get(SearchRequestParams.PARAM_SEARCH_SMALLSORT), ",");
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.smallSortId, smallsortids));
}
// 大中小分类
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_COMMONSORT) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_COMMONSORT))) {
// 过滤通用分类
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_COMMONSORT)) {
int[] commonSortIds = ConvertUtils.stringToIntArray(paramMap.get(SearchRequestParams.PARAM_SEARCH_COMMONSORT), ",");
BoolQueryBuilder commonSortIdFilter = QueryBuilders.boolQuery();
commonSortIdFilter.should(QueryBuilders.termsQuery(ProductIndexEsField.maxSortId, commonSortIds));
... ... @@ -236,256 +257,217 @@ public class SearchServiceHelper {
commonSortIdFilter.should(QueryBuilders.termsQuery(ProductIndexEsField.smallSortId, commonSortIds));
boolFilter.must(commonSortIdFilter);
}
// 颜色
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_COLOR) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_COLOR))
&& !SearchRequestParams.PARAM_SEARCH_COLOR.equals(filterParamName)) {
int[] colorids = ConvertUtils.stringToIntArray(paramMap.get(SearchRequestParams.PARAM_SEARCH_COLOR), ",");
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.colorIds, colorids));
// 过滤店铺
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_SHOP)) {
int[] shopids = ConvertUtils.stringToIntArray(paramMap.get(SearchRequestParams.PARAM_SEARCH_SHOP), ",");
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.shopId, shopids));
}
// 尺码
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_SIZE) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_SIZE))
&& !SearchRequestParams.PARAM_SEARCH_SIZE.equals(filterParamName)) {
int[] sizeids = ConvertUtils.stringToIntArray(paramMap.get(SearchRequestParams.PARAM_SEARCH_SIZE), ",");
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.sizeIds, sizeids));
// 促销标记
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_ISPROMOTION)) {
int[] ispromotions = ConvertUtils.stringToIntArray(paramMap.get(SearchRequestParams.PARAM_SEARCH_ISPROMOTION), ",");
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.ispromotion, ispromotions));
}
// 销售价格
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_PRICE) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_PRICE))
&& !SearchRequestParams.PARAM_SEARCH_PRICE.equals(filterParamName)) {
String[] prices = paramMap.get(SearchRequestParams.PARAM_SEARCH_PRICE).split(",");
if (prices.length < 2) {
throw new IllegalArgumentException("价格[price]参数(" + paramMap.get(SearchRequestParams.PARAM_SEARCH_PRICE) + ")为非区间值!");
}
double begin = Double.parseDouble(prices[0]);
double end = Double.parseDouble(prices[1]);
boolFilter.must(QueryBuilders.rangeQuery(ProductIndexEsField.salesPrice).gte(begin).lte(end));
// APP类型
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_APPTYPE)) {
int[] appTypes = ConvertUtils.stringToIntArray(paramMap.get(SearchRequestParams.PARAM_SEARCH_APPTYPE), ",");
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.appType, appTypes));
}
// 性别
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_GENDER) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_GENDER))
&& !SearchRequestParams.PARAM_SEARCH_GENDER.equals(filterParamName)) {
int[] genders = ConvertUtils.stringToIntArray(paramMap.get(SearchRequestParams.PARAM_SEARCH_GENDER), ",");
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.gender, genders));
// SKN
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SYNC_SKN)) {
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, paramMap.get(SearchRequestParams.PARAM_SYNC_SKN.split(","))));
}
// //////////////////////////////////////////不支持多个参数///////////////////////////////////////////////////////////
// 是否特价
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_SPECIALOFFER) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_SPECIALOFFER))) {
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_SPECIALOFFER)) {
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.specialoffer, paramMap.get(SearchRequestParams.PARAM_SEARCH_SPECIALOFFER)));
}
// 是否打折
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_ISDISCOUNT) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_ISDISCOUNT))) {
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_ISDISCOUNT)) {
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.isDiscount, paramMap.get(SearchRequestParams.PARAM_SEARCH_ISDISCOUNT)));
}
// 是否为学生价
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_STUDENTPRICE) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_STUDENTPRICE))) {
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_STUDENTPRICE)) {
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.isStudentPrice, paramMap.get(SearchRequestParams.PARAM_SEARCH_STUDENTPRICE)));
}
// 是否学生返币
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_STUDENTREBATE) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_STUDENTREBATE))) {
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_STUDENTREBATE)) {
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.isstudentrebate, paramMap.get(SearchRequestParams.PARAM_SEARCH_STUDENTREBATE)));
}
// 是否支持分期
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_ISINSTALMENT) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_ISINSTALMENT))) {
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_ISINSTALMENT)) {
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.isInstalment, paramMap.get(SearchRequestParams.PARAM_SEARCH_ISINSTALMENT)));
}
// 属性(待研究)
if (!"parameter".equals(filterParamName)) {
Iterator<String> itParamKeys = paramMap.keySet().iterator();
StringBuilder standardValues = new StringBuilder();
while (itParamKeys.hasNext()) {
String paramKey = itParamKeys.next();
if (paramKey.contains("parameter")) {
if (paramKey.contains("_")) {
standardValues.append(",").append(paramKey.split("_")[1]).append(paramMap.get(paramKey));
}
}
}
if (standardValues.length() > 0) {
long[] standards = ConvertUtils.stringToLongArray(standardValues.toString().replaceFirst(",", ""), ",");
for (long standard : standards) {
// boolFilter.must(FilterBuilders.termFilter("standardIds",
// standard));
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.standardIds, standard));
}
}
}
// 是否在售
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_ISSALES) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_ISSALES))) {
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_ISSALES)) {
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.isSales, paramMap.get(SearchRequestParams.PARAM_SEARCH_ISSALES)));
}
// 促销标记
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_ISPROMOTION) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_ISPROMOTION))) {
int[] ispromotions = ConvertUtils.stringToIntArray(paramMap.get(SearchRequestParams.PARAM_SEARCH_ISPROMOTION), ",");
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.ispromotion, ispromotions));
}
// vip折扣类型
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_VIPDISCOUNTTYPE) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_VIPDISCOUNTTYPE))) {
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_VIPDISCOUNTTYPE)) {
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.vipDiscountType, paramMap.get(SearchRequestParams.PARAM_SEARCH_VIPDISCOUNTTYPE)));
}
// 促销折扣
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_PD) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_PD))) {
String[] discounts = paramMap.get(SearchRequestParams.PARAM_SEARCH_PD).split(",");
if (discounts.length < 2) {
throw new IllegalArgumentException("折扣[pd]参数(" + paramMap.get(SearchRequestParams.PARAM_SEARCH_PD) + ")为非区间值!");
}
double begin = Double.parseDouble(discounts[0]);
double end = Double.parseDouble(discounts[1]);
boolFilter.must(QueryBuilders.rangeQuery(ProductIndexEsField.promotionDiscount).gte(begin).lte(end));
}
// 商品属性 1正常商品 2赠品
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_ATTRIBUTE) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_ATTRIBUTE))) {
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_ATTRIBUTE)) {
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.attribute, paramMap.get(SearchRequestParams.PARAM_SEARCH_ATTRIBUTE)));
}
// 商品属性 1赠品 2正常商品
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_ATTRIBUTE_NOT) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_ATTRIBUTE_NOT))) {
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_ATTRIBUTE_NOT)) {
boolFilter.mustNot(QueryBuilders.termQuery(ProductIndexEsField.attribute, paramMap.get(SearchRequestParams.PARAM_SEARCH_ATTRIBUTE_NOT)));
}
// 库存
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_STOCKNUM) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_STOCKNUM))) {
// 获取今天往后n天的unix时间戳;
long now = DateUtil.getTimeSecondByDate(new Date());
int begin = Integer.parseInt(paramMap.get(SearchRequestParams.PARAM_SEARCH_STOCKNUM));
if (begin == 0) {
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.storageNum, begin));
} else {
if (StringUtils.isNotBlank(paramMap.get("showSoldOut")) && paramMap.get("showSoldOut").equals("1")) {
boolFilter.should(QueryBuilders.rangeQuery(ProductIndexEsField.storageNum).gte(begin)).should(
QueryBuilders.boolQuery().must(QueryBuilders.termQuery(ProductIndexEsField.storageNum, 0))
.must(QueryBuilders.rangeQuery(ProductIndexEsField.storageUpdateTime).gte(now)));
} else {
boolFilter.must(QueryBuilders.rangeQuery(ProductIndexEsField.storageNum).gte(begin));
}
}
}
// 是否限量
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_ISLIMITED) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_ISLIMITED))) {
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_ISLIMITED)) {
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.islimited, paramMap.get(SearchRequestParams.PARAM_SEARCH_ISLIMITED)));
}
// 是否限购
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_ISLIMITEDBUY) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_ISLIMITEDBUY))) {
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_ISLIMITEDBUY)) {
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.isLimitbuy, paramMap.get(SearchRequestParams.PARAM_SEARCH_ISLIMITEDBUY)));
}
// 是否预售
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_ISADVANCE) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_ISADVANCE))) {
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_ISADVANCE)) {
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.isAdvance, paramMap.get(SearchRequestParams.PARAM_SEARCH_ISADVANCE)));
}// 是否定金预售
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_ISDESPOSITADVANCE) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_ISDESPOSITADVANCE))) {
}
// 是否定金预售
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_ISDESPOSITADVANCE)) {
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.isDepositAdvance, paramMap.get(SearchRequestParams.PARAM_SEARCH_ISDESPOSITADVANCE)));
}
// 是否新品
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_ISNEW) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_ISNEW))) {
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_ISNEW)) {
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.isnew, paramMap.get(SearchRequestParams.PARAM_SEARCH_ISNEW)));
}
// 是否奥莱
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_ISOUTLETS) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_ISOUTLETS))) {
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_ISOUTLETS)) {
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.isOutlets, paramMap.get(SearchRequestParams.PARAM_SEARCH_ISOUTLETS)));
}
// 状态是否上架
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_STATUS) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_STATUS))) {
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_STATUS)) {
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.status, paramMap.get(SearchRequestParams.PARAM_SEARCH_STATUS)));
}
// APP类型
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_APPTYPE) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_APPTYPE))
&& !SearchRequestParams.PARAM_SEARCH_APPTYPE.equals(filterParamName)) {
int[] appTypes = ConvertUtils.stringToIntArray(paramMap.get(SearchRequestParams.PARAM_SEARCH_APPTYPE), ",");
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.appType, appTypes));
}
// 风格
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_STYLE) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_STYLE))
&& !SearchRequestParams.PARAM_SEARCH_STYLE.equals(filterParamName)) {
int[] styleids = ConvertUtils.stringToIntArray(paramMap.get(SearchRequestParams.PARAM_SEARCH_STYLE), ",");
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.styleIds, styleids));
}
// 销售平台 (网站、APP、现场)
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_SELLCHANNELS) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_SELLCHANNELS))) {
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_SELLCHANNELS)) {
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.sellChannels, paramMap.get(SearchRequestParams.PARAM_SEARCH_SELLCHANNELS)));
}
if (paramMap.containsKey("folder_id") && StringUtils.isNotBlank(paramMap.get("folder_id"))) {
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.folderId, paramMap.get("folder_id")));
// folderId
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_FOLDERID)) {
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.folderId, paramMap.get(SearchRequestParams.PARAM_SEARCH_FOLDERID)));
}
// series_id
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_SERIESID)) {
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.seriesId, paramMap.get(SearchRequestParams.PARAM_SEARCH_SERIESID)));
}
// 判断是否需要是筛选全球购[全球购的筛选项]
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_IS_GLOBAL)) {
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.isGlobal, paramMap.get(SearchRequestParams.PARAM_SEARCH_IS_GLOBAL)));
}
// 用户VIP级别过滤
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.USER_VIP_LEVEL)) {
List<String> userVipLevels = this.getUserVipLevels(paramMap.get(SearchRequestParams.USER_VIP_LEVEL));
if (userVipLevels != null) {
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.vipLevels, userVipLevels));
}
}
// 断码(breakingRate>=50 || basePinRatio>=3.5)
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_BREAKING) && "1".equals(paramMap.get(SearchRequestParams.PARAM_SEARCH_BREAKING))) {
BoolQueryBuilder breakingFilter = QueryBuilders.boolQuery();
breakingFilter.should(QueryBuilders.rangeQuery(ProductIndexEsField.breakingRate).from(50));
breakingFilter.should(QueryBuilders.rangeQuery(ProductIndexEsField.basePinRatio).from(3.5));
boolFilter.must(breakingFilter);
}
// 屏蔽页面判断
if ("Y".equals(paramMap.get(SearchRequestParams.PARAM_SEARCH_GLOBAL_FILTER_BRAND)) && StringUtils.isNotBlank(paramMap.get("pageId"))) {
boolFilter.mustNot(QueryBuilders.termQuery(ProductIndexEsField.forbiddenPageIds, paramMap.get("pageId")));
}
// //////////////////////////////////////////////////范围过滤////////////////////////////////////////////////////////////
// 销售价格
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_PRICE)) {
try {
String[] prices = paramMap.get(SearchRequestParams.PARAM_SEARCH_PRICE).split(",");
double begin = Double.parseDouble(prices[0]);
double end = Double.parseDouble(prices[1]);
boolFilter.must(QueryBuilders.rangeQuery(ProductIndexEsField.salesPrice).gte(begin).lte(end));
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
if (paramMap.containsKey("series_id") && StringUtils.isNotBlank(paramMap.get("series_id"))) {
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.seriesId, paramMap.get("series_id")));
// 促销折扣
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_PD)) {
try {
String[] discounts = paramMap.get(SearchRequestParams.PARAM_SEARCH_PD).split(",");
double begin = Double.parseDouble(discounts[0]);
double end = Double.parseDouble(discounts[1]);
boolFilter.must(QueryBuilders.rangeQuery(ProductIndexEsField.promotionDiscount).gte(begin).lte(end));
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
// 促销折扣
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_PDINT) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_PDINT))) {
String[] discounts = paramMap.get(SearchRequestParams.PARAM_SEARCH_PDINT).split(",");
if (discounts.length < 2) {
throw new IllegalArgumentException("折扣[p_d_int]参数(" + paramMap.get(SearchRequestParams.PARAM_SEARCH_PDINT) + ")为非区间值!");
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_PDINT)) {
try {
String[] discounts = paramMap.get(SearchRequestParams.PARAM_SEARCH_PDINT).split(",");
double begin = Double.parseDouble(discounts[0]);
double end = Double.parseDouble(discounts[1]);
boolFilter.must(QueryBuilders.rangeQuery(ProductIndexEsField.promotionDiscountInt).gte(begin).lte(end));
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
double begin = Double.parseDouble(discounts[0]);
double end = Double.parseDouble(discounts[1]);
boolFilter.must(QueryBuilders.rangeQuery(ProductIndexEsField.promotionDiscountInt).gte(begin).lte(end));
}
// 首次上架时间间隔
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_FIRST_SHELVE_TIME) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_FIRST_SHELVE_TIME))
&& !SearchRequestParams.PARAM_SEARCH_FIRST_SHELVE_TIME.equals(filterParamName)) {
String[] times = paramMap.get(SearchRequestParams.PARAM_SEARCH_FIRST_SHELVE_TIME).split(",");
if (times.length < 2) {
throw new IllegalArgumentException("首次上架时间[first_shelve_time]参数(" + paramMap.get(SearchRequestParams.PARAM_SEARCH_FIRST_SHELVE_TIME) + ")为非区间值!");
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_FIRST_SHELVE_TIME)) {
try {
String[] times = paramMap.get(SearchRequestParams.PARAM_SEARCH_FIRST_SHELVE_TIME).split(",");
double begin = Double.parseDouble(times[0]);
double end = Double.parseDouble(times[1]);
boolFilter.must(QueryBuilders.rangeQuery(ProductIndexEsField.firstShelveTime).from(begin).to(end));
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
double begin = Double.parseDouble(times[0]);
double end = Double.parseDouble(times[1]);
boolFilter.must(QueryBuilders.rangeQuery(ProductIndexEsField.firstShelveTime).from(begin).to(end));
}
// 上架日期间隔
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_SHELVETIME) && !SearchRequestParams.PARAM_SEARCH_SHELVETIME.equals(filterParamName)) {
String[] times = paramMap.get(SearchRequestParams.PARAM_SEARCH_SHELVETIME).split(",");
if (times.length < 2) {
throw new IllegalArgumentException("上架时间[shelve_time]参数(" + paramMap.get(SearchRequestParams.PARAM_SEARCH_SHELVETIME) + ")为非区间值!");
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_SHELVETIME)) {
try {
String[] times = paramMap.get(SearchRequestParams.PARAM_SEARCH_SHELVETIME).split(",");
double begin = Double.parseDouble(times[0]);
double end = Double.parseDouble(times[1]);
boolFilter.must(QueryBuilders.rangeQuery(ProductIndexEsField.shelveTime).from(begin).to(end));
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
double begin = Double.parseDouble(times[0]);
double end = Double.parseDouble(times[1]);
boolFilter.must(QueryBuilders.rangeQuery(ProductIndexEsField.shelveTime).from(begin).to(end));
}
// 上架日期
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_DAY) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_DAY))) {
String recentDay = paramMap.get(SearchRequestParams.PARAM_SEARCH_DAY);
if (!DateUtil.isDate(recentDay)) {
throw new IllegalArgumentException("wrong date format");
if (this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_DAY)) {
try {
String recentDay = paramMap.get(SearchRequestParams.PARAM_SEARCH_DAY);
if (!DateUtil.isDate(recentDay)) {
throw new IllegalArgumentException("wrong date format");
}
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.shelveDay, recentDay));
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.shelveDay, recentDay));
}
// 增加活动模板相关的过滤条件
// ///////////////////////////////////////////特殊过滤/////////////////////////////////////////////////////////
// 库存相关的过滤条件
QueryBuilder stocknumberQueryBuilder = this.getStorageNumQueryBuilder(paramMap, filterParamName);
if (stocknumberQueryBuilder != null) {
boolFilter.must(stocknumberQueryBuilder);
}
// 活动模板相关的过滤条件
BoolQueryBuilder activitiesTermsBuilder = searchCommonHelper.getActivitiesTermsBuilder(paramMap);
if (activitiesTermsBuilder != null) {
boolFilter.must(QueryBuilders.nestedQuery(ProductIndexEsField.activities, activitiesTermsBuilder));
}
// 断码(breakingRate>=50 || basePinRatio>=3.5)
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_BREAKING) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_BREAKING))) {
String breaking = paramMap.get(SearchRequestParams.PARAM_SEARCH_BREAKING);
if ("1".equals(breaking)) {
boolFilter.must(QueryBuilders.boolQuery().should(QueryBuilders.rangeQuery(ProductIndexEsField.breakingRate).from(50))
.should(QueryBuilders.rangeQuery(ProductIndexEsField.basePinRatio).from(3.5)));
}
}
// SKN
if (paramMap.containsKey(SearchRequestParams.PARAM_SYNC_SKN) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SYNC_SKN))) {
String sknString = paramMap.get(SearchRequestParams.PARAM_SYNC_SKN);
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, sknString.split(",")));
}
// 年龄层
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_AGELEVEL) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_AGELEVEL))
&& !SearchRequestParams.PARAM_SEARCH_AGELEVEL.equals(filterParamName)) {
int[] ageLevels = ConvertUtils.stringToIntArray(paramMap.get(SearchRequestParams.PARAM_SEARCH_AGELEVEL), ",");
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.ageLevel, ageLevels));
}
// 判断是否需要包含全球购[全球购的开关]
if (!searchCommonHelper.containGlobal(paramMap)) {
boolFilter.mustNot(QueryBuilders.termsQuery(ProductIndexEsField.isGlobal, "Y"));
}
// 判断是否需要是筛选全球购[全球购的筛选项]
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_IS_GLOBAL) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_IS_GLOBAL))) {
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.isGlobal, paramMap.get(SearchRequestParams.PARAM_SEARCH_IS_GLOBAL)));
}
// 如果contain_seckill!=Y,则过滤掉秒杀商品
if (!paramMap.containsKey("contain_seckill") || !"Y".equals(paramMap.get("contain_seckill"))) {
boolFilter.mustNot(QueryBuilders.termsQuery(ProductIndexEsField.isSeckill, "Y"));
}
// 用户VIP级别过滤
String userVipLevel = (paramMap.get(SearchRequestParams.USER_VIP_LEVEL));
if (StringUtils.isNotBlank(userVipLevel)) {
List<String> userVipLevels = this.getUserVipLevels(userVipLevel);
if (userVipLevels != null) {
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.vipLevels, userVipLevels));
}
}
// 通用的过滤请求
for (String key : paramMap.keySet()) {
if (key.startsWith("not_")) {
... ... @@ -501,6 +483,29 @@ public class SearchServiceHelper {
boolFilter.mustNot(QueryBuilders.existsQuery(field));
}
}
// 规则
if (!"parameter".equals(filterParamName)) {
Iterator<String> itParamKeys = paramMap.keySet().iterator();
StringBuilder standardValues = new StringBuilder();
while (itParamKeys.hasNext()) {
String paramKey = itParamKeys.next();
if (paramKey.contains("parameter")) {
if (paramKey.contains("_")) {
standardValues.append(",").append(paramKey.split("_")[1]).append(paramMap.get(paramKey));
}
}
}
if (standardValues.length() > 0) {
long[] standards = ConvertUtils.stringToLongArray(standardValues.toString().replaceFirst(",", ""), ",");
for (long standard : standards) {
// boolFilter.must(FilterBuilders.termFilter("standardIds",
// standard));
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.standardIds, standard));
}
}
}
if (boolFilter.hasClauses()) {
return boolFilter;
} else {
... ... @@ -508,6 +513,26 @@ public class SearchServiceHelper {
}
}
private QueryBuilder getStorageNumQueryBuilder(Map<String, String> paramMap, String filterParamName) {
if (!this.checkParamNotFiltered(paramMap, filterParamName, SearchRequestParams.PARAM_SEARCH_STOCKNUM)) {
return null;
}
int begin = Integer.parseInt(paramMap.get(SearchRequestParams.PARAM_SEARCH_STOCKNUM));
if (begin == 0) {
return QueryBuilders.termQuery(ProductIndexEsField.storageNum, 0);
}
if (!"1".equals(paramMap.get("showSoldOut"))) {
return QueryBuilders.rangeQuery(ProductIndexEsField.storageNum).gte(begin);
}
// 延期显示[( 库存>=begin) ||(storageNum<begin & storageUpdateTime>now)]
BoolQueryBuilder storageNumFilter = QueryBuilders.boolQuery();
storageNumFilter.should(QueryBuilders.rangeQuery(ProductIndexEsField.storageNum).gte(begin));
storageNumFilter.should(QueryBuilders.boolQuery().must(QueryBuilders.rangeQuery(ProductIndexEsField.storageNum).to(begin))
.must(QueryBuilders.rangeQuery(ProductIndexEsField.storageUpdateTime).gte(DateUtil.getTimeSecondByDate(new Date()))));
return storageNumFilter;
}
private List<String> getUserVipLevels(String userVipLevel) {
if ("1".equals(userVipLevel)) {
return Arrays.asList("1");
... ...
... ... @@ -47,6 +47,10 @@ public class SearchRequestParams {
// 2赠品
public static final String PARAM_SEARCH_SELLCHANNELS = "sell_channels"; // ◆销售平台
// (网站、APP、现场)
public static final String PARAM_SEARCH_FOLDERID = "folder_id"; //
public static final String PARAM_SEARCH_SERIESID = "series_id"; //
// (网站、APP、现场)
public static final String PARAM_SEARCH_ATTRIBUTE_NOT = "attribute_not"; // ◆商品属性
public static final String PARAM_SEARCH_STATUS = "status"; // 查询是否上架
public static final String PARAM_SEARCH_KEYWORD = "query";// 检索词
... ...