xunsearch使用记录

部署,配置,有时间在记录

<?php
namespace APPlib;
class XSGameku {
    public $error;
    public $xs;
    public $search;
    public $index;
    private $project='gameku';
    
    public function __construct(){
        if(!defined("XUNSEARCH_SDK_PATH")){
            $this->error = 'not define XUNSEARCH_SDK_PATH';
            return $this->error;
        }
        
        include XUNSEARCH_SDK_PATH . 'lib' . DIRECTORY_SEPARATOR . 'XS.php';

        if(!class_exists('XS')){
            $this->error = 'class XS not exist';
            return $this->error;
        }
        
        $this->xs = new XS($this->project); 
        $this->index = $this->xs->getIndex(); 
        $this->search = $this->xs->getSearch();
    }
    
    public function getSearch($where){
        $query = $data = $ids = array();$count = 0;

        if(isset($where['name']))   $query[]= "name:{$where['name']}";
        if(isset($where['online']))   $query[]= "online:{$where['online']}";
        if(isset($where['seq']))   $query[]= "seq:{$where['seq']}";
        if(isset($where['is_ios']))   $query[]= "is_ios:1";
        if(isset($where['is_android']))   $query[]= "is_android:1";
        if(isset($where['type_new']))   $query[]= "type_new:{$where['type_new']}";
        if(isset($where['theme_new']))   $query[]= "theme_new:{$where['theme_new']}";
        if(isset($where['price']))   $query[]= "price:{$where['price']}";
        if(!isset($where['orderby'])) $where['orderby']='id';
        
        if($query){
            $query = implode(' AND ',$query);
        }else{
            $query = '';
        }
        //echo $query;
        $this -> search -> setQuery($query);
        $querty = $this -> search -> getQuery();
        //echo ($querty);
        
        if(isset($where['star']) && $where['star'] != 0){
            //计算range--begin,end
            $begin = ($star -1) * 20 -1;
            $end = $star * 20;
            $this -> search -> addRange('star',$begin,$end);
        };
        
        
        switch($where['orderby']){
            case 'hot':
                $this -> search -> setMultiSort(array('hot'=>true,'id_num'=>false));
                $orderby_str = "hot ASC,id DESC";
                break;
            case 'zuixin':
                $this -> search -> setMultiSort(array('zuixin'=>true,'id_num'=>false));
                $orderby_str = "zuixin ASC,id DESC";
                break;
            default:
                $this -> search -> setSort('id_num');
                $orderby_str = "id DESC";
                break;
        }

        $page_start = ($where['pageno']-1) * $where['pagesize'];
        $this -> search -> setLimit($where['pagesize'], $page_start);
        
        $dataA = $this -> search -> search();
        if($dataA){
            foreach ($dataA as $key=>$doc){            
                $ids[] = $doc->id;
            }
            $GamkuModel = APPlibget_app_model('GamekuModel');
            if($where['field'] == 'www'){
                $field = 'id,name,icon,filesize,filesize_ios,type_new,price,star,islike,unlike';
            }else{
                $field = 'id,name,icon';
            }
            $data = $GamkuModel->field($field)->where('id in('.implode(',',$ids) .')')->order($orderby_str)->select();
        }
        
        //var_dump($data);
        $count = $this  -> search -> getLastCount();
        //var_dump($count);
        //exit;

        $max_pageno = ceil($count/$where['pagesize']);
        return array($data,$max_pageno,$count);
    
    }
    
    public function getSearchName($name=false,$pageno=1,$pagesize=20){
        $data=array();$count = 0;
        $this->search->setScwsMulti(0);
        if($name !== false){
            $query = "name:{$name}";
        }else{
            return false;
        }

        $this -> search -> setQuery($query);
        $page_start = ($pageno-1) * $pagesize;
        $this -> search -> setLimit($pagesize, $page_start);
        
        $dataA = $this -> search -> search();
        if($dataA){
            // foreach ($dataA as $key=>$doc){            
                // $data[$key]['id']   = $doc->id;
                // $data[$key]['name'] = $doc->name;
                // $data[$key]['icon'] = $doc->icon;
            // }

            foreach ($dataA as $key=>$doc){            
                $ids[] = $doc->id;
            }
            $GamkuModel = APPlibget_app_model('GamekuModel');
            $field = 'id,name,icon,filesize,filesize_ios,type_new,price,star';
            $data = $GamkuModel->field($field)->where('id in('.implode(',',$ids) .')')->select();

        }
        
        //var_dump($data);
        $count = $this  -> search -> getLastCount();
        //var_dump($count);
        //exit;

        $max_pageno = ceil($count/$pagesize);
        return array($data,$max_pageno,$count);
    
    }
    
    // public function getSearchXun($type,$wd,$platform,$class,$gtype,$orderby,$pageno,$pagesize = 4){
        // $this -> search -> setAutoSynonyms();
        //$this -> search -> setFuzzy();
        
        //$this->search->setScwsMulti(0);
        //$this->search->setScwsMulti(8);
        
        
        //header("Content-type: text/html; charset=utf-8");
        //$tokenizer = new XSTokenizerScws();
        
