|
|
package com.yoho.search.service.servicenew.impl;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
|
...
|
...
|
@@ -29,6 +32,7 @@ import com.yoho.search.service.service.helper.SearchSortHelper; |
|
|
import com.yoho.search.service.servicenew.ISearchLikeService;
|
|
|
import com.yoho.search.service.utils.SearchRequestParams;
|
|
|
import com.yoho.search.service.vo.SearchApiResult;
|
|
|
import com.yoho.search.service.vo.SearchFieldBoost;
|
|
|
|
|
|
@Service
|
|
|
public class SearchLikeServiceImpl implements ISearchLikeService {
|
...
|
...
|
@@ -46,6 +50,45 @@ public class SearchLikeServiceImpl implements ISearchLikeService { |
|
|
@Autowired
|
|
|
private FunctionScoreSearchHelper functionScoreSearchHelper;
|
|
|
|
|
|
private List<SearchFieldBoost> searchFieldBoosts = new ArrayList<SearchFieldBoost>();
|
|
|
|
|
|
private int sortMaxBoost = 1000;
|
|
|
private int productNameMaxBoost = 700;
|
|
|
private int brandMaxBoost = 600;
|
|
|
private int standardOnlyNamesBoost = 400;
|
|
|
private int otherBoost = 200;
|
|
|
|
|
|
@PostConstruct
|
|
|
void init() {
|
|
|
// 品类权重
|
|
|
List<String> searchFields = Arrays.asList("smallSort", "smallSort.smallSort_pinyin", "middleSort", "middleSort.middleSort_pinyin", "maxSort", "maxSort.maxSort_pinyin");
|
|
|
this.addSearchFieldBoost(searchFields, sortMaxBoost);
|
|
|
|
|
|
// 商品名称权重
|
|
|
searchFields = Arrays.asList("productName.productName_ansj");
|
|
|
this.addSearchFieldBoost(searchFields, productNameMaxBoost);
|
|
|
|
|
|
// 品牌权重
|
|
|
searchFields = Arrays.asList("brandName.brandName_lowercase","brandName","brandNameCn","brandNameCn.brandNameCn_pinyin","brandDomain","brandNameEn");
|
|
|
this.addSearchFieldBoost(searchFields, brandMaxBoost);
|
|
|
|
|
|
// 规则权重
|
|
|
searchFields = Arrays.asList("standardOnlyNames.standardOnlyNames_ansj","standardOnlyNames.standardOnlyNames_pinyin");
|
|
|
this.addSearchFieldBoost(searchFields, standardOnlyNamesBoost);
|
|
|
|
|
|
//其他字段权重
|
|
|
searchFields = Arrays.asList("specialSearchField","productKeyword","brandKeyword","genderS","searchField_ansj","searchField");
|
|
|
this.addSearchFieldBoost(searchFields, otherBoost);
|
|
|
}
|
|
|
|
|
|
private void addSearchFieldBoost(List<String> searchFields,float maxBoost){
|
|
|
float boost = maxBoost;
|
|
|
for (String searchField : searchFields) {
|
|
|
searchFieldBoosts.add(new SearchFieldBoost(searchField, boost));
|
|
|
boost = boost -20;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult searchLike(Map<String, String> paramMap) {
|
|
|
// 1、获取参数
|
...
|
...
|
@@ -71,19 +114,10 @@ public class SearchLikeServiceImpl implements ISearchLikeService { |
|
|
// 3、构建MultiMatchQueryBuilder
|
|
|
String product_name = (String) productInfo.get("product_name");
|
|
|
String brand_name = (String) productInfo.get("brand_name");
|
|
|
MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(product_name +","+ brand_name);
|
|
|
MultiMatchQueryBuilder.Type multiMatchQueryBuilderType = searchCommonHelper.getMultiMatchQueryBuilderType();
|
|
|
if (multiMatchQueryBuilderType != null) {
|
|
|
multiMatchQueryBuilder.type(multiMatchQueryBuilderType);
|
|
|
}
|
|
|
List<String> fields = ISearchConstants.SEARCH_DEFAULT_FIELD;
|
|
|
for (String field : fields) {
|
|
|
String[] fieldBoost = field.split("\\^");
|
|
|
if (fieldBoost.length == 2) {
|
|
|
multiMatchQueryBuilder.field(fieldBoost[0], Float.parseFloat(fieldBoost[1]));
|
|
|
} else if (fieldBoost.length == 1) {
|
|
|
multiMatchQueryBuilder.field(fieldBoost[0]);
|
|
|
}
|
|
|
MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(product_name + "," + brand_name);
|
|
|
multiMatchQueryBuilder.type(MultiMatchQueryBuilder.Type.CROSS_FIELDS);
|
|
|
for (SearchFieldBoost searchFieldBoost : searchFieldBoosts) {
|
|
|
multiMatchQueryBuilder.field(searchFieldBoost.getFieldName(), searchFieldBoost.getBoost());
|
|
|
}
|
|
|
multiMatchQueryBuilder.minimumShouldMatch("30%");
|
|
|
// 4、添加个性化打分
|
...
|
...
|
@@ -97,16 +131,15 @@ public class SearchLikeServiceImpl implements ISearchLikeService { |
|
|
if (!StringUtils.isBlank(gender)) {
|
|
|
boolFilter.must(QueryBuilders.termQuery("gender", gender));
|
|
|
}
|
|
|
// 5.2)找相似要具体到到中分类或小分类,大分类范围太广
|
|
|
BoolQueryBuilder sortFilter = QueryBuilders.boolQuery();
|
|
|
Integer max_sort_id = (Integer) productInfo.get("max_sort_id");
|
|
|
Integer middle_sort_id = (Integer) productInfo.get("middle_sort_id");
|
|
|
Integer small_sort_id = (Integer) productInfo.get("small_sort_id");
|
|
|
sortFilter.should(QueryBuilders.termQuery("maxSortId", max_sort_id));
|
|
|
sortFilter.should(QueryBuilders.termQuery("middleSortId", middle_sort_id));
|
|
|
sortFilter.should(QueryBuilders.termQuery("smallSortId", small_sort_id));
|
|
|
boolFilter.must(sortFilter);
|
|
|
|
|
|
// 5.2)设置默认的过滤条件
|
|
|
// 5.3)设置默认的过滤条件
|
|
|
boolFilter.must(QueryBuilders.termQuery("status", 1));
|
|
|
boolFilter.must(QueryBuilders.termQuery("isOutlets", 2));
|
|
|
boolFilter.must(QueryBuilders.termQuery("attribute", 1));
|
...
|
...
|
@@ -121,12 +154,12 @@ public class SearchLikeServiceImpl implements ISearchLikeService { |
|
|
sortBuilders.add(SortBuilders.fieldSort("_score").order(SortOrder.DESC));
|
|
|
searchParam.setSortBuilders(sortBuilders);
|
|
|
|
|
|
// 7、设置分页c参数
|
|
|
// 7、设置分页参数
|
|
|
searchParam.setPage(page);
|
|
|
searchParam.setOffset((page - 1) * pageSize);
|
|
|
searchParam.setSize(pageSize);
|
|
|
|
|
|
//8、先从缓存获取,获取到直接返回
|
|
|
|
|
|
// 8、先从缓存获取,获取到直接返回
|
|
|
String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
CacheEnum cacheEnum = CacheEnum.EHCACHE;
|
|
|
JSONObject dataMap = searchCacheService.getJSONObjectFromCache(cacheEnum, productIndexName, searchParam);
|
...
|
...
|
@@ -151,7 +184,7 @@ public class SearchLikeServiceImpl implements ISearchLikeService { |
|
|
searchCacheService.addJSONObjectToCache(cacheEnum, productIndexName, searchParam, dataMap);
|
|
|
return searchApiResult.setData(dataMap);
|
|
|
}
|
|
|
|
|
|
|
|
|
private long getPageTotal(long total, int pageSize) {
|
|
|
if (pageSize <= 0) {
|
|
|
return total;
|
...
|
...
|
|