Skip to content

Commit

Permalink
fix: 优化商品参数聚合搜索
Browse files Browse the repository at this point in the history
  • Loading branch information
LeiGaoRobot committed Dec 5, 2023
1 parent 71ed58e commit 183b2ba
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
48 changes: 48 additions & 0 deletions framework/src/main/java/cn/lili/elasticsearch/EmptyPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cn.lili.elasticsearch;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

public class EmptyPage implements Pageable {

@Override
public int getPageNumber() {
return 0;
}

@Override
public int getPageSize() {
return 0;
}

@Override
public long getOffset() {
return 0;
}

@Override
public Sort getSort() {
return null;
}

@Override
public Pageable next() {
return null;
}

@Override
public Pageable previousOrFirst() {
return null;
}

@Override
public Pageable first() {
return null;
}

@Override
public boolean hasPrevious() {
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import cn.lili.cache.CachePrefix;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.vo.PageVO;
import cn.lili.elasticsearch.EmptyPage;
import cn.lili.modules.goods.entity.enums.GoodsAuthEnum;
import cn.lili.modules.goods.entity.enums.GoodsStatusEnum;
import cn.lili.modules.search.entity.dos.EsGoodsIndex;
Expand Down Expand Up @@ -122,7 +123,7 @@ public EsGoodsRelatedInfo getSelector(EsGoodsSearchDTO goodsSearch, PageVO pageV
return null;
}

NativeSearchQueryBuilder builder = createSearchQueryBuilder(goodsSearch, pageVo);
NativeSearchQueryBuilder builder = createSearchQueryBuilder(goodsSearch, null);
//分类
AggregationBuilder categoryNameBuilder = AggregationBuilders.terms("categoryNameAgg").field("categoryNamePath.keyword");
builder.addAggregation(AggregationBuilders.terms("categoryAgg").field("categoryPath").subAggregation(categoryNameBuilder));
Expand All @@ -137,6 +138,7 @@ public EsGoodsRelatedInfo getSelector(EsGoodsSearchDTO goodsSearch, PageVO pageV
AggregationBuilder sortBuilder = AggregationBuilders.sum("sortAgg").field(ATTR_SORT);
AggregationBuilder paramsNameBuilder = AggregationBuilders.terms("nameAgg").field(ATTR_NAME).subAggregation(sortBuilder).order(BucketOrder.aggregation("sortAgg", false)).subAggregation(valuesBuilder);
builder.addAggregation(AggregationBuilders.nested("attrAgg", ATTR_PATH).subAggregation(paramsNameBuilder));
builder.withPageable(new EmptyPage());
NativeSearchQuery searchQuery = builder.build();
SearchHits<EsGoodsIndex> search = restTemplate.search(searchQuery, EsGoodsIndex.class);

Expand Down Expand Up @@ -615,17 +617,11 @@ private void keywordSearch(BoolQueryBuilder filterBuilder, String keyword) {
private List<FunctionScoreQueryBuilder.FilterFunctionBuilder> buildFunctionSearch() {
List<FunctionScoreQueryBuilder.FilterFunctionBuilder> filterFunctionBuilders = new ArrayList<>();

// GaussDecayFunctionBuilder skuNoScore = ScoreFunctionBuilders.gaussDecayFunction("skuSource", 100, 10).setWeight(2);
// FunctionScoreQueryBuilder.FilterFunctionBuilder skuNoBuilder = new FunctionScoreQueryBuilder.FilterFunctionBuilder(skuNoScore);
// filterFunctionBuilders.add(skuNoBuilder);
// 修改分数算法为无,数字最大分数越高
FieldValueFactorFunctionBuilder skuNoScore = ScoreFunctionBuilders.fieldValueFactorFunction("skuSource").modifier(FieldValueFactorFunction.Modifier.LOG1P).setWeight(3);
FunctionScoreQueryBuilder.FilterFunctionBuilder skuNoBuilder = new FunctionScoreQueryBuilder.FilterFunctionBuilder(skuNoScore);
filterFunctionBuilders.add(skuNoBuilder);

// 修改分数算法为无,数字最大分数越高
// FieldValueFactorFunctionBuilder buyCountScore = ScoreFunctionBuilders.fieldValueFactorFunction("buyCount").modifier(FieldValueFactorFunction.Modifier.NONE).setWeight(10);
// FunctionScoreQueryBuilder.FilterFunctionBuilder buyCountBuilder = new FunctionScoreQueryBuilder.FilterFunctionBuilder(buyCountScore);
// filterFunctionBuilders.add(buyCountBuilder);
return filterFunctionBuilders;
}

Expand Down

0 comments on commit 183b2ba

Please sign in to comment.