Example #1
0
 public static function getFullPy($word)
 {
     $RET = '';
     if ($word) {
         $word = SearchUtil::EscapeKeyword($word);
         // 分词
         $url = sprintf('http://360xun.sinaapp.com/api/segment?words=%s', urlencode($word));
         $segWord = httpClient::curlRequset($url);
         // 中文单字
         $singleWordArr = self::SplitSingleChinese($word);
         $singleWord = implode(' ', $singleWordArr);
         // 中文拼音前缀
         $preWordArr = self::SplitChinese($word);
         $preWord = implode(' ', $preWordArr);
         $pySegWord = Pinyin::str2py($segWord);
         $pySingleWord = Pinyin::str2py($singleWord);
         $pyPreWord = Pinyin::str2py($preWord);
         $segArr = explode(' ', $segWord);
         $engArr = array();
         if ($segArr) {
             foreach ($segArr as $s) {
                 if (preg_match('/^[a-zA-Z]+$/is', $s)) {
                     if ($engs = self::SplitEnglish($s, 3)) {
                         $engArr = array_merge($engArr, $engs);
                     }
                 }
             }
         }
         $RET = $pySegWord . ' ' . $pySingleWord . ' ' . $pyPreWord;
         if ($engArr) {
             $RET = implode(' ', $engArr) . ' ' . $RET;
         }
         $RET_ARR = array_unique(explode(' ', $RET));
         $RET = implode(' ', $RET_ARR);
         $RET = trim($RET);
     }
     return $RET;
 }
