BigdataSimilarSknIndexBaseService.java 2.38 KB
package com.yoho.search.service.index;

import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.bo.BigdataSimilarSknIndexBO;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.dal.model.BigdataSimilarSkn;
import com.yoho.search.common.SearchCommonService;
import org.apache.commons.beanutils.BeanUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

@Service
public class BigdataSimilarSknIndexBaseService {

    private static final Logger logger = LoggerFactory.getLogger(BigdataSimilarSknIndexBaseService.class);

    @Autowired
    private SearchCommonService searchCommonService;

    public BigdataSimilarSknIndexBO querySimilarSkn(String productSkn) {
        try {
            SearchParam searchParam = new SearchParam();
            BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
            boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, productSkn));
            searchParam.setFiter(boolFilter);
            searchParam.setAggregationBuilders(null);
            searchParam.setSize(1);

            String productIndexName = ISearchConstants.INDEX_NAME_BIGDATA_SIMILAR_SKN_INDEX;
            SearchResult searchResult = searchCommonService.doSearch(productIndexName, searchParam);
            if (searchResult == null) {
                return null;
            }
            List<Map<String, Object>> similarSknResults = searchResult.getResultList();
            if (CollectionUtils.isEmpty(similarSknResults) || similarSknResults.get(0) == null) {
                return null;
            }
            Map<String, Object> result =  similarSknResults.get(0);
            BigdataSimilarSknIndexBO bigdataSimilarSkn = new BigdataSimilarSknIndexBO();
            BeanUtils.populate(bigdataSimilarSkn, result);
            return bigdataSimilarSkn;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return null;
        }
    }

}