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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse.AnalyzeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
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 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.PersonalVectorFeatureSearch;
import com.yoho.search.service.helper.SearchKeyWordHelper;
import com.yoho.search.service.service.IProductListService;

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

    @Autowired
    private SearchKeyWordHelper searchKeyWordService;
    @Autowired
    private PersonalVectorFeatureSearch personalVectorFeatureSearch;
    @Autowired
    private IProductListService productListService;

    @Autowired
    private SearchDynamicConfigService searchDynamicConfigService;

    /**
     * 获取热搜词结果
     *
     * @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);
    }

    @RequestMapping(value = "/dynamicParameterValue")
    @ResponseBody
    public Map<String, Object> dynamicParameterValue(String key) {
        Map<String, Object> rtnMap = new HashMap<String, Object>();

        try {
            Assert.notNull(key);
            rtnMap.put("code", 200);
            rtnMap.put("value", searchDynamicConfigService.getDynamicParameterValue(key));
        } catch (Exception e) {
            rtnMap.put("code", 400);
            rtnMap.put("msg", e.getMessage());
        }

        return rtnMap;
    }

    @SuppressWarnings("unchecked")
	@RequestMapping(method = RequestMethod.GET, value = "/calVectorFeature")
    @ResponseBody
    public SearchApiResult calVectorFeature(@RequestParam String uid, @RequestParam String skns, @RequestParam String version) throws Exception {
        SearchApiResult productListResult = productListService.productListBySknList(skns);
        if (productListResult.getCode() != 200) {
            return productListResult;
        }
        Map<String, String> productVectorFeatureMap = (Map<String, String>) productListResult.getData();
        return new SearchApiResult().setData(personalVectorFeatureSearch.calVectorFeature(uid, version, productVectorFeatureMap));
    }


    /**
     * 用于动态地变更日记级别。
     * @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());
        }
    }
}