封裝xunsearch類

weixin_34402408發表於2016-09-21

封閉xunsearch方便其進行操作

require_once WEB_ROOT . '/lib/xs/php/lib/XS.class.php';
class Myxs {

    private $xs;
    private $scws;
    private $doc;
    private $index;
    private $search;
    private $_sort;
    private static $myxs = [];


    private function __construct($pro) {
        
        $this->xs = new XS($pro);
        $this->doc = new XSDocument();
        $this->index = $this->xs->index;
        $this->search = $this->xs->search;
        $this->scws = new XSTokenizerSCWS;
    }

    

    public function getsearch() {
        return $this->search;
    }

    public function getindex() {
        return $this->index;
    }

    public function getScws() {
        return $this->scws;
    }



    public static function init($pro){
        if (!empty($myxs[$pro]) && self::$myxs[$pro] instanceof XS) {
            return self::$myxs;
        } else {
            self::$myxs[$pro] = new self($pro);
        }
        return self::$myxs[$pro];
    }

    public function limit ($limit, $offset = 0) {
        $this->search->setlimit($limit, $offset);
    }

    /**
     * 排序time:[0,1]0反序1正序
     * @param  [type] $str [description]
     * @return [type]      [description]
     */
    public function addsort($str) {
        $this->_sort = $str;
    }


    public function clean () {
        $this->index->clean();
    }

    /**
     * 新增索引
     * @param  [type]  $data   [description]
     * @param  boolean $update [description]
     * @return [type]          [description]
     */
    public function docadd($data,$update = true) {
        if (!is_array($data)) {
            return false;
        }
        //$doc = new XSDocument();
        $this->doc->setFields($data);

        if ($update) {
            $this->index->update($this->doc);

        } else {
            $this->index->add($this->doc);
        }


        $this->index->flushIndex();

        
    }
    /**
     * 刪除索引
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function docdel($data) {
        if (!is_array($data)) {
            return false;
        }

        $this->doc->setFields($data);
        $this->index->update();
    }

    /**
     * 重新整理索引
     * @param  string $doc [description]
     * @return [type]      [description]
     */
    public static function docflush($doc = 'all'){
        if ($doc == 'all'){
            foreach(self::$myxs as $k => $v) {
                try{
                    $v->index->flushIndex();
                }catch(Exception $e) {
                    echo $e;
                }
            }
        }else {
            try{
                self::$myxs[$doc]->index->flushIndex();
            }catch(Exception $e){
                echo $e;
            }
        }
        
    }



    public function getCount(){
        $this->docflush();
        return $this->search->getDbTotal();
    }
    /**
     * 以陣列形式搜尋
     * @param  [type] $arr [description]
     * @param  string $sep [description]
     * @return [type]      [description]
     */
    function getByKey ($key, $limit = 10, $page =1,$fuzzy = true, $sep = 'OR') {
        $page = max(1, $page);
        
        $this->search = $this->search->setSort("time", false);
        $this->search->setLimit($limit, ($page-1) * $limit);
        $this->search->setFuzzy($fuzzy); 
        if (is_array($key)) {
            $key = implode($sep, $key);
        }
        return $this->search->search($key);
    }
    /**
     * 獲取熱搜排序型別, 預設為 total(搜尋總量), 可選值還有 lastnum(上週), currnum(本週)
     * @param  integer $limit [description]
     * @param  string  $type  [description]
     * @return [type]         [description]
     */
    function getHotArc($type = "total", $arclimit = 10, $keylimit = 6) {
        $words = $this->search->getHotQuery($keylimit, $type);
        $words = array_keys($words);
        shuffle($words);
        return $this->getByKey($words, $arclimit);
    }

    /**
     * 提取重要詞,並返回由重要片語成的陣列
     * @param [type]  $text  [description]
     * @param integer $limit [description]
     * @param string  $attr  [description]
     */
    function scwsByText($text, $limit = 4, $attr = 'n') {
        $this->scws->setIgnore(true);
        $this->scws->setDuality(true)->setMulti(3);
        $results = $this->scws->getTops($text, $limit, $attr);
        $tmp = [];
        foreach ($results as $key => $value) {
            array_push($tmp, $value['word']);
        }
        return $tmp;
    }

    function getScwsAll ($str, $attr = []) {
        $source=["nr"=>4,"n"=>3,"v"=>2];
        $re = [];
        $this->scws->setIgnore(true);
        $this->scws->setDuality(true);
        
        $results = $this->scws->getResult($str);
        $kwd=[];
        foreach ($results as $k => $v) {
            $kwd[$v['word']]=strlen($v['word'])*(isset($source[$v['attr']])?$source[$v['attr']]:1);
        }
        arsort($kwd);



        return array_slice($kwd, 0,3);
    }


}

相關文章