Example #2
0
 public function search($keyword, $page = 1, $pageSize = 10, &$hasMore = false, &$cacheHit = false)
 {
     $RET = [];
     $searchRs = [];
     /*
             if($soft_type == self::APPSTORE) {
        $cache = Factory::Redis(ClusterRedis::CLUSTER_APP);
        list($keywordCacheKey,$expire) = $cache->GetStrategy()->GetAppSearchKey($keyword);
             } elseif($soft_type == self::GAME_CENTER) {
        $cache = Factory::Redis(ClusterRedis::CLUSTER_GAME_APP);
        list($keywordCacheKey,$expire) = $cache->GetStrategy()->GetAppSearchKey($keyword);
             }
     */
     /** get Cache 
         if(AppLogic::IsUseCache())
         {
             if($keywordCacheKey && ($res = $cache->get($keywordCacheKey)) && ($searchRs = Util::JsonDec($res, TRUE)))
             {
                 $cacheHit = TRUE;
             }
         }*/
     if (!isset($searchRs) || empty($searchRs)) {
         // 判断是否是包名
         //if( !$this->isPackage($keyword) ) {
         // 繁体转简体
         //$keyword = ChineseConvert::t2s($keyword);
         //$categoryId = CategoryLogic::GetCategoryMap($keyword);
         // 分词
         $url = sprintf('http://360xun.sinaapp.com/api/segment?words=%s', urlencode($keyword));
         $segWord = httpClient::curlRequset($url);
         $pySegWord = Pinyin::str2py($segWord);
         /*
                         // 如果分词结果不是字符串
                         if(!is_string($segWord)) {
            return false;
                         }
         */
         // 整词列表
         /*
                         if( $this->WholeWords($keyword) ) {
            $segWord .= '|('.$keyword.')';
                         }
         */
         // 分词结果转拼音
         //$pyWord = py_class::str2py($segWord);
         //Log::Single()->Info('Search Word', array('key'=>$keyword, 'seg'=>$segWord, 'py'=>$pyWord, 'p'=>$page, 'size'=>$pageSize));
         //$pyWord = $this->filterKeyword($pyWord);
         //} else {
         /*
                         $segWord = '';
                         $pyWord = '';
                         $package = $keyword;
                         Log::Single()->Info('Search package', array('key'=>$keyword, 'p'=>$page, 'size'=>$pageSize));
         */
         //}
         $this->search->setFilterRange('status', 1, 1);
         $this->search->setMatchMode(SPH_MATCH_EXTENDED2);
         $this->search->setSortMode(SPH_SORT_EXTENDED, '@relevance DESC, update_time DESC, rating DESC');
         $this->search->setFieldWeights($this->GetSearchWeightMap());
         $this->search->setMaxQueryTime(3000);
         $this->search->setRankingMode(SPH_RANK_SPH04);
         //$this->search->setRankingMode(SPH_RANK_EXPR, 'sum((exact_hit*5+(min_hit_pos==1)*2+lcs)*user_weight)*1000+bm25');
         $this->search->setLimits(0, 100, self::DEFAULT_SEARCH_TOTAL, self::DEFAULT_SEARCH_TOTAL);
         $this->search->setArrayResult(TRUE);
         $indexs = implode(',', $this->GetSearchIndex());
         $filterWordArr = $this->GetSearchFilterWord();
         $filterDeveloper = 0;
         foreach ($filterWordArr as $word) {
             if (strpos($keyword, $word) !== FALSE) {
                 $filterDeveloper = 1;
                 break;
             }
         }
         $seg_keyword = $segWord;
         $seg_keyword = array_filter(explode(' ', $seg_keyword));
         $seg_keyword = '(' . implode(')|(', $seg_keyword) . ')';
         //var_dump($seg_keyword);die;
         // addQuerys
         $this->search->addQuery("@title {$seg_keyword}", $indexs);
         $this->search->addQuery("@original_title {$seg_keyword}", $indexs);
         $this->search->addQuery("@aka {$seg_keyword}", $indexs);
         $this->search->addQuery("@directors {$seg_keyword}", $indexs);
         $this->search->addQuery("@casts {$seg_keyword}", $indexs);
         $this->search->addQuery("@year {$seg_keyword}", $indexs);
         $this->search->addQuery("@country {$seg_keyword}", $indexs);
         if ($pySegWord) {
             $py_keyword = $pySegWord;
             $py_keyword = array_filter(explode(' ', $py_keyword));
             $py_keyword = '(' . implode(')|(', $py_keyword) . ')';
             //$py_keyword = count($py_keyword)=1 ? '('.implode(')|(', $py_keyword).')' : $py_keyword;
             $this->search->addQuery("@py_title {$py_keyword}", $indexs);
             $this->search->addQuery("@py_original_title {$py_keyword}", $indexs);
             /*
                             $pyWordArr = explode('|', $pyWord);
                             foreach($pyWordArr as $pyName) {
                                 $this->search->addQuery("@py_name {$pyName}", $indexs);
                             }
                             $this->search->addQuery("@py_cname {$pyWord}", $indexs);
             */
         }
         $res = $this->search->runQueries();
         if (is_array($res)) {
             $error = '';
             $warning = '';
             $searchRs = array();
             foreach ($res as $k => $v) {
                 if (isset($v['error']) && $v['error']) {
                     $error .= $k . ':' . $v['error'] . ' & ';
                 }
                 if (isset($v['warning']) && $v['warning']) {
                     $warning .= $k . ':' . $v['warning'] . ' & ';
                 }
                 // matches
                 if (isset($v['matches']) && $v['matches']) {
                     foreach ($v['matches'] as $data) {
                         if (!array_key_exists($data['id'], $searchRs)) {
                             $searchRs[$data['id']] = $data['weight'];
                         } else {
                             $searchRs[$data['id']] += $data['weight'];
                         }
                     }
                 }
             }
             arsort($searchRs);
             /** 写Cache 
                 if($searchRs && $keywordCacheKey)
                 {
                     $cache->multi(Redis::MULTI);
                     $cache->set($keywordCacheKey, Util::JsonEnc($searchRs));
                     $expire && $cache->expire($keywordCacheKey, $expire);
                     $cache->exec();
                 }*/
             // log error
             if (!empty($error)) {
                 //Log::Single()->Error("Sphinx error", array('error'=>$error, 'keyword'=>$keyword, 'segWord'=>$segWord, 'py'=>$pyWord));
             }
             // log warning
             if (!empty($warning)) {
                 //Log::Single()->Warn("Sphinx warning", array('warning'=>$warning));
             }
         }
     }
     if (isset($searchRs) && $searchRs) {
         $page = $page <= 0 ? 1 : $page;
         $pageSize = $pageSize <= 0 ? 10 : $pageSize;
         $offset = ($page - 1) * $pageSize;
         $sliceSearchRs = array_slice($searchRs, $offset, $pageSize + 1, TRUE);
         $count = count($sliceSearchRs);
         $hasMore = $count >= $pageSize + 1 ? TRUE : FALSE;
         $idx = 0;
         foreach ($sliceSearchRs as $appId => $weight) {
             $RET[$appId] = $weight;
             $idx++;
             if ($idx >= $pageSize) {
                 break;
             }
         }
     }
     return $RET;
 }