UpgradeSearchConsumerController.java 2.15 KB
package com.yoho.search.consumer.test;

import com.yoho.search.consumer.job.SuggestionJob;
import com.yoho.search.consumer.service.base.SuggestWordDefService;
import com.yoho.search.spider.job.SpiderJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ginozhang on 2017/3/10.
 */
@Controller
public class UpgradeSearchConsumerController {

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

    @Autowired
    private SuggestWordDefService suggestWordDefService;
    @Autowired
    private SuggestionJob suggestionJob;
    @Autowired
    private SpiderJob spiderJob;

    @RequestMapping(value = "/proteced/upgrade")
    @ResponseBody
    public Map<String, Object> upgrade() {
        Map<String, Object> rtnMap = new HashMap<String, Object>();
        try {
            doUpgrade();
            rtnMap.put("code", 200);
            rtnMap.put("msg", "success");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            rtnMap.put("code", 500);
            rtnMap.put("msg", e.getMessage());
        }
        return rtnMap;
    }

    private void doUpgrade() {
        // Important NOTE: 本升级restful 接口为2017.03.13升级所写 升级后可删除
        long begin = System.currentTimeMillis();
        logger.info("[func=doUpgrade][begin={}]", begin);

        // 1. 清除suggest_word_def表
        suggestWordDefService.deleteAllValidKeywords();
        logger.info("[func=doUpgrade][msg=deleteAllValidKeywords succeed]");

        // 2. 执行SuggestionDiscovery/SuggestionCounter/SuggestIndexRebuilder
        suggestionJob.executeAll();

        // 3. 执行SuggestConvertor/conversion索引的重建
        spiderJob.convertAndRebuildIndex();
        logger.info("[func=doUpgrade][msg=convertAndRebuildIndex succeed][cost={}]", System.currentTimeMillis() - begin);
    }

}