        //$tokenizer->addDict('/usr/local/xunsearch/data/gameku/dict.xdb', SCWS_XDICT_XDB);
        //$tokenizer->setMulti(SCWS_MULTI_ZALL);
        
        //$this -> index->setCustomDict('我');
        //var_dump($this -> index->getCustomDict());
        //$this -> index->setScwsMulti(SCWS_MULTI_ZALL);
        //var_dump($this -> index->getScwsMulti());
        //exit;
        
        // var_dump($tokenizer->hasWord('我','r'));
        // var_dump($tokenizer->hasWord('星','n'));
        // var_dump($tokenizer->hasWord('我叫','v'));
        // var_dump($tokenizer->hasWord('星球','n'));
        // var_dump($tokenizer->hasWord('涂他涂','n'));
        // print_R($tokenizer->getResult($name));exit;
        
        
        
        // $this -> search -> setAutoSynonyms();
        // //$this -> search -> setFuzzy();
        // //$wd = "$wd";
        // $this -> search -> setQuery($wd);
        // //$this -> search -> setQuery('name:武士 僵尸 大战');
        // $this -> search -> addRange('auth_status',2,2);
        // //header("Content-Type: text/html;Charset=UTF8;");
        // //$this -> search -> addRange('type',3,3);
        // //$this -> search -> addRange('platform',2,2);
        // //$this -> search -> setSort('dl_count_px',false);
        // // $this -> search -> setLimit(3, 0);
        // // $querty = $this  -> search -> getQuery();
        // //$dataA = $this  -> search -> search();
        // // //$count = $this  -> search -> getLastCount();
        // // var_dump($querty);
        // //$this -> search -> setSort('pub_time_px',false);
        // // $querty = $this  -> search -> getQuery();
        // //var_dump($dataA);exit;
        
        // $type != 0     ? $this -> search -> addRange('type',$type,$type) : false;
        // $platform != 0 ? $this -> search -> addRange('platform',$platform,$platform) : false;
        // $class != 0    ? $this -> search -> addRange('class',$class,$class) : false;
        // $gtype != 0    ? $this -> search -> addRange('gtype_new',$gtype,$gtype) : false;

        // switch($orderby){
            // case 1 : $this -> search -> setSort('pub_time_px',false);break;
            // case 2 : $this -> search -> setSort('dl_count_px',false);break;
            // case 3 : $this -> search -> setSort('star_px',false);break;
            // default : break;
        // }

        // $page_start = ($pageno-1) * $pagesize;
        // $this -> search -> setLimit($pagesize, $page_start);

        // $querty = $this  -> search -> getQuery();
        // $dataA = $this  -> search -> search();
        // $count = $this  -> search -> getLastCount();
        // //var_dump($querty);
        // $data =array();
        // if($count>0){
            // foreach ($dataA as $key=>$doc){            
                // $data[$key]['id'] = $doc->id;
                // $data[$key]['name'] = $doc->name;
                // $data[$key]['platform']=$doc->platform;
                // $data[$key]['icon'] = $doc->icon;
                // $data[$key]['mark'] = $doc->mark;
                // $data[$key]['star'] = $doc->star;
                // $data[$key]['dl_count'] = $doc->dl_count;
                // $data[$key]['pub_time'] = $doc->pub_time;
                // $data[$key]['comment_id'] = $doc->comment_id;
            // }    
        // }
        // $max_pageno = ceil($count/$pagesize);
        // return array($data,$max_pageno,$count);
    
    // }
    
    public function dataIn($id){
        if(empty($id) || $id < 0){
            return false;
        }
        $GamekuModel = APPlibget_app_model('GamekuModel');
        $data = $GamekuModel->field('id,name,name_old,online,seq,is_ios,is_android,type_new,theme_new,price,star,star,hot,zuixin,status')->where("id=$id")->find();
        if($data && $data['status'] == 2){
            //创建文档对象。
            $dataXun['id'] = $dataXun['id_num'] = $data['id'];
            $dataXun['name'] = $data['name'];
            $dataXun['name_old'] = $data['name_old'];
            $dataXun['online'] = $data['online'];
            $dataXun['seq'] = $data['seq'];
            $dataXun['is_ios'] = $data['is_ios'];
            $dataXun['is_android'] = $data['is_android'];
            $dataXun['type_new'] = $data['type_new'];
            $dataXun['theme_new'] = $data['theme_new'];
            $dataXun['price'] = $data['price'];
            $dataXun['star'] = $data['star'];
            $dataXun['hot'] = $data['hot'];
            $dataXun['zuixin'] = $data['zuixin'];

            //这里优化为队列
            $doc = new XSDocument;
            $doc->setFields($dataXun);
            $this->index->update($doc);
            $this->index->flushIndex();
            return true;
        }
    }

    public function deleteIndex($idArray){            
        $this->index->del($idArray); 
        //$this->index->flushIndex();
    }

    public function cleanIndex(){        
        $this->index->clean();
    }
}
原文地址:https://www.cnblogs.com/kudosharry/p/4629798.html