UfoResultController.java 2.9 KB
package com.yoho.search.restapi.tools;

import com.yoho.search.base.constants.ISearchConstants;
import com.yoho.search.base.constants.UfoProductIndexEsField;
import com.yoho.search.common.BaseSceneService;
import com.yoho.search.common.SearchCommonService;
import com.yoho.search.common.UfoSearchRequestParams;
import com.yoho.search.common.utils.HttpServletRequestUtils;
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.service.scene.others.explain.SearchExplainerService;
import com.yoho.search.service.scene.others.explain.UfoSearchExplainerService;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping(value = "/tools")
public class UfoResultController extends BaseSceneService {

    @Autowired
    private SearchCommonService searchCommonService;
    @Autowired
    private UfoSearchExplainerService ufoSearchExplainerService;
    /**
     * 根据ufo code查询商品索引信息
     */
    @RequestMapping(method = RequestMethod.GET, value = "/queryUfoSknInfo")
    public SearchApiResult queryUfoSknInfo(HttpServletRequest request) {
        Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
        if (StringUtils.isBlank(paramMap.get(UfoSearchRequestParams.UFO_PARAM_PRODUCT_CODE))) {
            return new SearchApiResult().setCode(400).setMessage("缺少skn");
        }
        SearchParam searchParam = new SearchParam();
        searchParam.setSize(1);
        searchParam.setQuery(QueryBuilders.matchQuery(UfoProductIndexEsField.productCode, paramMap.get(UfoSearchRequestParams.UFO_PARAM_PRODUCT_CODE)));
        SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_UFO_PRODUCT_INDEX, searchParam);
        if (searchResult != null && searchResult.getResultList() != null && !searchResult.getResultList().isEmpty()) {
            return new SearchApiResult().setData(searchResult.getResultList().get(0));
        }
        return new SearchApiResult().setData(null);
    }

    /**
     * 显示ufo分词结果
     */
    @RequestMapping(value = "/ufoShow")
    public Map<String, Object> ufoShow(HttpServletRequest request) {
        Map<String, String> paramMap = com.yoho.search.base.utils.HttpServletRequestUtils.transParamType(request);
        try {
            return ufoSearchExplainerService.ufoShow(paramMap);
        } catch (Throwable t) {
            return new HashMap<>();
        }
    }
}