ToolsController.java 8.6 KB
package com.yoho.search.restapi.tools;

import com.yoho.search.common.utils.HttpServletRequestUtils;
import com.yoho.search.common.utils.LogUtils;
import com.yoho.search.models.KeyWordWithCount;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.base.SearchDynamicConfigService;
import com.yoho.search.service.helper.SearchKeyWordHelper;
import com.yoho.search.service.scene.UserVectorSortBrandService;
import com.yoho.search.service.scorer.personal.PersonalVectorFeatureSearch;
import com.yoho.search.service.service.IProductListService;
import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse.AnalyzeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/tools")
public class ToolsController {

    @Autowired
    private SearchKeyWordHelper searchKeyWordService;
    @Autowired
    private SearchDynamicConfigService searchDynamicConfigService;
    @Autowired
    private UserVectorSortBrandService userVectorSortBrandService;

    /**
     * 获取热搜词结果
     *
     * @return
     */
    @SuppressWarnings("unchecked")
	@RequestMapping(method = RequestMethod.GET, value = "/hotSearchWords")
    @ResponseBody
    public SearchApiResult hotSearchWords(@RequestParam(defaultValue = "1000") int limit,
                                          @RequestParam(defaultValue = "false") boolean onlyShowKeyWord,
                                          @RequestParam(defaultValue = "") String dateStr) {
        SearchApiResult searchApiResult = new SearchApiResult();
        Map<String, Object> results = searchKeyWordService.getHotkeyWords(limit, dateStr);
        if (!onlyShowKeyWord) {
            return searchApiResult.setData(results);
        }

        List<String> keywords = new ArrayList<>();
        for (KeyWordWithCount keyWordWithCount : (List<KeyWordWithCount>) results.get("keywords")) {
            keywords.add(keyWordWithCount.getKeyWord());
        }
        return searchApiResult.setData(keywords);
    }

    /**
     * 获取空结果搜索词
     *
     * @return
     */
    @SuppressWarnings("unchecked")
	@RequestMapping(method = RequestMethod.GET, value = "/emptyResultKeywords")
    @ResponseBody
    public SearchApiResult emptyResultKeywords(@RequestParam(defaultValue = "1000") int limit,
                                               @RequestParam(defaultValue = "false") boolean onlyShowKeyWord,
                                               @RequestParam(defaultValue = "") String dateStr) {
        SearchApiResult searchApiResult = new SearchApiResult();
        Map<String, Object> results = searchKeyWordService.getEmptyKeyWords(limit, dateStr);
        if (!onlyShowKeyWord) {
            return searchApiResult.setData(results);
        }

        List<String> keywords = new ArrayList<>();
        for (KeyWordWithCount keyWordWithCount : (List<KeyWordWithCount>) results.get("keywords")) {
            keywords.add(keyWordWithCount.getKeyWord());
        }
        return searchApiResult.setData(keywords);
    }

    /**
     * 获取一页搜索词
     *
     * @return
     */
    @SuppressWarnings("unchecked")
	@RequestMapping(method = RequestMethod.GET, value = "/lessKeyWords")
    @ResponseBody
    public SearchApiResult lessKeyWords(@RequestParam(defaultValue = "1000") int limit,
                                        @RequestParam(defaultValue = "false") boolean onlyShowKeyWord,
                                        @RequestParam(defaultValue = "") String dateStr) {
        SearchApiResult searchApiResult = new SearchApiResult();
        Map<String, Object> results = searchKeyWordService.getLessKeyWords(limit, dateStr);
        if (!onlyShowKeyWord) {
            return searchApiResult.setData(results);
        }

        List<String> keywords = new ArrayList<>();
        for (KeyWordWithCount keyWordWithCount : (List<KeyWordWithCount>) results.get("keywords")) {
            keywords.add(keyWordWithCount.getKeyWord());
        }
        return searchApiResult.setData(keywords);
    }

