Authored by 胡古飞

商品数量大于2000个时,自动以一个月内的新品往上计算数量

... ... @@ -5,6 +5,7 @@ import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
... ... @@ -79,11 +80,15 @@ public class PersonalVectorFeatureSearch {
* @param functionScoreQueryBuilder
* @param paramMap
*/
public void addCommonPersonalizedScriptScore(FunctionScoreQueryBuilder functionScoreQuery, QueryBuilder scoreFilter, Map<String, String> paramMap) {
public void addCommonPersonalizedScriptScore(FunctionScoreQueryBuilder functionScoreQuery, BoolQueryBuilder paramFilter, Map<String, String> paramMap) {
// 1、获取PersonalizedSearch
PersonalizedSearch personalizedSearch = this.getPersonalizedSearch(paramMap);
if (personalizedSearch == null) {
return;
personalizedSearch = new PersonalizedSearch("1", "1", "1");
//return;
}
// 2、为个性化打分添加filter条件
BoolQueryBuilder scoreFilter = productCountService.genScoreFilter(paramFilter);
this.addFeatureFactorVectorScore(functionScoreQuery, scoreFilter, personalizedSearch.getUserVectorFeature(), personalizedSearch.getVectorFeatureVersion());
}
... ...
... ... @@ -104,23 +104,20 @@ public class FunctionScoreSearchHelper {
// 1、个性化相关的统一处理逻辑
public void addPersonalizedSearch(FunctionScoreQueryBuilder functionScoreQueryBuilder, BoolQueryBuilder paramFilter, Map<String, String> paramMap) {
// 判断是否开启了个性化搜索
// 1、判断是否开启了个性化搜索
if (!searchCommonHelper.isNeedPersonalSearch(paramMap)) {
return;
}
// 2、为个性化打分添加filter条件
BoolQueryBuilder scoreFilter = productCountService.genScoreFilter(paramFilter);
// 2、执行个性化脚本打分
personalVectorFeatureSearch.addCommonPersonalizedScriptScore(functionScoreQueryBuilder, paramFilter, paramMap);
// 3、执行个性化脚本打分
personalVectorFeatureSearch.addCommonPersonalizedScriptScore(functionScoreQueryBuilder, scoreFilter, paramMap);
// 4、针对首次上架时间打分
// 3、针对首次上架时间打分
if (searchCommonHelper.isNewRecPageDefault(paramMap)) {
this.addFirstShelveTimeScore(functionScoreQueryBuilder, getNewArrivalPageShelveTimeScore());
} else {
this.addFirstShelveTimeScore(functionScoreQueryBuilder, getOtherPageShelveTimeScore());
}
// 5、针对断码率高的商品降分
// 4、针对断码率高的商品降分
this.addBreakSizeProductSearch(functionScoreQueryBuilder, paramMap);
}
... ...
... ... @@ -8,6 +8,7 @@ import javax.annotation.PostConstruct;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -126,12 +127,13 @@ public class ProductCountServiceImpl implements IProductCountService {
BoolQueryBuilder mustFilter = QueryBuilders.boolQuery();
mustFilter.must(QueryBuilders.termQuery("status", 1));
mustFilter.mustNot(QueryBuilders.termQuery("attribute", 2));
mustFilter.must(QueryBuilders.rangeQuery("storageNum").gte(1));
mustFilter.must(QueryBuilders.rangeQuery("storageNum").gte(10));
mustFilter.must(QueryBuilders.rangeQuery("breakSizePercent").to(50));
mustFilter.must(QueryBuilders.termQuery("isOutlets", 2));
return mustFilter;
}
private long queryCount(BoolQueryBuilder paramFilter, BoolQueryBuilder scoreFilter) {
private long queryCount(QueryBuilder paramFilter, BoolQueryBuilder scoreFilter) {
BoolQueryBuilder filter = new BoolQueryBuilder();
filter.must(paramFilter);
filter.must(scoreFilter);
... ... @@ -143,21 +145,30 @@ public class ProductCountServiceImpl implements IProductCountService {
@Override
public BoolQueryBuilder genScoreFilter(BoolQueryBuilder paramFilter) {
BoolQueryBuilder scoreFilter = this.genScoreDefaultFilter();
if (this.queryCount(paramFilter, scoreFilter) <= 2000) {
long totalCount = this.queryCount(paramFilter, scoreFilter);
if (totalCount <= 2000) {
return scoreFilter;
}
// 零点那一刻命中不了,得想办法解决【比如凌晨两点再切数量】
long todayLastTimeSecond = DateUtil.getLastTimeSecond(new Date());
long oneMonthTs = 30 * 24 * 60 * 60 * 1000L;
long oneMonthInSecond = 30 * 24 * 60 * 60;
int tryCount = 1;
while (tryCount <= 3) {
scoreFilter = this.genScoreDefaultFilter();
scoreFilter.must(QueryBuilders.rangeQuery("firstShelveTime").from(todayLastTimeSecond - tryCount * oneMonthTs));
if (this.queryCount(paramFilter, scoreFilter) > 1000) {
scoreFilter.must(QueryBuilders.rangeQuery("firstShelveTime").from(todayLastTimeSecond - tryCount * oneMonthInSecond));
totalCount = this.queryCount(paramFilter, scoreFilter);
if(totalCount>=1000){
return scoreFilter;
}
tryCount++;
}
return scoreFilter;
}
public static void main(String[] args) {
long todayLastTimeSecond = DateUtil.getLastTimeSecond(new Date());
long oneMonthInSecond = 30 * 24 * 60 * 60;
System.out.println(todayLastTimeSecond - 1 * oneMonthInSecond);
}
}
... ...