Authored by unknown

es适配到5.4.3

Showing 63 changed files with 302 additions and 254 deletions
package com.yoho.search.models;
import java.util.ArrayList;
import java.util.List;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder;
import com.yoho.search.service.scorer.IScorer;
public class YohoFilterFunctionBuilders {
public List<FilterFunctionBuilder> filterFunctionBuilders;
public YohoFilterFunctionBuilders(){
this.filterFunctionBuilders = new ArrayList<FilterFunctionBuilder>();
}
public YohoFilterFunctionBuilders(List<IScorer> scorers){
this.filterFunctionBuilders = new ArrayList<FilterFunctionBuilder>();
if(scorers!=null){
for (IScorer iScorer : scorers) {
iScorer.addScorer(this);
}
}
}
public YohoFilterFunctionBuilders(IScorer scorer){
this.filterFunctionBuilders = new ArrayList<FilterFunctionBuilder>();
if(scorer!=null){
scorer.addScorer(this);
}
}
public YohoFilterFunctionBuilders add(QueryBuilder filter, ScoreFunctionBuilder<?> scoreFunction){
this.filterFunctionBuilders.add(new FilterFunctionBuilder(filter,scoreFunction));
return this;
}
public YohoFilterFunctionBuilders add(ScoreFunctionBuilder<?> scoreFunction){
this.filterFunctionBuilders.add(new FilterFunctionBuilder(scoreFunction));
return this;
}
public FilterFunctionBuilder[] getFilterFunctionBuilders() {
return filterFunctionBuilders.toArray(new FilterFunctionBuilder[filterFunctionBuilders.size()]);
}
}
... ...
... ... @@ -24,7 +24,7 @@ public abstract class AbstractSingleFieldAggregation extends AbstractAggregation
}
@Override
public AbstractAggregationBuilder getBuilder() {
public AbstractAggregationBuilder<?> getBuilder() {
return AggregationBuilders.terms(aggName()).field(getField()).size(count);
}
... ...
... ... @@ -52,7 +52,7 @@ public class AgeLevelAggregation extends AbstractAggregation {
}
@Override
public AbstractAggregationBuilder getBuilder() {
public AbstractAggregationBuilder<?> getBuilder() {
return AggregationBuilders.terms(aggName()).field(ProductIndexEsField.ageLevel).size(100);
}
... ...
... ... @@ -49,7 +49,7 @@ public class BrandAggregation extends AbstractAggregation {
}
@Override
public AbstractAggregationBuilder getBuilder() {
public AbstractAggregationBuilder<?> getBuilder() {
return AggregationBuilders.terms(aggName()).field(ProductIndexEsField.brandId).size(brandCount);
}
... ...
... ... @@ -37,7 +37,7 @@ public class ColorAggregation extends AbstractAggregation {
}
@Override
public AbstractAggregationBuilder getBuilder() {
public AbstractAggregationBuilder<?> getBuilder() {
return AggregationBuilders.terms(aggName()).field(ProductIndexEsField.colorIds).size(100);
}
... ...
... ... @@ -36,7 +36,7 @@ public class DiscountAggregation extends AbstractAggregation {
}
@Override
public AbstractAggregationBuilder getBuilder() {
public AbstractAggregationBuilder<?> getBuilder() {
return AggregationBuilders.range(aggName()).field(ProductIndexEsField.promotionDiscount).addRange(0.0, 0.399).addRange(0.4, 0.699).addRange(0.7, 0.999);
}
... ...
... ... @@ -33,7 +33,7 @@ public class GenderAggregation extends AbstractAggregation {
}
@Override
public AbstractAggregationBuilder getBuilder() {
public AbstractAggregationBuilder<?> getBuilder() {
return AggregationBuilders.terms(aggName()).field(ProductIndexEsField.gender).size(5);
}
... ...
... ... @@ -26,7 +26,7 @@ public class GenderNewAggregation extends AbstractAggregation {
}
@Override
public AbstractAggregationBuilder getBuilder() {
public AbstractAggregationBuilder<?> getBuilder() {
return AggregationBuilders.terms(aggName()).field(ProductIndexEsField.gender).size(5);
}
... ...
... ... @@ -52,7 +52,7 @@ public class GroupBrandAggregation extends AbstractAggregation {
}
@Override
public AbstractAggregationBuilder getBuilder() {
public AbstractAggregationBuilder<?> getBuilder() {
return AggCommonHelper.getTopHitAggregation(Arrays.asList(simpleFieldAgg), this.topHitOrder, this.topHitCount);
}
... ...
... ... @@ -51,7 +51,7 @@ public class GroupShopAggregation extends AbstractAggregation {
}
@Override
public AbstractAggregationBuilder getBuilder() {
public AbstractAggregationBuilder<?> getBuilder() {
return AggCommonHelper.getTopHitAggregation(Arrays.asList(simpleFieldAgg), this.topHitOrder, this.topHitCount);
}
... ...
... ... @@ -25,7 +25,7 @@ public class IsGlobalAggregation extends AbstractAggregation {
}
@Override
public AbstractAggregationBuilder getBuilder() {
public AbstractAggregationBuilder<?> getBuilder() {
return AggregationBuilders.terms(aggName()).field(ProductIndexEsField.isGlobal).size(5);
}
... ...
... ... @@ -25,7 +25,7 @@ public class IsLimitedAggregation extends AbstractAggregation {
}
@Override
public AbstractAggregationBuilder getBuilder() {
public AbstractAggregationBuilder<?> getBuilder() {
return AggregationBuilders.terms(aggName()).field(ProductIndexEsField.islimited).size(2);
}
... ...
... ... @@ -25,7 +25,7 @@ public class IsNewAggregation extends AbstractAggregation {
}
@Override
public AbstractAggregationBuilder getBuilder() {
public AbstractAggregationBuilder<?> getBuilder() {
return AggregationBuilders.terms(aggName()).field(ProductIndexEsField.isnew).size(2);
}
... ...
... ... @@ -25,7 +25,7 @@ public class IsSecialofferAggregation extends AbstractAggregation {
}
@Override
public AbstractAggregationBuilder getBuilder() {
public AbstractAggregationBuilder<?> getBuilder() {
return AggregationBuilders.terms(aggName()).field(ProductIndexEsField.specialoffer).size(2);
}
... ...
... ... @@ -38,7 +38,7 @@ public class PriceAggregation extends AbstractAggregation {
}
@Override
public AbstractAggregationBuilder getBuilder() {
public AbstractAggregationBuilder<?> getBuilder() {
return AggregationBuilders.terms(aggName()).field(ProductIndexEsField.salesPrice).size(10000).order(Terms.Order.term(true));
}
... ...
... ... @@ -37,7 +37,7 @@ public class RecentShelveDayAggregation extends AbstractAggregation {
}
@Override
public AbstractAggregationBuilder getBuilder() {
public AbstractAggregationBuilder<?> getBuilder() {
return AggregationBuilders.terms(aggName()).field(ProductIndexEsField.shelveDay).size(7).order(Terms.Order.term(false));
}
... ...
... ... @@ -44,7 +44,7 @@ public class RecommendBrandAggregation extends AbstractAggregation {
}
@Override
public AbstractAggregationBuilder getBuilder() {
public AbstractAggregationBuilder<?> getBuilder() {
return AggCommonHelper.getTopHitAggregation(simpleFieldAggs, topHitOrder, 1);
}
... ...
... ... @@ -52,7 +52,7 @@ public class RecommendShopAggregation extends AbstractAggregation {
}
@Override
public AbstractAggregationBuilder getBuilder() {
public AbstractAggregationBuilder<?> getBuilder() {
return AggCommonHelper.getTopHitAggregation(simpleFieldAggs, topHitOrder, topHitCount);
}
... ...
... ... @@ -34,7 +34,7 @@ public class SizeAggregation extends AbstractAggregation {
}
@Override
public AbstractAggregationBuilder getBuilder() {
public AbstractAggregationBuilder<?> getBuilder() {
return AggregationBuilders.terms(aggName()).field(ProductIndexEsField.sizeIds).size(300);
}
... ...
... ... @@ -14,7 +14,7 @@ import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket;
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import com.alibaba.fastjson.JSONArray;
import com.yoho.search.base.utils.ProductIndexEsField;
... ... @@ -38,13 +38,13 @@ public class SortGroupAggregation extends AbstractAggregation {
public String filterName() {
return "group_sort";
}
@Override
public AbstractAggregationBuilder getBuilder() {
public AbstractAggregationBuilder<?> getBuilder() {
// 构造聚合条件(如果需要小分类,则加上小分类的聚合)
TermsBuilder maxAggBuilder = AggregationBuilders.terms("maxAgg").field(ProductIndexEsField.maxSortName).size(100);
TermsBuilder middleAggBuilder = AggregationBuilders.terms("middleAgg").field(ProductIndexEsField.middleSortName).size(100);
TermsBuilder smallAggBuilder = AggregationBuilders.terms("smallAgg").field(ProductIndexEsField.smallSortName).size(100);
TermsAggregationBuilder maxAggBuilder = AggregationBuilders.terms("maxAgg").field(ProductIndexEsField.maxSortName).size(100);
TermsAggregationBuilder middleAggBuilder = AggregationBuilders.terms("middleAgg").field(ProductIndexEsField.middleSortName).size(100);
TermsAggregationBuilder smallAggBuilder = AggregationBuilders.terms("smallAgg").field(ProductIndexEsField.smallSortName).size(100);
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_NEEDSMALLSORT) && "1".equals(paramMap.get(SearchRequestParams.PARAM_SEARCH_NEEDSMALLSORT))) {
maxAggBuilder.subAggregation(middleAggBuilder.subAggregation(smallAggBuilder));
} else {
... ...
... ... @@ -42,7 +42,7 @@ public class StandardAggregation extends AbstractAggregation {
}
@Override
public AbstractAggregationBuilder getBuilder() {
public AbstractAggregationBuilder<?> getBuilder() {
return AggregationBuilders.terms(aggName()).field(ProductIndexEsField.standardNames).size(5000);
}
... ...
... ... @@ -41,7 +41,7 @@ public class StyleAggregation extends AbstractAggregation {
}
@Override
public AbstractAggregationBuilder getBuilder() {
public AbstractAggregationBuilder<?> getBuilder() {
return AggregationBuilders.terms(aggName()).field(ProductIndexEsField.styleIds).size(100);
}
... ...
... ... @@ -15,7 +15,7 @@ import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket;
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
import org.elasticsearch.search.sort.SortBuilders;
import org.slf4j.Logger;
... ... @@ -53,19 +53,18 @@ public class AggCommonHelper {
* @param topHitFieldCount
* @return
*/
public static AbstractAggregationBuilder getTopHitAggregation(List<SimpleFieldAgg> simpleFieldAggs, String topHitOrder, int topHitCount) {
public static AbstractAggregationBuilder<?> getTopHitAggregation(List<SimpleFieldAgg> simpleFieldAggs, String topHitOrder, int topHitCount) {
int length = simpleFieldAggs.size();
FieldSortOrder fieldSortOrder = new FieldSortOrder(topHitOrder);
AbstractAggregationBuilder result = null;
AbstractAggregationBuilder<?> result = null;
for (int i = length - 1; i >= 0; i--) {
SimpleFieldAgg simpleFieldAgg = simpleFieldAggs.get(i);
TermsBuilder temp = AggregationBuilders.terms(simpleFieldAgg.getAggName()).field(simpleFieldAgg.getEsField()).size(simpleFieldAgg.getBucketCount());
TermsAggregationBuilder temp = AggregationBuilders.terms(simpleFieldAgg.getAggName()).field(simpleFieldAgg.getEsField()).size(simpleFieldAgg.getBucketCount());
// 如果是最小层面的聚合,则加上tophit的聚合
if (result == null) {
if (topHitCount > 0) {
temp.subAggregation(AggregationBuilders.max("sort").field(fieldSortOrder.getSortField()));
temp.subAggregation(AggregationBuilders.topHits("topHit").addSort(SortBuilders.fieldSort(fieldSortOrder.getSortField()).order(fieldSortOrder.getSortOrder()))
.setSize(topHitCount));
temp.subAggregation(AggregationBuilders.topHits("topHit").sort(SortBuilders.fieldSort(fieldSortOrder.getSortField()).order(fieldSortOrder.getSortOrder())).size(topHitCount));
}
result = temp;
} else {
... ...
... ... @@ -22,6 +22,7 @@ import org.springframework.util.Assert;
import com.yoho.search.base.utils.SearchPageIdDefine;
import com.yoho.search.core.personalized.PersonalizedSearch;
import com.yoho.search.models.FirstShelveTimeScore;
import com.yoho.search.models.YohoFilterFunctionBuilders;
import com.yoho.search.service.base.SearchDynamicConfigService;
import com.yoho.search.service.base.SearchRequestParams;
import com.yoho.search.service.scorer.IScorer;
... ... @@ -61,23 +62,19 @@ public class FunctionScoreSearchHelper {
* @return
*/
public QueryBuilder buildFunctionScoreQueryBuild(QueryBuilder queryBuilder, BoolQueryBuilder boolQueryBuilder, Map<String, String> paramMap) {
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder);
// 1、构造persionalFilter
BoolQueryBuilder persionalFilter = this.genPersionalFilter(queryBuilder, boolQueryBuilder);
// 2、获取打分器
String pageId = MapUtils.getString(paramMap, SearchRequestParams.PARAM_SEARCH_PAGEID, "0");
List<IScorer> scores = this.getScorers(pageId, persionalFilter, paramMap);
if (scores == null || scores.isEmpty()) {
return functionScoreQueryBuilder;
}
// 3、添加打分器
for (IScorer iScorer : scores) {
if (iScorer != null) {
iScorer.addScorer(functionScoreQueryBuilder);
}
List<IScorer> scorers = this.getScorers(pageId, persionalFilter, paramMap);
if (scorers == null || scorers.isEmpty()) {
return new FunctionScoreQueryBuilder(queryBuilder);
}
// 3、构造functionScoreQueryBuilder
YohoFilterFunctionBuilders yohoFilterFunctionBuilders = new YohoFilterFunctionBuilders(scorers);
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder, yohoFilterFunctionBuilders.getFilterFunctionBuilders());
// 4、设置打分模式
functionScoreQueryBuilder.boostMode(CombineFunction.MULT);
functionScoreQueryBuilder.boostMode(CombineFunction.MULTIPLY);
return functionScoreQueryBuilder;
}
... ... @@ -234,9 +231,10 @@ public class FunctionScoreSearchHelper {
scorers.add(this.getPersonalVectorFeatureScorer(persionalFilter, paramMap));
return scorers;
}
/**
* 个性化的打分器
*
* @param persionalFilter
* @param paramMap
* @return
... ... @@ -255,7 +253,7 @@ public class FunctionScoreSearchHelper {
BoolQueryBuilder scoreFilter = productCountService.genScoreFilter(persionalFilter);
return searchScorerFactory.getFeatureFactorScorer(scoreFilter, personalizedSearch.getUserVectorFeature(), personalizedSearch.getVectorFeatureVersion());
}
/**
* 直接使用商品特征的向量用来做个性化打分
*
... ... @@ -264,21 +262,21 @@ public class FunctionScoreSearchHelper {
* @return
*/
public QueryBuilder buildFunctionScoreQueryBuildWithProductFeature(QueryBuilder queryBuilder, String productVectorFeature) {
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder);
// 1. 获取商品特征向量
if (StringUtils.isBlank(productVectorFeature)) {
return functionScoreQueryBuilder;
return queryBuilder;
}
// 2. 传入参数调用脚本,以商品特征代替用户特征
String[] productVectorFeatures = productVectorFeature.split("\\|", 2);
if (productVectorFeatures.length != 2) {
return functionScoreQueryBuilder;
return queryBuilder;
}
IScorer scorer = searchScorerFactory.getFeatureFactorScorer(queryBuilder, productVectorFeatures[1], productVectorFeatures[0]);
scorer.addScorer(functionScoreQueryBuilder);
IScorer scorer = searchScorerFactory.getFeatureFactorScorer(queryBuilder, productVectorFeatures[1], productVectorFeatures[0]);
YohoFilterFunctionBuilders yohoFilterFunctionBuilders = new YohoFilterFunctionBuilders(scorer);
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder,yohoFilterFunctionBuilders.getFilterFunctionBuilders());
return functionScoreQueryBuilder;
}
// 新品到着页的根据首次上架时间衰减函数规则值
private FirstShelveTimeScore getNewArrivalPageShelveTimeScore() {
try {
... ...
... ... @@ -7,9 +7,10 @@ import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
... ... @@ -66,7 +67,7 @@ public class SearchServiceHelper {
if (searchCommonHelper.isQuerySknOrSku(query)) {
queryBuilder.field(ProductIndexEsField.productSkn_productSkn_ansj);
queryBuilder.field(ProductIndexEsField.skuIds);
queryBuilder.operator(MatchQueryBuilder.Operator.OR);
queryBuilder.operator(Operator.OR);
queryBuilder.minimumShouldMatch("1");
return queryBuilder;
}
... ... @@ -74,10 +75,10 @@ public class SearchServiceHelper {
// 4.设置查询字段和比重【AND表示多字段都要匹配,可提高精确度】
this.setDefaultSearchField(queryBuilder);
if ("or".equalsIgnoreCase(ISearchConstants.SEARCH_OPERATOR)) {
queryBuilder.operator(MatchQueryBuilder.Operator.OR);
queryBuilder.operator(Operator.OR);
queryBuilder.minimumShouldMatch(ISearchConstants.SEARCH_MINIMUM_SHOULD_MATCH);
} else {
queryBuilder.operator(MatchQueryBuilder.Operator.AND);
queryBuilder.operator(Operator.AND);
}
// 5.如果不是skn,则将搜索关键词存进redis
... ... @@ -448,7 +449,7 @@ public class SearchServiceHelper {
// 活动模板相关的过滤条件
BoolQueryBuilder activitiesTermsBuilder = searchCommonHelper.getActivitiesTermsBuilder(paramMap);
if (activitiesTermsBuilder != null) {
boolFilter.must(QueryBuilders.nestedQuery(ProductIndexEsField.activities, activitiesTermsBuilder));
boolFilter.must(QueryBuilders.nestedQuery(ProductIndexEsField.activities, activitiesTermsBuilder,ScoreMode.None));
}
// 如果contain_seckill!=Y,则过滤掉秒杀商品
... ...
... ... @@ -134,7 +134,7 @@ public class SearchSortHelper {
* @param fieldName
* @param sortOrder
*/
public void addSortBuildSorts(List<SortBuilder> sortBuilders, List<String> filteredFieldNames, String fieldName, SortOrder sortOrder) {
public void addSortBuildSorts(List<SortBuilder<?>> sortBuilders, List<String> filteredFieldNames, String fieldName, SortOrder sortOrder) {
if (filteredFieldNames.contains(fieldName)) {
return;
}
... ... @@ -173,7 +173,7 @@ public class SearchSortHelper {
}
// 都要添加次要排序条件
private void addDeafultSortBuildSorts(List<SortBuilder> sortBuilders, List<String> filteredFieldNames) {
private void addDeafultSortBuildSorts(List<SortBuilder<?>> sortBuilders, List<String> filteredFieldNames) {
this.addSortBuildSorts(sortBuilders, filteredFieldNames, "salesNum", SortOrder.DESC);
this.addSortBuildSorts(sortBuilders, filteredFieldNames, "firstShelveTime", SortOrder.DESC);
this.addSortBuildSorts(sortBuilders, filteredFieldNames, "id", SortOrder.DESC);
... ... @@ -185,8 +185,8 @@ public class SearchSortHelper {
* @param paramMap
* @return
*/
public List<SortBuilder> buildSortList(Map<String, String> paramMap) {
List<SortBuilder> sortBuilders = new ArrayList<SortBuilder>();
public List<SortBuilder<?>> buildSortList(Map<String, String> paramMap) {
List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
List<String> filteredFieldNames = new ArrayList<String>();
// 1、判断是否需要分数优先
... ...
... ... @@ -63,8 +63,8 @@ public class TblProductServiceHepler {
return boolFilterBuilder;
}
public List<AbstractAggregationBuilder> buildAggregations(Map<String, String> paramMap) {
List<AbstractAggregationBuilder> list = new ArrayList<AbstractAggregationBuilder>();
public List<AbstractAggregationBuilder<?>> buildAggregations(Map<String, String> paramMap) {
List<AbstractAggregationBuilder<?>> list = new ArrayList<AbstractAggregationBuilder<?>>();
String key;
String field;
for (Map.Entry<String, String> entry : paramMap.entrySet()) {
... ...
... ... @@ -20,7 +20,7 @@ import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket;
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -135,9 +135,9 @@ public class BreakSizeSceneService extends AbstractSceneService {
SearchParam searchParam = searchParamHelper.buildDefault(paramMap);
// 按品类聚合
List<AbstractAggregationBuilder> list = new ArrayList<AbstractAggregationBuilder>();
TermsBuilder tremsBuilder = AggregationBuilders.terms("sortIdAgg").field("smallSortId").size(300);// 小分类200多一点
tremsBuilder.subAggregation(AggregationBuilders.topHits("productSkn").setSize(1));// 只取一个smallSortId对用的hits数据【其实只是为了取middleSortId,maxSortId】
List<AbstractAggregationBuilder<?>> list = new ArrayList<AbstractAggregationBuilder<?>>();
TermsAggregationBuilder tremsBuilder = AggregationBuilders.terms("sortIdAgg").field("smallSortId").size(300);// 小分类200多一点
tremsBuilder.subAggregation(AggregationBuilders.topHits("productSkn").size(1));// 只取一个smallSortId对用的hits数据【其实只是为了取middleSortId,maxSortId】
tremsBuilder.subAggregation(AggregationBuilders.terms("sizeIdAgg").field("sizeIds").size(100));// 尺码不多,100足够
list.add(tremsBuilder);
searchParam.setAggregationBuilders(list);
... ...
... ... @@ -23,7 +23,7 @@ import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket;
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
... ... @@ -42,6 +42,7 @@ import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.models.GoodProductBO;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.models.YohoFilterFunctionBuilders;
import com.yoho.search.service.base.SearchCacheService;
import com.yoho.search.service.base.SearchCommonService;
import com.yoho.search.service.base.index.ProductIndexBaseService;
... ... @@ -77,12 +78,9 @@ public class GoodProductSceneService {
productListSearchCache = searchCacheFactory.getProductListSearchCache();
}
private static final String rec_product_skn = "rec_product_skn";//推荐的skn
private static final String view_product_skn = "view_product_skn";
private static final String cart_product_skn = "cart_product_skn";
private static final String collect_product_skn = "collect_product_skn";
public SearchApiResult goodProductList(Map<String, String> paramMap) throws Exception {
// 1、检测分页参数
... ... @@ -91,12 +89,12 @@ public class GoodProductSceneService {
if (page < 1 || pageSize < 0) {
return new SearchApiResult().setCode(400).setMessage("分页参数不合法");
}
// 1.1 SKN参数获取
List<String> view_product_skns = stringToList(paramMap.getOrDefault(view_product_skn, ""), ",");
List<String> cart_productskns = stringToList(paramMap.getOrDefault(cart_product_skn, ""), ",");
List<String> collect_product_skns = stringToList(paramMap.getOrDefault(collect_product_skn, ""), ",");
// 2、先获取用户浏览的SKN对应的品类列表
List<String> allProductSkn = new ArrayList<String>();
allProductSkn.addAll(view_product_skns);
... ... @@ -120,7 +118,7 @@ public class GoodProductSceneService {
excludeFields.remove(ProductIndexEsField.phrase);
searchParam.setExcludeFields(excludeFields);
List<SortBuilder> sortBuilders = new ArrayList<SortBuilder>();
List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
sortBuilders.add(SortBuilders.fieldSort("_score").order(SortOrder.DESC));
sortBuilders.add(SortBuilders.fieldSort(ProductIndexEsField.heatValue).order(SortOrder.DESC));
sortBuilders.add(SortBuilders.fieldSort(ProductIndexEsField.id).order(SortOrder.DESC));
... ... @@ -163,9 +161,9 @@ public class GoodProductSceneService {
boolFilter.must(QueryBuilders.termsQuery("productSkn", productSkns));
searchParam.setFiter(boolFilter);
// 2、设置聚合条件,获取所有的品类和品牌
TermsBuilder smallSortIdAgg = AggregationBuilders.terms("smallSortAgg").field("smallSortId").size(100);
TermsBuilder brandIdAgg = AggregationBuilders.terms("brandAgg").field("brandId").size(100);
List<AbstractAggregationBuilder> aggregationBuilders = new ArrayList<AbstractAggregationBuilder>();
TermsAggregationBuilder smallSortIdAgg = AggregationBuilders.terms("smallSortAgg").field("smallSortId").size(100);
TermsAggregationBuilder brandIdAgg = AggregationBuilders.terms("brandAgg").field("brandId").size(100);
List<AbstractAggregationBuilder<?>> aggregationBuilders = new ArrayList<AbstractAggregationBuilder<?>>();
aggregationBuilders.add(smallSortIdAgg);
aggregationBuilders.add(brandIdAgg);
searchParam.setAggregationBuilders(aggregationBuilders);
... ... @@ -251,19 +249,19 @@ public class GoodProductSceneService {
// 强制加上个性化打分
functionScoreSearchHelper.buildFunctionScoreQueryBuild(functionScoreQueryBuilder, boolFilter, paramMap);
searchParam.setQuery(functionScoreQueryBuilder);
// 4、设置聚合条件
String sortField = "_score";
SortOrder sortOrder = SortOrder.DESC;
List<AbstractAggregationBuilder> list = new ArrayList<AbstractAggregationBuilder>();
List<AbstractAggregationBuilder<?>> list = new ArrayList<AbstractAggregationBuilder<?>>();
// 4.1)构造父聚合:品类聚合【同时按子聚合的sort字段排序】
TermsBuilder parentAggregationBuilder = AggregationBuilders.terms("smallSortAgg").field("smallSortId").size(smallSortIds.size());
TermsAggregationBuilder parentAggregationBuilder = AggregationBuilders.terms("smallSortAgg").field("smallSortId").size(smallSortIds.size());
// 4.2)构造子聚合:品牌或聚合【同时按子聚合的sort字段排序】
TermsBuilder sonAggregationBuilder = AggregationBuilders.terms("brandAgg").field("brandId").size(brandIds.size());
TermsAggregationBuilder sonAggregationBuilder = AggregationBuilders.terms("brandAgg").field("brandId").size(brandIds.size());
// 4.3)为子聚合添加孙聚合:取得分最大的值
sonAggregationBuilder.subAggregation(AggregationBuilders.max("sort").field(sortField));
// 4.4)为子聚合孙聚合:取打分最高的一个product
sonAggregationBuilder.subAggregation(AggregationBuilders.topHits("product").addSort(SortBuilders.fieldSort(sortField).order(sortOrder)).setSize(1));
sonAggregationBuilder.subAggregation(AggregationBuilders.topHits("product").sort(SortBuilders.fieldSort(sortField).order(sortOrder)).size(1));
list.add(parentAggregationBuilder.subAggregation(sonAggregationBuilder));
searchParam.setAggregationBuilders(list);
... ... @@ -361,22 +359,20 @@ public class GoodProductSceneService {
private QueryBuilder builderGoodProductQueryBuilder(Map<String, String> paramMap, BoolQueryBuilder boolFilter, List<String> recommendedSknList) {
QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder);
// 针对推荐出来的SKN做加分
if (recommendedSknList != null && !recommendedSknList.isEmpty()) {
// 未推荐出商品则直接走个性化
if (recommendedSknList == null || recommendedSknList.isEmpty()) {
return functionScoreSearchHelper.buildFunctionScoreQueryBuild(queryBuilder, boolFilter, paramMap);
} else {
YohoFilterFunctionBuilders yohoFilterFunctionBuilders = new YohoFilterFunctionBuilders();
Map<Integer, List<String>> recommondSknMap = this.splitProductSkns(recommendedSknList, 2);
float currentGroupScore = 1000;
for (Map.Entry<Integer, List<String>> entry : recommondSknMap.entrySet()) {
functionScoreQueryBuilder.add(QueryBuilders.termsQuery("productSkn", entry.getValue()), ScoreFunctionBuilders.weightFactorFunction(currentGroupScore));
yohoFilterFunctionBuilders.add(QueryBuilders.termsQuery("productSkn", entry.getValue()), ScoreFunctionBuilders.weightFactorFunction(currentGroupScore));
currentGroupScore = currentGroupScore - 10;
}
} else {
// 否则加上个性化打分
if (searchCommonHelper.isNeedPersonalSearch(paramMap)) {
functionScoreSearchHelper.buildFunctionScoreQueryBuild(functionScoreQueryBuilder, boolFilter, paramMap);
}
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder, yohoFilterFunctionBuilders.getFilterFunctionBuilders());
return functionScoreQueryBuilder;
}
return functionScoreQueryBuilder;
}
/**
... ...
... ... @@ -120,7 +120,7 @@ public class HotSaleRankSceneService extends AbstractCacheAbleService {
searchParam.setOffset((page - 1) * pageSize);
searchParam.setSize(pageSize);
// 3)设置排序字段
List<SortBuilder> sortBuilderS = new ArrayList<SortBuilder>();
List<SortBuilder<?>> sortBuilderS = new ArrayList<SortBuilder<?>>();
sortBuilderS.add(SortBuilders.fieldSort(ProductIndexEsField.sevendayMoney).order(SortOrder.DESC));
sortBuilderS.add(SortBuilders.fieldSort(ProductIndexEsField.salesNum).order(SortOrder.DESC));
sortBuilderS.add(SortBuilders.fieldSort(ProductIndexEsField.id).order(SortOrder.DESC));
... ...
... ... @@ -34,6 +34,7 @@ import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.core.personalized.PersonalizedSearch;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.models.YohoFilterFunctionBuilders;
import com.yoho.search.service.base.ProductListSortService;
import com.yoho.search.service.base.SearchCacheService;
import com.yoho.search.service.base.SearchCommonService;
... ... @@ -111,7 +112,7 @@ public class NewGoodProductSceneService {
searchParam.setExcludeFields(excludeFields);
// 4、构造排序条件
List<SortBuilder> sortBuilders = new ArrayList<SortBuilder>();
List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
sortBuilders.add(SortBuilders.fieldSort("_score").order(SortOrder.DESC));
sortBuilders.add(SortBuilders.fieldSort(ProductIndexEsField.heatValue).order(SortOrder.DESC));
sortBuilders.add(SortBuilders.fieldSort(ProductIndexEsField.id).order(SortOrder.DESC));
... ... @@ -166,9 +167,6 @@ public class NewGoodProductSceneService {
}
private QueryBuilder builderGoodProductQueryBuilder(Map<String, String> paramMap, List<String> recProductSkns) {
QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder);
List<IScorer> scorers = new ArrayList<IScorer>();
// 1、添加recProductSkns
scorers.add(new FirstProductSknScorer(recProductSkns));
... ... @@ -184,17 +182,19 @@ public class NewGoodProductSceneService {
persionalFilter.must(QueryBuilders.rangeQuery("firstShelveTime").from(todayLastTimeSecond - 3 * oneMonthInSecond));
scorers.add(this.getPersonalVectorFeatureScorer(persionalFilter, paramMap));
// 4、构建
YohoFilterFunctionBuilders yohoFilterFunctionBuilders = new YohoFilterFunctionBuilders();
for (IScorer iScorer : scorers) {
if (iScorer != null) {
iScorer.addScorer(functionScoreQueryBuilder);
iScorer.addScorer(yohoFilterFunctionBuilders);
}
}
// 4、主推 【上衣、裙装、裤装、鞋靴 】
functionScoreQueryBuilder.add(QueryBuilders.termsQuery(ProductIndexEsField.maxSortId, Arrays.asList("1", "3", "4", "6")), ScoreFunctionBuilders.weightFactorFunction(10));
// 5、主推 【上衣、裙装、裤装、鞋靴 】
yohoFilterFunctionBuilders.add(QueryBuilders.termsQuery(ProductIndexEsField.maxSortId, Arrays.asList("1", "3", "4", "6")), ScoreFunctionBuilders.weightFactorFunction(10));
// 5、设置打分模式
functionScoreQueryBuilder.boostMode(CombineFunction.MULT);
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(QueryBuilders.matchAllQuery(),yohoFilterFunctionBuilders.getFilterFunctionBuilders());
functionScoreQueryBuilder.boostMode(CombineFunction.MULTIPLY);
return functionScoreQueryBuilder;
}
... ...
... ... @@ -264,7 +264,7 @@ public class PromotionSceneService {
// 2、构造带filter和Query的SearchParam
SearchParam searchParam = searchParamHelper.buildWithMustFilter(paramMap, mustFilterByPromotion);
// 3、构造聚合条件【7种】
List<AbstractAggregationBuilder> aggregationBuilders = new ArrayList<AbstractAggregationBuilder>();
List<AbstractAggregationBuilder<?>> aggregationBuilders = new ArrayList<AbstractAggregationBuilder<?>>();
searchParam.setAggregationBuilders(aggregationBuilders);
// 3.1获取通用的聚合场景
aggregationBuilders.addAll(selectionsWithOutAdvanceService.getAllAggregationBuilders(paramMap));
... ...
... ... @@ -147,8 +147,8 @@ public class SceneSelectionsService extends AbstractCacheAbleService {
return aggregations;
}
private List<AbstractAggregationBuilder> getAggregationBuilders(List<IAggregation> aggregations) {
List<AbstractAggregationBuilder> builders = new ArrayList<>();
private List<AbstractAggregationBuilder<?>> getAggregationBuilders(List<IAggregation> aggregations) {
List<AbstractAggregationBuilder<?>> builders = new ArrayList<>();
for (IAggregation aggregation : aggregations) {
builders.add(aggregation.getBuilder());
}
... ...
... ... @@ -113,7 +113,7 @@ public class SearchLikeInShopService extends AbstractCacheAbleService {
searchParam.setQuery(this.builderQueryBuilder(productInfoInEs, "20%"));
searchParam.setFiter(this.builderFilterBuilder(productInfoInEs, productSkns));
// 3、设置排序规则[按打分排序]
List<SortBuilder> sortBuilders = new ArrayList<SortBuilder>();
List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
sortBuilders.add(SortBuilders.fieldSort("_score").order(SortOrder.DESC));
searchParam.setSortBuilders(sortBuilders);
// 4、设置分页参数
... ...
... ... @@ -121,7 +121,7 @@ public class SearchLikeNotInShopService extends AbstractCacheAbleService {
searchParam.setQuery(this.builderQueryBuilder(productInfoInEs, inSameSort ? "30%" : "20%"));
searchParam.setFiter(this.builderFilterBuilder(productInfoInEs, productSkns, inSameSort));
// 3、设置排序规则[按打分排序]
List<SortBuilder> sortBuilders = new ArrayList<SortBuilder>();
List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
sortBuilders.add(SortBuilders.fieldSort("_score").order(SortOrder.DESC));
searchParam.setSortBuilders(sortBuilders);
// 4、设置分页参数
... ...
... ... @@ -129,7 +129,7 @@ public class SearchLikeSceneService extends AbstractCacheAbleService {
searchParam.setFiter(booleanQueryBuilder);
// 3、设置排序规则[按打分排序]
List<SortBuilder> sortBuilders = new ArrayList<SortBuilder>();
List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
sortBuilders.add(SortBuilders.fieldSort("_score").order(SortOrder.DESC));
searchParam.setSortBuilders(sortBuilders);
// 4、设置分页参数
... ...
package com.yoho.search.service.scorer;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import com.yoho.search.models.YohoFilterFunctionBuilders;
public interface IScorer {
public void addScorer(FunctionScoreQueryBuilder functionScoreQueryBuilder);
public void addScorer(YohoFilterFunctionBuilders yohoFilterFunctionBuilders);
}
... ...
package com.yoho.search.service.scorer.impl;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.models.YohoFilterFunctionBuilders;
import com.yoho.search.service.scorer.IScorer;
public class AddScoreSknScorer implements IScorer{
... ... @@ -12,8 +12,8 @@ public class AddScoreSknScorer implements IScorer{
private static final float SKN_INCREASE_SCORE_WEIGHT = 1.1f;
@Override
public void addScorer(FunctionScoreQueryBuilder functionScoreQueryBuilder) {
functionScoreQueryBuilder.add(QueryBuilders.termQuery(ProductIndexEsField.toAddScore, "Y"), ScoreFunctionBuilders.weightFactorFunction(SKN_INCREASE_SCORE_WEIGHT));
public void addScorer(YohoFilterFunctionBuilders yohoFilterFunctionBuilders) {
yohoFilterFunctionBuilders.add(QueryBuilders.termQuery(ProductIndexEsField.toAddScore, "Y"), ScoreFunctionBuilders.weightFactorFunction(SKN_INCREASE_SCORE_WEIGHT));
}
}
... ...
... ... @@ -4,10 +4,10 @@ import java.util.List;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.models.YohoFilterFunctionBuilders;
import com.yoho.search.service.scorer.IScorer;
public class BreakSizeProductScorer implements IScorer {
... ... @@ -21,13 +21,13 @@ public class BreakSizeProductScorer implements IScorer {
}
@Override
public void addScorer(FunctionScoreQueryBuilder functionScoreQueryBuilder) {
public void addScorer(YohoFilterFunctionBuilders yohoFilterFunctionBuilders) {
BoolQueryBuilder breakSizeProduct = QueryBuilders.boolQuery();
breakSizeProduct.must(QueryBuilders.rangeQuery(ProductIndexEsField.breakSizePercent).gt(50));
if (userFavoriteSizeIds!=null && !userFavoriteSizeIds.isEmpty()) {
breakSizeProduct.mustNot(QueryBuilders.termsQuery(ProductIndexEsField.sizeIds, userFavoriteSizeIds));
}
functionScoreQueryBuilder.add(breakSizeProduct, ScoreFunctionBuilders.weightFactorFunction(BREAK_PRODUCT_WEIGHT));
yohoFilterFunctionBuilders.add(breakSizeProduct, ScoreFunctionBuilders.weightFactorFunction(BREAK_PRODUCT_WEIGHT));
}
}
... ...
... ... @@ -4,11 +4,11 @@ import java.util.HashMap;
import java.util.Map;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.ScriptType;
import com.yoho.search.models.YohoFilterFunctionBuilders;
import com.yoho.search.service.scorer.IScorer;
/**
... ... @@ -32,18 +32,18 @@ public class FeatureFactorScorer implements IScorer {
}
@Override
public void addScorer(FunctionScoreQueryBuilder functionScoreQueryBuilder) {
public void addScorer(YohoFilterFunctionBuilders yohoFilterFunctionBuilders) {
Map<String, Object> scriptParams = new HashMap<>();
scriptParams.put("field", "productFeatureFactor");
scriptParams.put("userFeatureFactors", featureFactors);
scriptParams.put("vectorFeatureVersion", featureVersion);
scriptParams.put("baseConstant", BASE_CONSTANT);
scriptParams.put("factorConstant", FACTOR_CONSTANT);
Script script = new Script("feature_factor_vector_score", ScriptService.ScriptType.INLINE, "native", scriptParams);
Script script = new Script(ScriptType.INLINE, "native", "feature_factor_vector_score", scriptParams);
if (scoreFilter != null) {
functionScoreQueryBuilder.add(scoreFilter, ScoreFunctionBuilders.scriptFunction(script));
yohoFilterFunctionBuilders.add(scoreFilter, ScoreFunctionBuilders.scriptFunction(script));
} else {
functionScoreQueryBuilder.add(ScoreFunctionBuilders.scriptFunction(script));
yohoFilterFunctionBuilders.add(ScoreFunctionBuilders.scriptFunction(script));
}
}
... ...
... ... @@ -3,10 +3,10 @@ package com.yoho.search.service.scorer.impl;
import java.util.List;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.models.YohoFilterFunctionBuilders;
import com.yoho.search.service.scorer.IScorer;
public class FirstProductSknScorer implements IScorer{
... ... @@ -20,8 +20,8 @@ public class FirstProductSknScorer implements IScorer{
}
@Override
public void addScorer(FunctionScoreQueryBuilder functionScoreQueryBuilder) {
functionScoreQueryBuilder.add(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, firstProductSknList), ScoreFunctionBuilders.weightFactorFunction(FIRST_PRODUCT_WEIGHT));
public void addScorer(YohoFilterFunctionBuilders yohoFilterFunctionBuilders) {
yohoFilterFunctionBuilders.add(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, firstProductSknList), ScoreFunctionBuilders.weightFactorFunction(FIRST_PRODUCT_WEIGHT));
}
}
... ...
... ... @@ -3,12 +3,12 @@ package com.yoho.search.service.scorer.impl;
import java.util.Date;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import com.yoho.search.base.utils.DateUtil;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.models.FirstShelveTimeScore;
import com.yoho.search.models.YohoFilterFunctionBuilders;
import com.yoho.search.service.scorer.IScorer;
public class FirstShelveTimeScorer implements IScorer {
... ... @@ -23,14 +23,14 @@ public class FirstShelveTimeScorer implements IScorer {
}
@Override
public void addScorer(FunctionScoreQueryBuilder functionScoreQueryBuilder) {
public void addScorer(YohoFilterFunctionBuilders yohoFilterFunctionBuilders) {
int todayLastSecond = DateUtil.getLastTimeSecond(new Date());
int limitSecondValue = todayLastSecond - firstShelveTimeScore.getLimitDayCount() * ONE_DAY_SECOND_COUNT;
int scaleSecondTime = firstShelveTimeScore.getScaleDayCount() * ONE_DAY_SECOND_COUNT;
int offsetSecondValue = firstShelveTimeScore.getOffsetDayCount() * ONE_DAY_SECOND_COUNT;
functionScoreQueryBuilder.add(QueryBuilders.rangeQuery(ProductIndexEsField.firstShelveTime).lt(limitSecondValue), ScoreFunctionBuilders.weightFactorFunction(0.5f));
functionScoreQueryBuilder.add(QueryBuilders.rangeQuery(ProductIndexEsField.firstShelveTime).from(limitSecondValue),
ScoreFunctionBuilders.linearDecayFunction(ProductIndexEsField.firstShelveTime, todayLastSecond, scaleSecondTime).setOffset(offsetSecondValue));
yohoFilterFunctionBuilders.add(QueryBuilders.rangeQuery(ProductIndexEsField.firstShelveTime).lt(limitSecondValue), ScoreFunctionBuilders.weightFactorFunction(0.5f));
yohoFilterFunctionBuilders.add(QueryBuilders.rangeQuery(ProductIndexEsField.firstShelveTime).from(limitSecondValue),
ScoreFunctionBuilders.linearDecayFunction(ProductIndexEsField.firstShelveTime, todayLastSecond, scaleSecondTime,offsetSecondValue));
}
}
... ...
package com.yoho.search.service.scorer.impl;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.models.YohoFilterFunctionBuilders;
import com.yoho.search.service.scorer.IScorer;
public class GlobalProductScorer implements IScorer {
... ... @@ -12,8 +12,8 @@ public class GlobalProductScorer implements IScorer {
private static final float GLOABL_DEFUT_WEIGHT = 0.50f;// 全球购得分
@Override
public void addScorer(FunctionScoreQueryBuilder functionScoreQueryBuilder) {
functionScoreQueryBuilder.add(QueryBuilders.termQuery(ProductIndexEsField.isGlobal, "Y"), ScoreFunctionBuilders.weightFactorFunction(GLOABL_DEFUT_WEIGHT));
public void addScorer(YohoFilterFunctionBuilders yohoFilterFunctionBuilders) {
yohoFilterFunctionBuilders.add(QueryBuilders.termQuery(ProductIndexEsField.isGlobal, "Y"), ScoreFunctionBuilders.weightFactorFunction(GLOABL_DEFUT_WEIGHT));
}
}
... ...
... ... @@ -4,24 +4,24 @@ import java.util.Calendar;
import org.elasticsearch.common.lucene.search.function.FieldValueFactorFunction;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import com.yoho.search.base.utils.DateUtil;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.models.YohoFilterFunctionBuilders;
import com.yoho.search.service.scorer.IScorer;
public class NewArriveHeatDescScorer implements IScorer {
@Override
public void addScorer(FunctionScoreQueryBuilder functionScoreQueryBuilder) {
public void addScorer(YohoFilterFunctionBuilders yohoFilterFunctionBuilders) {
// 使用heatValue的值做得分
Calendar now = Calendar.getInstance();
now.add(Calendar.MONTH, -3);
long threeMonthTs = DateUtil.getFirstTimeSecond(now.getTime());
functionScoreQueryBuilder.add(QueryBuilders.rangeQuery(ProductIndexEsField.firstShelveTime).from(threeMonthTs), ScoreFunctionBuilders.fieldValueFactorFunction("heatValue")
yohoFilterFunctionBuilders.add(QueryBuilders.rangeQuery(ProductIndexEsField.firstShelveTime).from(threeMonthTs), ScoreFunctionBuilders.fieldValueFactorFunction("heatValue")
.factor(1f).missing(0).modifier(FieldValueFactorFunction.Modifier.NONE));
functionScoreQueryBuilder.add(QueryBuilders.rangeQuery(ProductIndexEsField.firstShelveTime).to(threeMonthTs), ScoreFunctionBuilders.fieldValueFactorFunction("heatValue")
yohoFilterFunctionBuilders.add(QueryBuilders.rangeQuery(ProductIndexEsField.firstShelveTime).to(threeMonthTs), ScoreFunctionBuilders.fieldValueFactorFunction("heatValue")
.factor(0.5f).missing(0).modifier(FieldValueFactorFunction.Modifier.NONE));
}
... ...
... ... @@ -4,12 +4,12 @@ import java.util.Date;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import com.yoho.search.base.constants.ProductModelValueConstants;
import com.yoho.search.base.utils.DateUtil;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.models.YohoFilterFunctionBuilders;
import com.yoho.search.service.scorer.IScorer;
public class ProblemProductScorer implements IScorer{
... ... @@ -17,14 +17,14 @@ public class ProblemProductScorer implements IScorer{
private static final float PROBLEM_GOODS_WEIGHT = 0.2f;// 一高三低商品降分
@Override
public void addScorer(FunctionScoreQueryBuilder functionScoreQueryBuilder) {
public void addScorer(YohoFilterFunctionBuilders yohoFilterFunctionBuilders) {
BoolQueryBuilder problemGoods = QueryBuilders.boolQuery();
problemGoods.must(QueryBuilders.termsQuery(ProductIndexEsField.activeValue, ProductModelValueConstants.getHighValues()));
problemGoods.must(QueryBuilders.termsQuery(ProductIndexEsField.transferValue, ProductModelValueConstants.getNotHighValues()));
problemGoods.must(QueryBuilders.termsQuery(ProductIndexEsField.giveValue, ProductModelValueConstants.getZeroAndNegativeValues()));
problemGoods.must(QueryBuilders.termsQuery(ProductIndexEsField.profitValue, ProductModelValueConstants.getNotHighValues()));
problemGoods.must(QueryBuilders.rangeQuery(ProductIndexEsField.firstShelveTime).lt(DateUtil.getFirstTimeSecond(DateUtil.addDay(new Date(), -30))));
functionScoreQueryBuilder.add(problemGoods, ScoreFunctionBuilders.weightFactorFunction(PROBLEM_GOODS_WEIGHT));
yohoFilterFunctionBuilders.add(problemGoods, ScoreFunctionBuilders.weightFactorFunction(PROBLEM_GOODS_WEIGHT));
}
}
... ...
package com.yoho.search.service.scorer.impl;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import com.yoho.search.models.YohoFilterFunctionBuilders;
import com.yoho.search.service.scorer.IScorer;
public class RandomScorer implements IScorer {
... ... @@ -21,11 +21,11 @@ public class RandomScorer implements IScorer {
}
@Override
public void addScorer(FunctionScoreQueryBuilder functionScoreQueryBuilder) {
public void addScorer(YohoFilterFunctionBuilders yohoFilterFunctionBuilders) {
if (filter == null) {
functionScoreQueryBuilder.add(ScoreFunctionBuilders.randomFunction(seed));
yohoFilterFunctionBuilders.add(ScoreFunctionBuilders.randomFunction(seed));
} else {
functionScoreQueryBuilder.add(filter, ScoreFunctionBuilders.randomFunction(seed));
yohoFilterFunctionBuilders.add(filter, ScoreFunctionBuilders.randomFunction(seed));
}
}
}
... ...
... ... @@ -7,11 +7,11 @@ import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.models.PhysicalChannelScore;
import com.yoho.search.models.YohoFilterFunctionBuilders;
import com.yoho.search.service.base.SearchRequestParams;
import com.yoho.search.service.scorer.IScorer;
... ... @@ -29,10 +29,10 @@ public class UserChannelSearchScorer implements IScorer {
}
@Override
public void addScorer(FunctionScoreQueryBuilder functionScoreQueryBuilder) {
public void addScorer(YohoFilterFunctionBuilders yohoFilterFunctionBuilders) {
List<PhysicalChannelScore> physicalChannelScores = this.getPhysicalChannelQueryBuilder();
for (PhysicalChannelScore physicalChannelScore : physicalChannelScores) {
functionScoreQueryBuilder.add(physicalChannelScore.getQueryBuilder(), ScoreFunctionBuilders.weightFactorFunction(physicalChannelScore.getWeight()));
yohoFilterFunctionBuilders.add(physicalChannelScore.getQueryBuilder(), ScoreFunctionBuilders.weightFactorFunction(physicalChannelScore.getWeight()));
}
}
... ...
... ... @@ -23,7 +23,7 @@ public interface ISelectionsForApp {
* @param paramMap
* @return
*/
public List<AbstractAggregationBuilder> getAllAggregationBuilders(Map<String, String> paramMap);
public List<AbstractAggregationBuilder<?>> getAllAggregationBuilders(Map<String, String> paramMap);
/**
* 根据聚合结果获取聚合结果
... ...
... ... @@ -78,7 +78,7 @@ public class TblProductService {
searchParam.setOffset(offset);
searchParam.setSize(limit);
// 设置排序字段
List<SortBuilder> sortBuilders = new ArrayList<SortBuilder>();
List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
if (StringUtils.isNotBlank(paramMap.get("order"))) {
String[] sortTypes = paramMap.get("order").split(",");
for (String sortType : sortTypes) {
... ...
... ... @@ -20,7 +20,7 @@ import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
... ... @@ -241,15 +241,14 @@ public class AggProductListServiceImpl implements IAggProductListService, Applic
}
// 2、构造聚合条件
final String firstAggName = "firstAgg";
List<AbstractAggregationBuilder> list = new ArrayList<AbstractAggregationBuilder>();
List<AbstractAggregationBuilder<?>> list = new ArrayList<AbstractAggregationBuilder<?>>();
// 2.1)构造父聚合:品牌或品类聚合【同时按子聚合的sort字段排序】
TermsBuilder firstAggregationBuilder = AggregationBuilders.terms(firstAggName).field(firstAggField).order(Terms.Order.aggregation("sort", aggSort.asc()))
TermsAggregationBuilder firstAggregationBuilder = AggregationBuilders.terms(firstAggName).field(firstAggField).order(Terms.Order.aggregation("sort", aggSort.asc()))
.size(firstAggBucketCount);
// 2.2)添加子聚合:取得分最大的值
firstAggregationBuilder.subAggregation(AggregationBuilders.max("sort").field(aggSort.getSortField()));
// 2.3)添加孙聚合:取打分最高的一个product
firstAggregationBuilder.subAggregation(AggregationBuilders.topHits("product").addSort(SortBuilders.fieldSort(aggSort.getSortField()).order(aggSort.getSortOrder()))
.setSize(topHitsBucketSize));
firstAggregationBuilder.subAggregation(AggregationBuilders.topHits("product").sort(SortBuilders.fieldSort(aggSort.getSortField()).order(aggSort.getSortOrder())).size(topHitsBucketSize));
list.add(firstAggregationBuilder);
searchParam.setAggregationBuilders(list);
... ...
... ... @@ -24,9 +24,9 @@ import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
import org.elasticsearch.search.aggregations.metrics.tophits.TopHitsBuilder;
import org.elasticsearch.search.aggregations.metrics.tophits.TopHitsAggregationBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
... ... @@ -155,7 +155,7 @@ public class BrandServiceImpl implements IBrandService, ApplicationEventPublishe
SearchParam searchParam = searchParamHelper.buildDefault(paramMap);
// 2、构造聚合参数
List<AbstractAggregationBuilder> list = new ArrayList<AbstractAggregationBuilder>();
List<AbstractAggregationBuilder<?>> list = new ArrayList<AbstractAggregationBuilder<?>>();
list.add(AggregationBuilders.terms("brandAlifAgg").field("brandAlif").size(1000).order(Terms.Order.term(true))
.subAggregation(AggregationBuilders.terms("brandAgg").field("brandId").size(1000)));
searchParam.setAggregationBuilders(list);
... ... @@ -346,10 +346,10 @@ public class BrandServiceImpl implements IBrandService, ApplicationEventPublishe
}
// 4、构造聚合条件
List<AbstractAggregationBuilder> list = new ArrayList<AbstractAggregationBuilder>();
List<AbstractAggregationBuilder<?>> list = new ArrayList<AbstractAggregationBuilder<?>>();
boolean isAscOrder = SortOrder.ASC.toString().equalsIgnoreCase(realSortType);
// 4.1)构造父聚合:品牌聚合【同时按子聚合的sort字段排序】
TermsBuilder brandAggregationBuilder = AggregationBuilders.terms("brandAgg").field("brandId").order(Terms.Order.aggregation("sort", isAscOrder)).size(viewNum);
TermsAggregationBuilder brandAggregationBuilder = AggregationBuilders.terms("brandAgg").field("brandId").order(Terms.Order.aggregation("sort", isAscOrder)).size(viewNum);
// 4.2)添加子聚合:sort值为sortFieldName的最大值或最小值,给父聚合排序用
if (isAscOrder) {
brandAggregationBuilder.subAggregation(AggregationBuilders.min("sort").field(realSortField));
... ... @@ -357,8 +357,7 @@ public class BrandServiceImpl implements IBrandService, ApplicationEventPublishe
brandAggregationBuilder.subAggregation(AggregationBuilders.max("sort").field(realSortField));
}
// 4.3)添加孙聚合:按sort取一个product
brandAggregationBuilder.subAggregation(AggregationBuilders.topHits("product")
.addSort(SortBuilders.fieldSort(realSortField).order(isAscOrder ? SortOrder.ASC : SortOrder.DESC)).setSize(1));
brandAggregationBuilder.subAggregation(AggregationBuilders.topHits("product").sort(SortBuilders.fieldSort(realSortField).order(isAscOrder ? SortOrder.ASC : SortOrder.DESC)).size(1));
list.add(brandAggregationBuilder);
searchParam.setAggregationBuilders(list);
... ... @@ -439,13 +438,13 @@ public class BrandServiceImpl implements IBrandService, ApplicationEventPublishe
SearchParam searchParam = searchParamHelper.buildDefault(paramMap);
// 4、构造聚合参数
List<AbstractAggregationBuilder> list = new ArrayList<AbstractAggregationBuilder>();
List<AbstractAggregationBuilder<?>> list = new ArrayList<AbstractAggregationBuilder<?>>();
// 获取取商品的数量
// 构造品牌聚合参数[最多取100个品牌的一个商品,取回来之后再做排序]
TermsBuilder brandAggregationBuilder = AggregationBuilders.terms("brandAgg").field("brandId").size(100);
TermsAggregationBuilder brandAggregationBuilder = AggregationBuilders.terms("brandAgg").field("brandId").size(100);
// 再构造对应HIT到的商品的聚合参数
FieldSortBuilder productSortBuilder = SortBuilders.fieldSort(realSortField).order(realSortOrder);
TopHitsBuilder productTopHitsBuilder = AggregationBuilders.topHits("product").addSort(productSortBuilder).setSize(1);
TopHitsAggregationBuilder productTopHitsBuilder = AggregationBuilders.topHits("product").sort(productSortBuilder).size(1);
brandAggregationBuilder.subAggregation(productTopHitsBuilder);
list.add(brandAggregationBuilder);
searchParam.setAggregationBuilders(list);
... ...
... ... @@ -14,7 +14,7 @@ import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -83,15 +83,15 @@ public class BrandWithShopsServiceImpl implements IBrandWithShopsService, Applic
// 1、构造查询参数
SearchParam searchParam = searchParamHelper.buildDefault(paramMap);
// 2、构造聚合参数
List<AbstractAggregationBuilder> list = new ArrayList<AbstractAggregationBuilder>();
List<AbstractAggregationBuilder<?>> list = new ArrayList<AbstractAggregationBuilder<?>>();
// 2.1)父聚合:brandAlif
TermsBuilder brandAlifAgg = AggregationBuilders.terms("brandAlifAgg").field("brandAlif").size(200).order(Terms.Order.term(true));
TermsAggregationBuilder brandAlifAgg = AggregationBuilders.terms("brandAlifAgg").field("brandAlif").size(200).order(Terms.Order.term(true));
// 2.2)子聚合:brandId
TermsBuilder brandIdAgg = AggregationBuilders.terms("brandIdAgg").field("brandId").size(1000);
TermsAggregationBuilder brandIdAgg = AggregationBuilders.terms("brandIdAgg").field("brandId").size(1000);
// 2.3)孙聚合:shop
TermsBuilder shopAgg = AggregationBuilders.terms("shopIdAgg").field("shopId").size(30);
TermsAggregationBuilder shopAgg = AggregationBuilders.terms("shopIdAgg").field("shopId").size(30);
// 2.4)曾孙聚合:是否全球购
TermsBuilder globalAgg = AggregationBuilders.terms("isGlobalAgg").field("isGlobal").size(5);
TermsAggregationBuilder globalAgg = AggregationBuilders.terms("isGlobalAgg").field("isGlobal").size(5);
list.add(brandAlifAgg.subAggregation(brandIdAgg.subAggregation(shopAgg.subAggregation(globalAgg))));
searchParam.setAggregationBuilders(list);
... ...
... ... @@ -9,8 +9,8 @@ import javax.annotation.PostConstruct;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
... ... @@ -94,7 +94,7 @@ public class HelperServiceImpl extends BaseService implements IHelperService, Ap
// 3.配置Query
MultiMatchQueryBuilder queryBuilder = QueryBuilders.multiMatchQuery(keyword);
queryBuilder.type(MultiMatchQueryBuilder.Type.CROSS_FIELDS);
queryBuilder.operator(MatchQueryBuilder.Operator.OR);
queryBuilder.operator(Operator.OR);
if (paramMap.containsKey("showCustomerService") && StringUtils.isNotBlank(paramMap.get("showCustomerService")) && paramMap.get("showCustomerService").equals("Y")) {
queryBuilder.minimumShouldMatch("30%");
} else {
... ... @@ -108,7 +108,7 @@ public class HelperServiceImpl extends BaseService implements IHelperService, Ap
searchParam.setFiter(boolFilter);
//5.配置Sort
List<SortBuilder> sortBuilders = new ArrayList<SortBuilder>();
List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
if (paramMap.containsKey("showCustomerService") && StringUtils.isNotBlank(paramMap.get("showCustomerService")) && paramMap.get("showCustomerService").equals("Y")) {
sortBuilders.add(SortBuilders.fieldSort("isCustomerService").order(SortOrder.DESC));
}
... ...
... ... @@ -11,8 +11,8 @@ import org.apache.commons.lang.StringUtils;
import org.elasticsearch.common.lucene.search.function.CombineFunction;
import org.elasticsearch.common.lucene.search.function.FieldValueFactorFunction;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
... ... @@ -38,6 +38,7 @@ 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.models.YohoFilterFunctionBuilders;
import com.yoho.search.service.base.SearchCacheService;
import com.yoho.search.service.base.SearchCommonService;
import com.yoho.search.service.service.IRobotQuestionService;
... ... @@ -54,7 +55,7 @@ public class RobotQuestionServiceImpl extends BaseService implements IRobotQuest
@Autowired
private SearchCommonService searchCommonService;
@Autowired
private SearchCacheService searchCacheService;
@Autowired
... ... @@ -65,7 +66,6 @@ public class RobotQuestionServiceImpl extends BaseService implements IRobotQuest
void init() {
searchCache = searchCacheFactory.getDefaultSearchCache();
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
... ... @@ -109,12 +109,13 @@ public class RobotQuestionServiceImpl extends BaseService implements IRobotQuest
// 3.配置Query
MultiMatchQueryBuilder queryBuilder = QueryBuilders.multiMatchQuery(keyword);
queryBuilder.type(MultiMatchQueryBuilder.Type.CROSS_FIELDS);
queryBuilder.operator(MatchQueryBuilder.Operator.OR);
queryBuilder.operator(Operator.OR);
queryBuilder.minimumShouldMatch(minimumShouldMatch);
queryBuilder.field("question", 200).field("keywords", 100);
// 权重字段影响打分,_score = _score + log(1 + 1 * questionSeq)
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder);
functionScoreQueryBuilder.add(ScoreFunctionBuilders.fieldValueFactorFunction("questionSeq").modifier(FieldValueFactorFunction.Modifier.LOG1P).factor(1.0f));
YohoFilterFunctionBuilders yohoFilterFunctionBuilders = new YohoFilterFunctionBuilders();
yohoFilterFunctionBuilders.add(ScoreFunctionBuilders.fieldValueFactorFunction("questionSeq").modifier(FieldValueFactorFunction.Modifier.LOG1P).factor(1.0f));
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder, yohoFilterFunctionBuilders.getFilterFunctionBuilders());
functionScoreQueryBuilder.boostMode(CombineFunction.SUM);
searchParam.setQuery(functionScoreQueryBuilder);
... ... @@ -124,14 +125,14 @@ public class RobotQuestionServiceImpl extends BaseService implements IRobotQuest
searchParam.setFiter(boolFilter);
// 5.配置sort
List<SortBuilder> sortBuilders = new ArrayList<SortBuilder>();
List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
sortBuilders.add(SortBuilders.fieldSort("_score").order(SortOrder.DESC));
sortBuilders.add(SortBuilders.fieldSort("questionSeq").order(SortOrder.DESC));
searchParam.setSortBuilders(sortBuilders);
// 6.走本地缓存
final String indexName = ISearchConstants.INDEX_NAME_ROBOTQUESTION;
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(searchCache,indexName, searchParam);
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(searchCache, indexName, searchParam);
if (cacheObject != null) {
SearchCacheMatchLogger.doSearchCacheMatchLog("/robotquestion.json", paramMap);
return new SearchApiResult().setData(cacheObject);
... ... @@ -150,7 +151,7 @@ public class RobotQuestionServiceImpl extends BaseService implements IRobotQuest
dataMap.put("page_size", searchParam.getSize());
dataMap.put("page_total", searchResult.getTotalPage());
dataMap.put("answer_list", this.getRobotQuestionMap(searchResult.getResultList()));
searchCacheService.addJSONObjectToCache(searchCache,indexName, searchParam, dataMap);
searchCacheService.addJSONObjectToCache(searchCache, indexName, searchParam, dataMap);
return new SearchApiResult().setData(dataMap);
} catch (Exception e) {
publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_ROBOTQUESTION.getEventName(), EventReportEnum.SEARCHCONTROLLER_ROBOTQUESTION.getFunctionName(),
... ...
... ... @@ -14,8 +14,8 @@ import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.common.lucene.search.function.CombineFunction;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
... ... @@ -39,6 +39,7 @@ import com.yoho.search.common.cache.model.SearchCache;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.models.YohoFilterFunctionBuilders;
import com.yoho.search.service.base.SearchCacheService;
import com.yoho.search.service.base.SearchCommonService;
import com.yoho.search.service.base.SearchRequestParams;
... ... @@ -271,7 +272,7 @@ public class SearchRecommendServiceImpl implements ISearchRecommendService {
searchParam.setFiter(boolFilter);
// 3.6) 按照得分、权重、数量的规则降序排序
List<SortBuilder> sortBuilders = new ArrayList<>(3);
List<SortBuilder<?>> sortBuilders = new ArrayList<>(3);
sortBuilders.add(SortBuilders.fieldSort("_score").order(SortOrder.DESC));
sortBuilders.add(SortBuilders.fieldSort("weight").order(SortOrder.DESC));
sortBuilders.add(SortBuilders.fieldSort(countField).order(SortOrder.DESC));
... ... @@ -301,17 +302,15 @@ public class SearchRecommendServiceImpl implements ISearchRecommendService {
}
private QueryBuilder buildQueryBuilderByLimit(List<String> terms) {
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(QueryBuilders.matchAllQuery());
YohoFilterFunctionBuilders yohoFilterFunctionBuilders = new YohoFilterFunctionBuilders();
// 给品类类型的关键词加分
functionScoreQueryBuilder.add(QueryBuilders.termQuery("type", Integer.valueOf(2)), ScoreFunctionBuilders.weightFactorFunction(3));
yohoFilterFunctionBuilders.add(QueryBuilders.termQuery("type", Integer.valueOf(2)), ScoreFunctionBuilders.weightFactorFunction(3));
// 按词出现的顺序加分
for (int i = 0; i < terms.size(); i++) {
functionScoreQueryBuilder.add(QueryBuilders.termQuery("standardKeyword", CharUtils.standardized(terms.get(i))),
yohoFilterFunctionBuilders.add(QueryBuilders.termQuery("standardKeyword", CharUtils.standardized(terms.get(i))),
ScoreFunctionBuilders.weightFactorFunction(terms.size() - i));
}
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(yohoFilterFunctionBuilders.getFilterFunctionBuilders());
functionScoreQueryBuilder.boostMode(CombineFunction.SUM);
return functionScoreQueryBuilder;
}
... ... @@ -320,27 +319,24 @@ public class SearchRecommendServiceImpl implements ISearchRecommendService {
// 3.1) 对于suggest的multi-fields至少要有一个字段匹配到 匹配打分为常量1
MultiMatchQueryBuilder queryBuilder = QueryBuilders
.multiMatchQuery(keywordsToSearch.trim().toLowerCase(), "keyword", "keyword.keyword_ik", "keyword.keyword_pinyin", "keyword.keyword_jianpin",
"keyword.keyword_lowercase").analyzer("ik_smart").type(MultiMatchQueryBuilder.Type.BEST_FIELDS).operator(MatchQueryBuilder.Operator.OR)
.minimumShouldMatch("1");
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(QueryBuilders.constantScoreQuery(queryBuilder));
"keyword.keyword_lowercase").analyzer("ik_smart").type(MultiMatchQueryBuilder.Type.BEST_FIELDS).operator(Operator.OR).minimumShouldMatch("1");
YohoFilterFunctionBuilders yohoFilterFunctionBuilders = new YohoFilterFunctionBuilders();
for (String term : termSet) {
// 3.2) 对于完全匹配Term的加1分
functionScoreQueryBuilder.add(QueryBuilders.termQuery("standardKeyword", CharUtils.standardized(term)), ScoreFunctionBuilders.weightFactorFunction(1));
yohoFilterFunctionBuilders.add(QueryBuilders.termQuery("standardKeyword", CharUtils.standardized(term)), ScoreFunctionBuilders.weightFactorFunction(1));
// 3.3) 对于匹配到一个Term的加2分
functionScoreQueryBuilder.add(QueryBuilders.termQuery("keyword.keyword_ik", term), ScoreFunctionBuilders.weightFactorFunction(2));
yohoFilterFunctionBuilders.add(QueryBuilders.termQuery("keyword.keyword_ik", term), ScoreFunctionBuilders.weightFactorFunction(2));
}
// 3.4) 处理性别相关的关键词
if (termSet.contains("男") && !termSet.contains("女")) {
functionScoreQueryBuilder.add(QueryBuilders.termQuery("keyword.keyword_ik", "女"), ScoreFunctionBuilders.weightFactorFunction(-5));
yohoFilterFunctionBuilders.add(QueryBuilders.termQuery("keyword.keyword_ik", "女"), ScoreFunctionBuilders.weightFactorFunction(-5));
} else if (!termSet.contains("男") && termSet.contains("女")) {
functionScoreQueryBuilder.add(QueryBuilders.termQuery("keyword.keyword_ik", "男"), ScoreFunctionBuilders.weightFactorFunction(-5));
yohoFilterFunctionBuilders.add(QueryBuilders.termQuery("keyword.keyword_ik", "男"), ScoreFunctionBuilders.weightFactorFunction(-5));
} else if (!termSet.contains("男") && !termSet.contains("女")) {
functionScoreQueryBuilder.add(QueryBuilders.termsQuery("keyword.keyword_ik", Arrays.asList("男", "女")), ScoreFunctionBuilders.weightFactorFunction(-2));
yohoFilterFunctionBuilders.add(QueryBuilders.termsQuery("keyword.keyword_ik", Arrays.asList("男", "女")), ScoreFunctionBuilders.weightFactorFunction(-2));
}
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(QueryBuilders.constantScoreQuery(queryBuilder),
yohoFilterFunctionBuilders.getFilterFunctionBuilders());
functionScoreQueryBuilder.boostMode(CombineFunction.SUM);
return functionScoreQueryBuilder;
}
... ...
... ... @@ -110,7 +110,7 @@ public class SearchSortGroupService implements ISearchSortGroupService, Applicat
}
}
// 构造聚合条件
List<AbstractAggregationBuilder> sortGroupBuilders = new ArrayList<AbstractAggregationBuilder>();
List<AbstractAggregationBuilder<?>> sortGroupBuilders = new ArrayList<AbstractAggregationBuilder<?>>();
IAggregation sortGroupAggregation = aggregationFactoryService.getSortGroupAggregation(paramMap);
sortGroupBuilders.add(sortGroupAggregation.getBuilder());
searchParam.setAggregationBuilders(sortGroupBuilders);
... ... @@ -167,7 +167,7 @@ public class SearchSortGroupService implements ISearchSortGroupService, Applicat
SearchParam searchParam = searchParamHelper.buildWithMustFilter(paramMap,mustFilter);
// 构造聚合条件
List<AbstractAggregationBuilder> sortGroupBuilders = new ArrayList<AbstractAggregationBuilder>();
List<AbstractAggregationBuilder<?>> sortGroupBuilders = new ArrayList<AbstractAggregationBuilder<?>>();
IAggregation sortGroupAggregation = aggregationFactoryService.getSortGroupAggregation(paramMap);
sortGroupBuilders.add(sortGroupAggregation.getBuilder());
searchParam.setAggregationBuilders(sortGroupBuilders);
... ...
... ... @@ -107,8 +107,8 @@ public class SelectionForAppImpl implements ISelectionsForApp {
* 构造不带提前聚合逻辑的报文
**/
@Override
public List<AbstractAggregationBuilder> getAllAggregationBuilders(Map<String, String> paramMap) {
List<AbstractAggregationBuilder> list = new ArrayList<AbstractAggregationBuilder>();
public List<AbstractAggregationBuilder<?>> getAllAggregationBuilders(Map<String, String> paramMap) {
List<AbstractAggregationBuilder<?>> list = new ArrayList<AbstractAggregationBuilder<?>>();
// 1)年龄层
list.add(aggregationFactoryService.getAgeLevelAggregation().getBuilder());
... ...
... ... @@ -236,8 +236,8 @@ public class SelectionsForPcImpl implements ISelectionsForPc {
* @param paramMap
* @return
*/
private List<AbstractAggregationBuilder> getOtherAggregations(Map<String, String> paramMap) {
List<AbstractAggregationBuilder> list = new ArrayList<AbstractAggregationBuilder>();
private List<AbstractAggregationBuilder<?>> getOtherAggregations(Map<String, String> paramMap) {
List<AbstractAggregationBuilder<?>> list = new ArrayList<AbstractAggregationBuilder<?>>();
// 1)年龄层
if (!paramMap.containsKey("ageLevel") || StringUtils.isBlank(paramMap.get("ageLevel"))) {
list.add(aggregationFactoryService.getAgeLevelAggregation().getBuilder());
... ...
... ... @@ -12,10 +12,11 @@ import javax.annotation.PostConstruct;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
... ... @@ -25,7 +26,7 @@ import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
... ... @@ -89,7 +90,7 @@ public class ShopListServiceImpl implements IShopListService {
// 2、构建query
MultiMatchQueryBuilder queryBuilder = QueryBuilders.multiMatchQuery(keyword);
queryBuilder.operator(MatchQueryBuilder.Operator.OR);
queryBuilder.operator(Operator.OR);
StringBuilder searchField = new StringBuilder();
searchField.append("brandName.brandName_lowercase^4000,brandName^900").append(',');
searchField.append("shopName.shopName_lowercase^4000,shopName^900").append(',');
... ... @@ -106,8 +107,9 @@ public class ShopListServiceImpl implements IShopListService {
}
queryBuilder.minimumShouldMatch("100%");
// 2.1 全球购得分减半
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder);
functionScoreQueryBuilder.add(QueryBuilders.termQuery("isGlobal", "Y"), ScoreFunctionBuilders.weightFactorFunction(0.5f));
FilterFunctionBuilder[] filterFunctionBuilders = new FilterFunctionBuilder[1];
filterFunctionBuilders[0] = new FilterFunctionBuilder(QueryBuilders.termQuery("isGlobal", "Y"), ScoreFunctionBuilders.weightFactorFunction(0.5f));
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder,filterFunctionBuilders);
searchParam.setQuery(functionScoreQueryBuilder);
// 3、构建filter
... ... @@ -142,14 +144,13 @@ public class ShopListServiceImpl implements IShopListService {
// 4、构建聚合条件
final String firstAggName = "firstAgg";
SearchSort aggSort = new SearchSort("_score", SortOrder.DESC);
List<AbstractAggregationBuilder> list = new ArrayList<AbstractAggregationBuilder>();
List<AbstractAggregationBuilder<?>> list = new ArrayList<AbstractAggregationBuilder<?>>();
// 2.1)构造父聚合:品牌或品类聚合【同时按子聚合的sort字段排序】
TermsBuilder firstAggregationBuilder = AggregationBuilders.terms(firstAggName).field("shopId").order(Terms.Order.aggregation("sort", aggSort.asc())).size(50);
TermsAggregationBuilder firstAggregationBuilder = AggregationBuilders.terms(firstAggName).field("shopId").order(Terms.Order.aggregation("sort", aggSort.asc())).size(50);
// 2.2)添加子聚合:取得分最大的值
firstAggregationBuilder.subAggregation(AggregationBuilders.max("sort").field(aggSort.getSortField()));
// 2.3)添加孙聚合:取打分最高的一个product
firstAggregationBuilder.subAggregation(AggregationBuilders.topHits("product").addSort(SortBuilders.fieldSort(aggSort.getSortField()).order(aggSort.getSortOrder()))
.setSize(1));
firstAggregationBuilder.subAggregation(AggregationBuilders.topHits("product").sort(SortBuilders.fieldSort(aggSort.getSortField()).order(aggSort.getSortOrder())).size(1));
list.add(firstAggregationBuilder);
searchParam.setAggregationBuilders(list);
... ...
... ... @@ -11,8 +11,8 @@ import javax.annotation.PostConstruct;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
... ... @@ -148,7 +148,7 @@ public class ShopsServiceImpl extends BaseService implements IShopsService, Appl
// 2、配置keyword的查询字段以及权重设置
MultiMatchQueryBuilder queryBuilder = QueryBuilders.multiMatchQuery(keyword);
queryBuilder.operator(MatchQueryBuilder.Operator.OR);
queryBuilder.operator(Operator.OR);
queryBuilder.field("brandName.brandName_ansj", 100).field("brandName.brandName_pinyin").field("brandNameCn.brandNameCn_ansj").field("brandNameCn.brandNameCn_pinyin")
.field("brandNameEn").field("brandDomain", 50);
queryBuilder.minimumShouldMatch("100%");
... ... @@ -302,7 +302,7 @@ public class ShopsServiceImpl extends BaseService implements IShopsService, Appl
}
searchParam.setQuery(boolQueryBuilder);
// 按得分排序
List<SortBuilder> sortBuilders = new ArrayList<SortBuilder>();
List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
sortBuilders.add(SortBuilders.fieldSort("_score").order(SortOrder.DESC));
searchParam.setSortBuilders(sortBuilders);
// 结果只返回shopId
... ...
... ... @@ -11,6 +11,7 @@ import javax.annotation.PostConstruct;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.common.lucene.search.function.CombineFunction;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
... ... @@ -19,7 +20,7 @@ import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
... ... @@ -36,6 +37,7 @@ import com.yoho.search.common.cache.model.SearchCache;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.models.YohoFilterFunctionBuilders;
import com.yoho.search.service.aggregations.common.SimpleFieldAgg;
import com.yoho.search.service.base.SearchCacheService;
import com.yoho.search.service.base.SearchCommonService;
... ... @@ -64,13 +66,13 @@ public class SimilarProductServiceImpl extends BaseService implements ISimilarPr
private ProductIndexBaseService productIndexBaseService;
@Autowired
private SearchCommonService searchCommonService;
@Autowired
private SearchCacheService searchCacheService;
@Autowired
private SearchCacheFactory searchCacheFactory;
private SearchCache searchLikeSearchCache;
@PostConstruct
void init() {
searchLikeSearchCache = searchCacheFactory.getSearchLikeSearchCache();
... ... @@ -119,9 +121,9 @@ public class SimilarProductServiceImpl extends BaseService implements ISimilarPr
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, productSknList));
searchParam.setFiter(boolFilter);
// 2、设置聚合条件,获取所有的品类和品牌
TermsBuilder smallSortIdAgg = AggregationBuilders.terms(SMALL_SORT_AGG_NAME).field(ProductIndexEsField.smallSortId).size(100);
TermsBuilder brandIdAgg = AggregationBuilders.terms(BRAND_AGG_NAME).field(ProductIndexEsField.brandId).size(100);
List<AbstractAggregationBuilder> aggregationBuilders = new ArrayList<>();
TermsAggregationBuilder smallSortIdAgg = AggregationBuilders.terms(SMALL_SORT_AGG_NAME).field(ProductIndexEsField.smallSortId).size(100);
TermsAggregationBuilder brandIdAgg = AggregationBuilders.terms(BRAND_AGG_NAME).field(ProductIndexEsField.brandId).size(100);
List<AbstractAggregationBuilder<?>> aggregationBuilders = new ArrayList<>();
aggregationBuilders.add(smallSortIdAgg);
aggregationBuilders.add(brandIdAgg);
searchParam.setAggregationBuilders(aggregationBuilders);
... ... @@ -129,7 +131,7 @@ public class SimilarProductServiceImpl extends BaseService implements ISimilarPr
searchParam.setOffset(0);
searchParam.setSize(0);
// 4、先从缓存中获取,如果能取到,则直接返回
JSONObject sortAndBrandJSONObject = searchCacheService.getJSONObjectFromCache(searchLikeSearchCache,ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
JSONObject sortAndBrandJSONObject = searchCacheService.getJSONObjectFromCache(searchLikeSearchCache, ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
if (sortAndBrandJSONObject != null) {
return sortAndBrandJSONObject;
}
... ... @@ -146,7 +148,7 @@ public class SimilarProductServiceImpl extends BaseService implements ISimilarPr
sortAndBrandJSONObject = new JSONObject();
sortAndBrandJSONObject.put(BRAND_IDS, brandIds);
sortAndBrandJSONObject.put(SMALL_SORT_IDS, smallSortIds);
searchCacheService.addJSONObjectToCache(searchLikeSearchCache,ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam, sortAndBrandJSONObject);
searchCacheService.addJSONObjectToCache(searchLikeSearchCache, ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam, sortAndBrandJSONObject);
return sortAndBrandJSONObject;
}
... ... @@ -185,7 +187,7 @@ public class SimilarProductServiceImpl extends BaseService implements ISimilarPr
List<SimpleFieldAgg> simpleFieldAggs = new ArrayList<SimpleFieldAgg>();
simpleFieldAggs.add(new SimpleFieldAgg(SMALL_SORT_AGG_NAME, ProductIndexEsField.smallSortId, 100));
simpleFieldAggs.add(new SimpleFieldAgg(BRAND_AGG_NAME, ProductIndexEsField.brandId, 100));
AbstractAggregationBuilder smallSortAndBrandAgg = AggCommonHelper.getTopHitAggregation(simpleFieldAggs, similarProductOrder, 2);
AbstractAggregationBuilder<?> smallSortAndBrandAgg = AggCommonHelper.getTopHitAggregation(simpleFieldAggs, similarProductOrder, 2);
searchParam.setAggregationBuilders(Arrays.asList(smallSortAndBrandAgg));
// 5、设置分页
... ... @@ -193,7 +195,7 @@ public class SimilarProductServiceImpl extends BaseService implements ISimilarPr
searchParam.setSize(0);
// 6、先从缓存中获取,如果能取到,则直接返回
JSONArray recommendedSknJSONArray = searchCacheService.getJSONArrayFromCache(searchLikeSearchCache,ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
JSONArray recommendedSknJSONArray = searchCacheService.getJSONArrayFromCache(searchLikeSearchCache, ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
if (recommendedSknJSONArray != null) {
return this.jsonArrayToList(recommendedSknJSONArray, String.class);
}
... ... @@ -228,7 +230,7 @@ public class SimilarProductServiceImpl extends BaseService implements ISimilarPr
searchParam.setAggregationBuilders(null);
searchParam.setOffset((page - 1) * pageSize);
searchParam.setSize(pageSize);
List<SortBuilder> sortBuilders = new ArrayList<>();
List<SortBuilder<?>> sortBuilders = new ArrayList<>();
sortBuilders.add(SortBuilders.fieldSort(ES_SCORE_FIELD).order(SortOrder.DESC));
sortBuilders.add(SortBuilders.fieldSort(ProductIndexEsField.salesNum).order(SortOrder.DESC));
sortBuilders.add(SortBuilders.fieldSort(ProductIndexEsField.firstShelveTime).order(SortOrder.DESC));
... ... @@ -236,7 +238,7 @@ public class SimilarProductServiceImpl extends BaseService implements ISimilarPr
searchParam.setSortBuilders(sortBuilders);
// 2.从缓存中获取数据
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(searchLikeSearchCache,indexName, searchParam);
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(searchLikeSearchCache, indexName, searchParam);
if (cacheObject != null) {
return new SearchApiResult().setData(cacheObject);
}
... ... @@ -253,7 +255,7 @@ public class SimilarProductServiceImpl extends BaseService implements ISimilarPr
dataMap.put("page_total", searchResult.getTotalPage());
dataMap.put("product_list", productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList()));
// 5.将结果存进缓存
searchCacheService.addJSONObjectToCache(searchLikeSearchCache,indexName, searchParam, dataMap);
searchCacheService.addJSONObjectToCache(searchLikeSearchCache, indexName, searchParam, dataMap);
return new SearchApiResult().setData(dataMap);
}
... ... @@ -265,7 +267,7 @@ public class SimilarProductServiceImpl extends BaseService implements ISimilarPr
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.termQuery(ProductIndexEsField.isOutlets, 2));
boolFilter.must(QueryBuilders.rangeQuery(ProductIndexEsField.storageNum).gte(1));
... ... @@ -293,11 +295,10 @@ public class SimilarProductServiceImpl extends BaseService implements ISimilarPr
}
private QueryBuilder builderProductQueryBuilder(Map<String, String> paramMap, List<String> recommendedSknList) {
QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder);
YohoFilterFunctionBuilders filterFunctionBuilders = new YohoFilterFunctionBuilders();
// 如果参数中包含firstProductSkn,则放在第一个
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_FIRST_PRODUCRSKN)) {
functionScoreQueryBuilder.add(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, paramMap.getOrDefault(SearchRequestParams.PARAM_SEARCH_FIRST_PRODUCRSKN, "")),
filterFunctionBuilders.add(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, paramMap.getOrDefault(SearchRequestParams.PARAM_SEARCH_FIRST_PRODUCRSKN, "")),
ScoreFunctionBuilders.weightFactorFunction(100000));
}
// 针对推荐出来的SKN做加分
... ... @@ -305,11 +306,13 @@ public class SimilarProductServiceImpl extends BaseService implements ISimilarPr
Map<Integer, List<String>> recommendSknMap = this.splitProductSknList(recommendedSknList, 2);
float currentGroupScore = 1000;
for (Map.Entry<Integer, List<String>> entry : recommendSknMap.entrySet()) {
functionScoreQueryBuilder.add(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, entry.getValue()),
filterFunctionBuilders.add(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, entry.getValue()),
ScoreFunctionBuilders.weightFactorFunction(currentGroupScore));
currentGroupScore = currentGroupScore - 10;
}
}
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(QueryBuilders.matchAllQuery(), filterFunctionBuilders.getFilterFunctionBuilders());
functionScoreQueryBuilder.boostMode(CombineFunction.MULTIPLY);
return functionScoreQueryBuilder;
}
... ...
... ... @@ -7,15 +7,16 @@ import java.util.Map;
import org.apache.commons.collections.MapUtils;
import org.elasticsearch.common.lucene.search.function.CombineFunction;
import org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.index.query.functionscore.script.ScriptScoreFunctionBuilder;
import org.elasticsearch.index.query.functionscore.ScriptScoreFunctionBuilder;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.ScriptType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -33,6 +34,7 @@ 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.models.YohoFilterFunctionBuilders;
import com.yoho.search.service.base.SearchCacheService;
import com.yoho.search.service.base.SearchCommonService;
import com.yoho.search.service.base.index.ProductIndexBaseService;
... ... @@ -77,7 +79,7 @@ public class SknImageVectorsServiceImpl implements ISknImageVectorsService, Appl
logger.info("[func=searchSknByPhoto][param={}][begin={}]", paramMap.toString(), begin);
String vectors_32 = paramMap.get(VECTORS_32_KEY);
String vectors_128 = paramMap.get(VECTORS_128_KEY);
//Integer viewNum = MapUtils.getInteger(paramMap, "viewNum", 20);
// Integer viewNum = MapUtils.getInteger(paramMap, "viewNum", 20);
Integer viewNum = 20;
// 2、按32维向量召回
List<Integer> sknListFromVector32 = this.querySknListWithVector32(vectors_32);
... ... @@ -201,8 +203,6 @@ public class SknImageVectorsServiceImpl implements ISknImageVectorsService, Appl
private FunctionScoreQueryBuilder buildFunctionScoreQueryBuilder(String vectorsFieldName, String vectorsValue, List<Integer> productSkns) {
MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery().boost(1.0f);
// query
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(matchAllQueryBuilder);
// script_score
String[] feaArray = vectorsValue.split(",");
Long[] fea = new Long[feaArray.length];
... ... @@ -215,19 +215,22 @@ public class SknImageVectorsServiceImpl implements ISknImageVectorsService, Appl
params.put("fea", fea);
params.put("verbose", true);
String inlineScript = SCRIPT_NAME;
Script script = new Script(inlineScript, ScriptService.ScriptType.INLINE, "native", params);
Script script = new Script(ScriptType.INLINE, "native", inlineScript, params);
ScriptScoreFunctionBuilder scriptBuilder = ScoreFunctionBuilders.scriptFunction(script);
// function_score
// filter,sknFilterString为空是第一次粗查询,不为空是第二次精查询
YohoFilterFunctionBuilders yohoFilterFunctionBuilders = new YohoFilterFunctionBuilders();
if (productSkns == null || productSkns.isEmpty()) {
QueryBuilder filter = QueryBuilders.matchAllQuery().boost(1.0f);
functionScoreQueryBuilder.add(filter, scriptBuilder);
yohoFilterFunctionBuilders.add(filter, scriptBuilder);
} else {
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
boolFilter.must(QueryBuilders.termsQuery("productSkn", productSkns));
functionScoreQueryBuilder.add(boolFilter, scriptBuilder);
yohoFilterFunctionBuilders.add(boolFilter, scriptBuilder);
}
functionScoreQueryBuilder.scoreMode("sum");
// query
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(matchAllQueryBuilder, yohoFilterFunctionBuilders.getFilterFunctionBuilders());
functionScoreQueryBuilder.scoreMode(FiltersFunctionScoreQuery.ScoreMode.SUM);
functionScoreQueryBuilder.boostMode(CombineFunction.REPLACE);
functionScoreQueryBuilder.maxBoost(3.4028235E38F);
functionScoreQueryBuilder.boost(1.0f);
... ... @@ -242,12 +245,12 @@ public class SknImageVectorsServiceImpl implements ISknImageVectorsService, Appl
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));
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, productSkns));
return boolFilter;
}
... ... @@ -265,7 +268,7 @@ public class SknImageVectorsServiceImpl implements ISknImageVectorsService, Appl
try {
// 1.参数获取
String param_product_skn = paramMap.get("product_skn");
//Integer viewNum = MapUtils.getInteger(paramMap, "viewNum", 20);
// Integer viewNum = MapUtils.getInteger(paramMap, "viewNum", 20);
Integer viewNum = 20;
List<Integer> productSkns = new ArrayList<Integer>();
for (String productSkn : param_product_skn.split(",")) {
... ...
... ... @@ -15,6 +15,7 @@ import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.term.TermSuggestion;
import org.slf4j.Logger;
... ... @@ -159,7 +160,7 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
searchParam.setFiter(boolFilter);
// 3)设置排序字段
List<SortBuilder> sortBuilders = new ArrayList<SortBuilder>();
List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
sortBuilders.add(SortBuilders.fieldSort("weight").order(SortOrder.DESC));
sortBuilders.add(SortBuilders.fieldSort(countEsField).order(SortOrder.DESC));
searchParam.setSortBuilders(sortBuilders);
... ... @@ -217,8 +218,9 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
try {
SearchParam suggestSearchParam = new SearchParam();
suggestSearchParam.setSize(0);
suggestSearchParam.setSuggestionBuilder(SuggestBuilders.termSuggestion("keyword_suggestion").text(keyword.trim().toLowerCase()).field("keyword.keyword_lowercase")
.size(1));
SuggestBuilder suggestionBuilder = new SuggestBuilder();
suggestionBuilder.addSuggestion("keyword_suggestion", SuggestBuilders.termSuggestion("keyword.keyword_lowercase").text(keyword.trim().toLowerCase()).size(1));
JSONObject jsonObject = searchCacheService.getJSONObjectFromCache(searchCache, ISearchConstants.INDEX_NAME_SUGGEST, suggestSearchParam);
if (jsonObject != null) {
SearchCacheMatchLogger.doSearchCacheMatchLog("getSpellingCorrectKeyword", keyword);
... ... @@ -328,7 +330,7 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
boolFilter.must(QueryBuilders.rangeQuery(countEsField).gte(Integer.valueOf(count)));
searchParam.setFiter(boolFilter);
// 1.2)根据count排序
List<SortBuilder> sortBuilders = new ArrayList<SortBuilder>();
List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
sortBuilders.add(SortBuilders.fieldSort(countEsField).order(SortOrder.DESC));
searchParam.setSortBuilders(sortBuilders);
searchParam.setOffset((page - 1) * pageSize);
... ...