    /**
     * 获取需要推荐的关键词
     *
     * @return
     */
    @SuppressWarnings("unchecked")
	@RequestMapping(method = RequestMethod.GET, value = "/recomKeyWords")
    @ResponseBody
    public SearchApiResult recomKeyWords(@RequestParam(defaultValue = "1000") int limit,
                                        @RequestParam(defaultValue = "false") boolean onlyShowKeyWord,
                                        @RequestParam(defaultValue = "") String dateStr) {
        SearchApiResult searchApiResult = new SearchApiResult();
        Map<String, Object> results = searchKeyWordService.getNeedRecomKeyWords(limit, dateStr);
        if (!onlyShowKeyWord) {
            return searchApiResult.setData(results);
        }

        List<String> keywords = new ArrayList<>();
        for (KeyWordWithCount keyWordWithCount : (List<KeyWordWithCount>) results.get("keywords")) {
            keywords.add(keyWordWithCount.getKeyWord());
        }
        return searchApiResult.setData(keywords);
    }

    /**
     * 删除redis上的key
     *
     * @param redisKey
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/deleteRedis")
    @ResponseBody
    public SearchApiResult deleteRedisKey(@RequestParam String redisKey) {
        SearchApiResult searchApiResult = new SearchApiResult();
        return searchApiResult.setData(searchKeyWordService.deleteRedisKey(redisKey));
    }

    /**
     * 获取分词结果
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/analyzeTokens")
    @ResponseBody
    public SearchApiResult onePageKeywords(@RequestParam(defaultValue = "") String text, @RequestParam(defaultValue = "ik_complex") String analyzer) {
        List<AnalyzeToken> analyzeTokens = searchKeyWordService.getAnalyzeTokens(text, analyzer);
        SearchApiResult searchApiResult = new SearchApiResult();
        return searchApiResult.setData(analyzeTokens);
    }

    /**
     * 获取zk上动态配置的结果
     * @param key
     * @return
     */
    @RequestMapping(value = "/dynamicParameterValue")
    @ResponseBody
    public Map<String, Object> dynamicParameterValue(String key) {
        Map<String, Object> rtnMap = new HashMap<String, Object>();
        try {
            rtnMap.put("code", 200);
            rtnMap.put("value", searchDynamicConfigService.getDynamicParameterValue(key));
        } catch (Exception e) {
            rtnMap.put("code", 400);
            rtnMap.put("msg", e.getMessage());
        }
        return rtnMap;
    }

    /**
     * 用于动态地变更日记级别。
     * @param loggerName 日志名称,有三类:
     *                   第一种是ROOT,对所有日志都起作用,
     *                   第二种是具体的日志名称,如CACHE_MATCH_REQUEST,
     *                   第三种就是全限定类名,如com.yoho.search.service.servicenew.impl.ProductListServiceImpl。
     * @param level  调整目标级别,支持ALL/TRACE/DEBUG/INFO/WARN/ERROR/OFF这些取值
     * @param seconds 调整日志级别的有效时间,默认为60秒,最多只能为300秒,到了时间后变更为原来的日志级别。
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/changeLogLevel")
    @ResponseBody
    public SearchApiResult changeLogLevel(@RequestParam(defaultValue = "ROOT") String loggerName,
                                          @RequestParam(defaultValue = "INFO") String level,
                                          @RequestParam(defaultValue = "60") int seconds) {
        try {
            return new SearchApiResult().setData(LogUtils.changeLogLevel(loggerName, level, seconds));
        } catch (Exception e) {
            return new SearchApiResult().setCode(500).setMessage(e.getMessage());
        }
    }

    /**
     * 获取用户根据向量预测出来的品类品牌
     * @param request
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/userVectorSortBrand")
    @ResponseBody
    public SearchApiResult userVectorSortBrand(HttpServletRequest request) {
        Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
        return userVectorSortBrandService.userVectorSortBrand(paramMap);
    }
}