/** * * @param string $query * @param string $index * @param string $comment * @param Oray_Search_Sphinx_Optios $option * @return Oray_Search_Result */ public function query($query, Oray_Search_Sphinx_Option $option, $index = "*", $comment = "") { $this->_mbPush(); $request = $option->buildSearchRequest($query, $index, $comment); if (null == $this->_getSocket()) { $this->_MBPop(); require_once 'Oray/Search/Sphinx/Exception.php'; throw new Oray_Search_Sphinx_Exception('Connection error may be timeout'); } // add Header $length = strlen($request) + 4; $request = pack("nnNN", self::COMMAND_SEARCH, self::COMMAND_VER_SEARCH, $length, 1) . $request; if (!$this->_send($request, $length + 8) || !($response = $this->_getResponse(self::COMMAND_VER_SEARCH))) { $this->_mbPop(); require_once 'Oray/Search/Sphinx/Exception.php'; throw new Oray_Search_Sphinx_Exception('Search query failed maybe server is busy or invalid request'); } $ret = $this->_praseResponse($response); $this->_mbPop(); return $ret; }
/** * 记录搜索 * */ public function searchAction() { $key = trim($this->_request->getQuery('keyword')); $target = trim($this->_request->getQuery('target')); $range = $this->_request->getQuery('range'); $page = max(1, (int) $this->_request->getQuery('page')); $fromUrl = $this->_request->getQuery('fromurl'); $pageSize = 20; $timeRange = null; if (!empty($range)) { switch ($range) { case 'week': $timeRange = array(strtotime('-7 days'), time()); break; case 'month': $timeRange = array(strtotime('-30 days'), time()); break; case '3month': $timeRange = array(strtotime('-90 days'), time()); break; } } $params = array('keyword' => $key, 'target' => $target, 'range' => $range, 'fromUrl' => $fromUrl); $config = $this->options['sphinx']; $sphinx = new Oray_Search_Sphinx_Client($config); $option = new Oray_Search_Sphinx_Option(array('sortby' => 'create_time', 'mode' => Oray_Search_Sphinx_Option::MATCH_EXTENDED2, 'offset' => ($page - 1) * $pageSize)); $arr = preg_split('/[\\s\\+\\/]+/', $key); foreach ($arr as $i => $word) { if (!$word) { unset($arr[$i]); } $arr[$i] = $sphinx->escapeString($word); } $keyword = implode('|', $arr); $keyword = "@content \"{$keyword}\"/1"; if ($target) { if ($target == '^user') { $email = str_replace('@', '\\@', $this->_user->userName); $keyword = "@users {$email} {$keyword}"; } elseif ($target == '^groups') { $groupId = trim($this->_request->getQuery('groupid')); if ($groupId) { // 获取加入时间 $inTime = 0; $daoGroup = $this->getImDao('Dao_Im_Group_Group'); $member = $daoGroup->getGroupMember($groupId, $this->_user->userName); if ($member) { $inTime = strtotime($member['createtime']); if (null == $timeRange) { $timeRange = array(0 => 0, 1 => time()); } if (empty($timeRange[0]) || $timeRange[0] < $inTime) { $timeRange[0] = $inTime; } $gid = str_replace('-', '', str_replace('@', '\\@', $groupId)); $keyword = "@groupid {$gid} {$keyword}"; } else { $keyword = null; } $params['groupid'] = $groupId; } } else { $otherId = $this->_request->getQuery('otherid'); $otherId = str_replace('@', '\\@', $otherId); $email = str_replace('@', '\\@', $this->_user->userName); $keyword = "@users {$email}+{$otherId}|{$otherId}+{$email} {$keyword}"; $params['otherid'] = $otherId; } } $logs = array(); $total = 0; $keys = array(); $w = strtolower($w); if ($keyword != null) { if (!empty($timeRange)) { $option->addRangeFilter('create_time', $timeRange[0], $timeRange[1]); } $result = $sphinx->query($keyword, $option, $config['indexes']['chat'] . ' ' . $config['indexes']['chatinc']); $matches = $result->getMatches(); $total = $result->getTotal(); $words = $result->getWords(); foreach ($words as $w => $info) { if ($w && $info['docs'] > 0 && false !== strpos(strtolower($key), $w)) { $keys[] = $w; } } if (count($matches)) { $logNums = array(); foreach ($matches as $match) { $logNums[] = $match['id']; } /* @var $daoChatLog Dao_Im_Chat_Log */ $daoChatLog = $this->getImDao('Dao_Im_Chat_Log'); $condition = array('ordernum' => $logNums); if ($target == '^groups') { $condition['groupid'] = $groupId; } else { $condition['ownerid'] = $this->_user->userName; } $logs = $daoChatLog->getLogs($condition)->toArray(); } } $this->view->back = $this->_request->getQuery('back'); $this->view->logs = $logs; $this->view->pageinfo = array('currpage' => $page, 'pagecount' => intval(($total - 1) / $pageSize + 1), 'recordcount' => $total); $this->view->keys = implode("','", $keys); $this->view->query = $params; $this->view->keyword = $keys; $this->view->type = $target == '^groups' ? 'group' : 'user'; }