BaseRecallService.java 3.87 KB
package com.yoho.search.recall.common;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.apache.commons.collections.MapUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.service.base.SearchCommonService;
import com.yoho.search.service.helper.SearchParamHelper;
import com.yoho.search.recall.common.model.CommonRecallResult;
import com.yoho.search.recall.common.model.CommonRecallSkn;

@Component
public class BaseRecallService {

	private static List<String> commonRecallFields = new ArrayList<String>();

	@Autowired
	protected SearchParamHelper searchParamHelper;
	@Autowired
	protected SearchCommonService searchCommonService;

	@PostConstruct
	void init() {
		commonRecallFields.add(ProductIndexEsField.productSkn);
		commonRecallFields.add(ProductIndexEsField.brandId);
		commonRecallFields.add(ProductIndexEsField.smallSortId);
		commonRecallFields.add(ProductIndexEsField.productFeatureFactor);
	}

	protected SearchParam buildSearchParam(Map<String, String> paramMap, BoolQueryBuilder mustFilter, List<SortBuilder<?>> sortBuilders, int size) throws Exception {
		// 1、构建filter
		SearchParam searchParam = searchParamHelper.buildWithMustFilter(paramMap, mustFilter);
		// 2、构建size
		searchParam.setOffset(0);
		searchParam.setSize(size);
		// 3、设置返回字段
		searchParam.setIncludeFields(commonRecallFields);
		// 4、设置orde
		searchParam.setSortBuilders(sortBuilders);
		return searchParam;
	}
	
	
	protected int getViewNum(Map<String,String> paramMap){
		return MapUtils.getIntValue(paramMap, "viewNum", 60);
	}

	/**
	 * 合并召回结果,默认最后一个包含了总数量
	 * 
	 * @param searchResults
	 * @return
	 */
	protected CommonRecallResult getCommonRecallResult(List<SearchResult> searchResults) {
		// 1、获取值
		List<CommonRecallSkn> productList = new ArrayList<CommonRecallSkn>();
		Set<Integer> existsProductSkn = new HashSet<Integer>();
		for (SearchResult searchResult : searchResults) {
			productList.addAll(this.getCommonRecallSknList(searchResult, existsProductSkn));
		}
		long totalCount = searchResults.get(searchResults.size() - 1).getTotal();
		// 2、构造返回结果
		CommonRecallResult commonRecallResults = new CommonRecallResult();
		commonRecallResults.setTotalCount(totalCount);
		commonRecallResults.setRecallSknList(productList);
		return commonRecallResults;
	}

	
	private List<CommonRecallSkn> getCommonRecallSknList(SearchResult searchResult, Set<Integer> existsProductSkn) {
		List<CommonRecallSkn> commonRecallSkns = new ArrayList<CommonRecallSkn>();
		List<Map<String, Object>> productList = searchResult.getResultList();
		for (Map<String, Object> product : productList) {
			Integer productSkn = MapUtils.getIntValue(product, ProductIndexEsField.productSkn, 0);
			if (existsProductSkn.contains(productSkn)) {
				continue;
			}
			existsProductSkn.add(productSkn);
			CommonRecallSkn commonRecallSkn = new CommonRecallSkn();
			commonRecallSkn.setProductSkn(productSkn);
			commonRecallSkn.setBrandId(MapUtils.getIntValue(product, ProductIndexEsField.brandId, 0));
			commonRecallSkn.setSmallSortId(MapUtils.getIntValue(product, ProductIndexEsField.smallSortId, 0));
			commonRecallSkn.setProductFeatureFactor(MapUtils.getString(product, ProductIndexEsField.productFeatureFactor, ""));
			commonRecallSkns.add(commonRecallSkn);
		}
		return commonRecallSkns;
	}

}