/** * Sphinx server connector initialization * * @return SphinxClient */ function init_sphinx() { $this->sphinx = new SphinxClient(); $this->sphinx->SetServer($this->admin_options['sphinx_host'], intval($this->admin_options['sphinx_port'])); $this->sphinx->SetMatchMode(SPH_MATCH_EXTENDED2); return $this->sphinx; }
/** * Initialize the Sphinx search engine * @return void */ public function initialize() { $this->_connectionOptions = array('host_name' => $this->modx->getOption('discuss.sphinx.host_name', null, 'localhost'), 'port' => $this->modx->getOption('discuss.sphinx.port', null, 9312), 'connection_timeout' => $this->modx->getOption('discuss.sphinx.connection_timeout', null, 30), 'searchd_retries' => $this->modx->getOption('discuss.sphinx.searchd_retries', null, 3), 'searchd_retry_delay' => $this->modx->getOption('discuss.sphinx.searchd_retry_delay', null, 10000)); $this->_indices = $this->modx->getOption('discuss.sphinx.indexes', null, 'discuss_posts'); $this->client = new SphinxClient(); $this->client->SetServer($this->_connectionOptions['host_name'], $this->_connectionOptions['port']); $this->client->SetConnectTimeout($this->_connectionOptions['connection_timeout']); $this->client->SetMatchMode(SPH_MATCH_EXTENDED); return true; }
/** * 初始化搜索引擎 */ private function _initSphinx() { $this->_loadCore('Help_SphinxClient'); $this->_sphinx = new SphinxClient(); $this->_sphinx->SetServer(SPHINX_HOST, SPHINX_PORT); $this->_sphinx->SetConnectTimeout(5); //连接时间 $this->_sphinx->SetArrayResult(true); $this->_sphinx->SetMaxQueryTime(10); //设置最大超时时间 $this->_sphinx->SetMatchMode(SPH_MATCH_ANY); //匹配模式 }
/** * 初始化搜索引擎 */ private function _initSphinx() { import('@.Util.SphinxClient'); $this->_sphinx = new SphinxClient(); $this->_sphinx->SetServer(C('SPHINX_HOST'), C('SPHINX_PORT')); $this->_sphinx->SetConnectTimeout(5); //连接时间 $this->_sphinx->SetArrayResult(true); //设置数组返回 $this->_sphinx->SetMaxQueryTime(10); //设置最大超时时间 $this->_sphinx->SetMatchMode(SPH_MATCH_ANY); //匹配模式 }
private function get_sugg_trigrams($word, SearchEngineOptions $options) { $trigrams = $this->BuildTrigrams($word); $query = "\"{$trigrams}\"/1"; $len = strlen($word); $this->resetSphinx(); $this->suggestionClient->SetMatchMode(SPH_MATCH_EXTENDED2); $this->suggestionClient->SetRankingMode(SPH_RANK_WORDCOUNT); $this->suggestionClient->SetFilterRange("len", $len - 2, $len + 4); $this->suggestionClient->SetSortMode(SPH_SORT_EXTENDED, "@weight DESC"); $this->suggestionClient->SetLimits(0, 10); $indexes = []; foreach ($options->getDataboxes() as $databox) { $indexes[] = 'suggest' . $this->CRCdatabox($databox); } $index = implode(',', $indexes); $res = $this->suggestionClient->Query($query, $index); if ($this->suggestionClient->Status() === false) { return []; } if (!$res || !isset($res["matches"])) { return []; } $words = []; foreach ($res["matches"] as $match) { $words[] = $match['attrs']['keyword']; } return $words; }
/** * * @param string $query * @return array of integers - taskIds */ public static function searchTasks($query) { $fieldWeights = array('description' => 10, 'note' => 6); $indexName = 'plancake_tasks'; $client = new SphinxClient(); // $client->SetServer (sfConfig::get('app_sphinx_host'), sfConfig::get('app_sphinx_port')); $client->SetFilter("author_id", array(PcUserPeer::getLoggedInUser()->getId())); $client->SetConnectTimeout(1); $client->SetMatchMode(SPH_MATCH_ANY); $client->SetSortMode(SPH_SORT_RELEVANCE); $client->SetRankingMode(SPH_RANK_PROXIMITY_BM25); $client->SetArrayResult(true); $client->SetFieldWeights($fieldWeights); $client->setLimits(0, 100); $results = $client->query($client->EscapeString($query), $indexName); if ($results === false) { $error = "Sphinx Error - " . $client->GetLastError(); sfErrorNotifier::alert($error); } $ids = array(); if (isset($results['matches']) && count($results['matches'])) { foreach ($results['matches'] as $match) { $ids[] = $match['id']; } } return PcTaskPeer::retrieveByPKs($ids); }
function hook_search($search) { $offset = 0; $limit = 500; $sphinxClient = new SphinxClient(); $sphinxpair = explode(":", SPHINX_SERVER, 2); $sphinxClient->SetServer($sphinxpair[0], (int) $sphinxpair[1]); $sphinxClient->SetConnectTimeout(1); $sphinxClient->SetFieldWeights(array('title' => 70, 'content' => 30, 'feed_title' => 20)); $sphinxClient->SetMatchMode(SPH_MATCH_EXTENDED2); $sphinxClient->SetRankingMode(SPH_RANK_PROXIMITY_BM25); $sphinxClient->SetLimits($offset, $limit, 1000); $sphinxClient->SetArrayResult(false); $sphinxClient->SetFilter('owner_uid', array($_SESSION['uid'])); $result = $sphinxClient->Query($search, SPHINX_INDEX); $ids = array(); if (is_array($result['matches'])) { foreach (array_keys($result['matches']) as $int_id) { $ref_id = $result['matches'][$int_id]['attrs']['ref_id']; array_push($ids, $ref_id); } } $ids = join(",", $ids); if ($ids) { return array("ref_id IN ({$ids})", array()); } else { return array("ref_id = -1", array()); } }
/** * Performs a search on an author's posts without caring about message contents. Depends on display specific params * * @param string $type contains either posts or topics depending on what should be searched for * @param boolean $firstpost_only if true, only topic starting posts will be considered * @param array $sort_by_sql contains SQL code for the ORDER BY part of a query * @param string $sort_key is the key of $sort_by_sql for the selected sorting * @param string $sort_dir is either a or d representing ASC and DESC * @param string $sort_days specifies the maximum amount of days a post may be old * @param array $ex_fid_ary specifies an array of forum ids which should not be searched * @param string $post_visibility specifies which types of posts the user can view in which forums * @param int $topic_id is set to 0 or a topic id, if it is not 0 then only posts in this topic should be searched * @param array $author_ary an array of author ids * @param string $author_name specifies the author match, when ANONYMOUS is also a search-match * @param array &$id_ary passed by reference, to be filled with ids for the page specified by $start and $per_page, should be ordered * @param int $start indicates the first index of the page * @param int $per_page number of ids each page is supposed to contain * @return boolean|int total number of results */ public function author_search($type, $firstpost_only, $sort_by_sql, $sort_key, $sort_dir, $sort_days, $ex_fid_ary, $post_visibility, $topic_id, $author_ary, $author_name, &$id_ary, $start, $per_page) { $this->search_query = ''; $this->sphinx->SetMatchMode(SPH_MATCH_FULLSCAN); $fields = $firstpost_only ? 'firstpost' : 'all'; $terms = 'all'; return $this->keyword_search($type, $fields, $terms, $sort_by_sql, $sort_key, $sort_dir, $sort_days, $ex_fid_ary, $post_visibility, $topic_id, $author_ary, $author_name, $id_ary, $start, $per_page); }
function sphinx_add_result_forum($items) { $inCore = cmsCore::getInstance(); global $_LANG; cmsCore::loadLanguage('components/forum'); $config = $inCore->loadComponentConfig('forum'); $search_model = cms_model_search::initModel(); foreach ($items as $id => $item) { if (!cmsCore::checkContentAccess($item['attrs']['access_list'])) { continue; } $pages = ceil($item['attrs']['post_count'] / $config['pp_thread']); $result_array = array( 'link' => '/forum/thread'. $id .'-'. $pages .'.html', 'place' => $item['attrs']['forum'], 'placelink' => '/forum/'. $item['attrs']['forum_id'], 'description' => $search_model->getProposalWithSearchWord($item['attrs']['description']), 'title' => $item['attrs']['title'], 'pubdate' => date('Y-m-d H:i:s', $item['attrs']['pubdate']) ); $search_model->addResult($result_array); } // Ищем в тексте постов $cl = new SphinxClient(); $cl->SetServer('127.0.0.1', 9312); $cl->SetMatchMode(SPH_MATCH_EXTENDED2); $cl->SetLimits(0, 100); $result = $cl->Query($search_model->against, $search_model->config['Sphinx_Search']['prefix'] .'_forum_posts'); if ($result !== false) { foreach ($result['matches'] as $id => $item) { $pages = ceil($item['attrs']['post_count'] / $config['pp_thread']); $post_page = ($pages > 1) ? postPage::getPage($item['attrs']['thread_id'], $id, $config['pp_thread']) : 1; $result_array = array( 'link' => '/forum/thread'. $item['attrs']['thread_id'] .'-'. $post_page .'.html#'. $id, 'place' => $_LANG['FORUM_POST'], 'placelink' => '/forum/thread'. $item['attrs']['thread_id'] .'-'. $post_page .'.html#'. $id, 'description' => $search_model->getProposalWithSearchWord($item['attrs']['content_html']), 'title' => $item['attrs']['thread'], 'imageurl' => $item['attrs']['fileurl'], 'pubdate' => date('Y-m-d H:i:s', $item['attrs']['pubdate']) ); $search_model->addResult($result_array); } } return; }
private function _getSphinxClient() { require_once SCRIPT_BASE . 'lib/sphinx-2.1.9/sphinxapi.php'; $sphinxClient = new SphinxClient(); $sphinxClient->SetServer('127.0.0.1', 9312); $sphinxClient->SetConnectTimeout(20); $sphinxClient->SetArrayResult(true); $sphinxClient->SetWeights(array(1000, 1)); $sphinxClient->SetMatchMode(SPH_MATCH_EXTENDED); return $sphinxClient; }
function query($query, $index, $offset = 0) { require_once DIR . "lib/sphinx/sphinxapi.php"; $sphinx = new SphinxClient(); $sphinx->setServer(SPHINX_HOST, SPHINX_PORT); $sphinx->SetLimits($offset, 100, 10000000); $sphinx->SetMatchMode(SPH_MATCH_EXTENDED); $sphinx->SetSortMode(SPH_SORT_ATTR_DESC, 'date_posted'); $res = $sphinx->Query($query, $index); return $res; }
/** * Непосредственно сам поиск * * @param string $sTerms Поисковый запрос * @param string $sObjType Тип поиска * @param int $iOffset Сдвиг элементов * @param int $iLimit Количество элементов * @param array $aExtraFilters Список фильтров * @return array */ public function FindContent($sTerms, $sObjType, $iOffset, $iLimit, $aExtraFilters) { /** * используем кеширование при поиске */ $sExtraFilters = serialize($aExtraFilters); $cacheKey = Config::Get('module.search.entity_prefix') . "searchResult_{$sObjType}_{$sTerms}_{$iOffset}_{$iLimit}_{$sExtraFilters}"; if (false === ($data = $this->Cache_Get($cacheKey))) { /** * Параметры поиска */ $this->oSphinx->SetMatchMode(SPH_MATCH_ALL); $this->oSphinx->SetLimits($iOffset, $iLimit); /** * Устанавливаем атрибуты поиска */ $this->oSphinx->ResetFilters(); if (!is_null($aExtraFilters)) { foreach ($aExtraFilters as $sAttribName => $sAttribValue) { $this->oSphinx->SetFilter($sAttribName, is_array($sAttribValue) ? $sAttribValue : array($sAttribValue)); } } /** * Ищем */ if (!is_array($data = $this->oSphinx->Query($sTerms, Config::Get('module.search.entity_prefix') . $sObjType . 'Index'))) { return FALSE; // Скорее всего недоступен демон searchd } /** * Если результатов нет, то и в кеш писать не стоит... * хотя тут момент спорный */ if ($data['total'] > 0) { $this->Cache_Set($data, $cacheKey, array(), 60 * 15); } } return $data; }
/** * __construct() * 构造函数 */ public function __construct() { parent::__construct(); parent::SetServer(SPH_HOST, 9312); //parent::SetServer("localhost",9312); //parent::SetConnectTimeout(10); parent::SetMatchMode(SPH_MATCH_EXTENDED2); //parent::SetFieldWeights($this->weights); parent::SetRankingMode(SPH_RANK_WORDCOUNT); // parent::SetSortMode(SPH_SORT_EXTENDED, '@weight desc'); parent::SetArrayResult(TRUE); $this->SetLimits($this->offset, $this->limit); }
protected function resetClient() { $this->sphinxClient->ResetFilters(); $this->sphinxClient->ResetGroupBy(); $this->sphinxClient->ResetOverrides(); $this->sphinxClient->SetLimits(0, 20); $this->sphinxClient->SetArrayResult(true); $this->sphinxClient->SetFieldWeights(array()); $this->sphinxClient->SetIDRange(0, 0); $this->sphinxClient->SetIndexWeights(array()); $this->sphinxClient->SetMatchMode(SPH_MATCH_EXTENDED2); $this->sphinxClient->SetRankingMode(SPH_RANK_NONE); $this->sphinxClient->SetSortMode(SPH_SORT_RELEVANCE, ""); $this->sphinxClient->SetSelect("*"); }
public function __construct($query) { $this->query = $query; $max_results = intval(SphinxSearch_Config_Plugin::getValue("results", "maxresults")); $this->limit = $max_results; $SphinxClient = new SphinxClient(); $this->SphinxClient = $SphinxClient; $SphinxClient->SetMatchMode(SPH_MATCH_EXTENDED2); $SphinxClient->SetSortMode(SPH_SORT_EXTENDED, "@weight DESC"); $SphinxClient->setServer("localhost", SphinxSearch_Config_Plugin::getValue("searchd", "port")); // Sphinx Client is to always return everything - it's just IDs // Paginator is then to cast the necessary Items, this can be done // with offset/limit $SphinxClient->setLimits(0, $max_results, $max_results); }
function sphinx_add_result_clubs($items) { global $_LANG; cmsCore::m('clubs'); $search_model = cms_model_search::initModel(); foreach ($items as $id => $item) { $result_array = array( 'link' => cmsCore::m('clubs')->getPostURL($item['attrs']['user_id'], $item['attrs']['seolink']), 'place' => ' «'. $item['attrs']['cat_title'] .'»', 'placelink' => cmsCore::m('clubs')->getBlogURL($item['attrs']['user_id']), 'description' => $search_model->getProposalWithSearchWord($item['attrs']['content_html']), 'title' => $item['attrs']['title'], 'imageurl' => $item['fileurl'], 'pubdate' => date('Y-m-d H:i:s', $item['attrs']['pubdate']) ); $search_model->addResult($result_array); } /////// поиск по клубным фоткам ////////// $cl = new SphinxClient(); $cl->SetServer('127.0.0.1', 9312); $cl->SetMatchMode(SPH_MATCH_EXTENDED2); $cl->SetLimits(0, 100); $result = $cl->Query($search_model->against, $search_model->config['Sphinx_Search']['prefix'] .'_clubs_photos'); if ($result !== false) { foreach ($result['matches'] as $id => $item) { $result_array = array( 'link' => '/clubs/photo'. $id .'.html', 'place' => $_LANG['CLUBS_PHOTOALBUM'] .' «'. $item['attrs']['cat_title'] .'»', 'placelink' => '/clubs/photoalbum'. $item['attrs']['cat_id'], 'description' => $search_model->getProposalWithSearchWord($item['attrs']['description']), 'title' => $item['attrs']['title'], 'imageurl' => (file_exists(PATH .'/images/photos/medium/'. $item['attrs']['file']) ? '/images/photos/medium/'. $item['attrs']['file'] : ''), 'pubdate' => date('Y-m-d H:i:s', $item['attrs']['pubdate']) ); $search_model->addResult($result_array); } } return; }
/** * Непосредственно сам поиск * * @param string $sQuery Поисковый запрос * @param string $sObjType Тип поиска * @param int $iOffset Сдвиг элементов * @param int $iLimit Количество элементов * @param array $aExtraFilters Список фильтров * * @return array */ public function FindContent($sQuery, $sObjType, $iOffset, $iLimit, $aExtraFilters) { // * используем кеширование при поиске $sExtraFilters = serialize($aExtraFilters); $cacheKey = Config::Get('plugin.sphinx.prefix') . "searchResult_{$sObjType}_{$sQuery}_{$iOffset}_{$iLimit}_{$sExtraFilters}"; if (false === ($data = E::ModuleCache()->Get($cacheKey))) { // * Параметры поиска $this->oSphinx->SetMatchMode(SPH_MATCH_ALL); $this->oSphinx->SetLimits($iOffset, $iLimit, 1000); // * Устанавливаем атрибуты поиска $this->oSphinx->ResetFilters(); if (!is_null($aExtraFilters)) { foreach ($aExtraFilters as $sAttribName => $sAttribValue) { $this->oSphinx->SetFilter($sAttribName, is_array($sAttribValue) ? $sAttribValue : array($sAttribValue)); } } // * Ищем $sIndex = Config::Get('plugin.sphinx.prefix') . $sObjType . 'Index'; $data = $this->oSphinx->Query($sQuery, $sIndex); if (!is_array($data)) { // Если false, то, скорее всего, ошибка и ее пишем в лог $sError = $this->GetLastError(); if ($sError) { $sError .= "\nquery:{$sQuery}\nindex:{$sIndex}"; if ($aExtraFilters) { $sError .= "\nfilters:"; foreach ($aExtraFilters as $sAttribName => $sAttribValue) { $sError .= $sAttribName . '=(' . (is_array($sAttribValue) ? join(',', $sAttribValue) : $sAttribValue) . ')'; } } $this->LogError($sError); } return false; } /** * Если результатов нет, то и в кеш писать не стоит... * хотя тут момент спорный */ if ($data['total'] > 0) { E::ModuleCache()->Set($data, $cacheKey, array(), 60 * 15); } } return $data; }
function MakeSuggestion($keyword) { $trigrams = BuildTrigrams($keyword); $query = "\"{$trigrams}\"/1"; $len = strlen($keyword); $delta = LENGTH_THRESHOLD; $cl = new SphinxClient(); $cl->SetMatchMode(SPH_MATCH_EXTENDED2); $cl->SetRankingMode(SPH_RANK_WORDCOUNT); $cl->SetFilterRange("len", $len - $delta, $len + $delta); $cl->SetSelect("*, @weight+{$delta}-abs(len-{$len}) AS myrank"); $cl->SetSortMode(SPH_SORT_EXTENDED, "myrank DESC, freq DESC"); $cl->SetArrayResult(true); // pull top-N best trigram matches and run them through Levenshtein $cl->SetLimits(0, TOP_COUNT); $res = $cl->Query($query, "suggest"); if (!$res || !$res["matches"]) { return false; } if (SUGGEST_DEBUG) { print "--- DEBUG START ---\n"; foreach ($res["matches"] as $match) { $w = $match["attrs"]["keyword"]; $myrank = @$match["attrs"]["myrank"]; if ($myrank) { $myrank = ", myrank={$myrank}"; } // FIXME? add costs? // FIXME! does not work with UTF-8.. THIS! IS!! PHP!!! $levdist = levenshtein($keyword, $w); print "id={$match['id']}, weight={$match['weight']}, freq={$match[attrs][freq]}{$myrank}, word={$w}, levdist={$levdist}\n"; } print "--- DEBUG END ---\n"; } // further restrict trigram matches with a sane Levenshtein distance limit foreach ($res["matches"] as $match) { $suggested = $match["attrs"]["keyword"]; if (levenshtein($keyword, $suggested) <= LEVENSHTEIN_THRESHOLD) { return $suggested; } } return $keyword; }
public function getSphinxAdapter() { require_once Mage::getBaseDir('lib') . DIRECTORY_SEPARATOR . 'sphinxapi.php'; // Connect to our Sphinx Search Engine and run our queries $sphinx = new SphinxClient(); $host = Mage::getStoreConfig('sphinxsearch/server/host'); $port = Mage::getStoreConfig('sphinxsearch/server/port'); if (empty($host)) { return $sphinx; } if (empty($port)) { $port = 9312; } $sphinx->SetServer($host, $port); $sphinx->SetMatchMode(SPH_MATCH_EXTENDED2); $sphinx->setFieldWeights(array('name' => 7, 'category' => 1, 'name_attributes' => 1, 'data_index' => 3)); $sphinx->setLimits(0, 200, 1000, 5000); // SPH_RANK_PROXIMITY_BM25 ist default $sphinx->SetRankingMode(SPH_RANK_SPH04, ""); // 2nd parameter is rank expr? return $sphinx; }
public function getResultByTag($keyword = "", $offset = 0, $limit = 0, $searchParams = array()) { $sphinx = $this->config->item('sphinx'); $query = array(); $cl = new SphinxClient(); $cl->SetServer($sphinx['ip'], $sphinx['port']); // 注意这里的主机 $cl->SetConnectTimeout($sphinx['timeout']); $cl->SetArrayResult(true); // $cl->SetIDRange(89,90);//过滤ID if (isset($searchParams['provice_sid']) && $searchParams['provice_sid']) { $cl->setFilter('provice_sid', array($searchParams['provice_sid'])); } if (isset($searchParams['city_sid']) && $searchParams['city_sid']) { $cl->setFilter('city_sid', array($searchParams['city_sid'])); } if (isset($searchParams['piccode']) && $searchParams['piccode']) { $cl->setFilter('piccode', array($searchParams['piccode'])); } if (isset($searchParams['recent']) && $searchParams['recent']) { $cl->SetFilterRange('createtime', time() - 86400 * 30, time()); //近期1个月 } if (isset($searchParams['searchtype']) && $searchParams['searchtype']) { //精确:模糊 $searchtype = SPH_MATCH_ALL; } else { $searchtype = SPH_MATCH_ANY; } $cl->SetLimits($offset, $limit); $cl->SetMatchMode($searchtype); // 使用多字段模式 $cl->SetSortMode(SPH_SORT_EXTENDED, "@weight desc,@id desc"); $index = "*"; $query = $cl->Query($keyword, $index); $cl->close(); return $query; }
/** * sphinx search */ public function sphinxSearch(Request $request, \SphinxClient $sphinx) { $search = $request->get('search'); //sphinx的主机名和端口 //mysql -h 127.0.0.1 -P 9306 $sphinx->SetServer('127.0.0.1', 9312); //设定搜索模式 SPH_MATCH_ALL(匹配所有的查询词) $sphinx->SetMatchMode(SPH_MATCH_ALL); //设置返回结果集为数组格式 $sphinx->SetArrayResult(true); //匹配结果的偏移量,参数的意义依次为:起始位置,返回结果条数,最大匹配条数 $sphinx->SetLimits(0, 20, 1000); //最大搜索时间 $sphinx->SetMaxQueryTime(10); //索引源是配置文件中的 index 类,如果有多个索引源可使用,号隔开:'email,diary' 或者使用'*'号代表全部索引源 $result = $sphinx->query($search, '*'); //返回值说明 total 本次查询返回条数 total_found 一共检索到多少条 docs 在多少文档中出现 hits——共出现了多少次 //关闭查询连接 $sphinx->close(); //打印结果 /*echo "<pre>"; print_r($result); echo "</pre>";exit();*/ $ids = [0]; if (!empty($result)) { foreach ($result['matches'] as $key => $val) { $ids[] = $val['id']; } } $ids = implode(',', array_unique($ids)); $list = DB::select("SELECT * from documents WHERE id IN ({$ids})"); if (!empty($list)) { foreach ($list as $key => $val) { $val->content = str_replace($search, '<span style="color: red;">' . $search . '</span>', $val->content); $val->title = str_replace($search, '<span style="color: red;">' . $search . '</span>', $val->title); } } return view('/sphinx.search')->with('data', array('total' => $result['total'] ? $result['total'] : 0, 'time' => $result['time'] ? $result['time'] : 0, 'list' => $list)); }
private function newSphinxClient() { $s = new SphinxClient(); $s->setServer(Yii::app()->params['sphinx_servername'], Yii::app()->params['sphinx_port']); $s->setMaxQueryTime(5000); $s->SetLimits(0, 100); $s->SetMatchMode(SPH_MATCH_EXTENDED); return $s; }
} $SphinxAPI = realpath($CATSHome . '/' . SPHINX_API); if (!file_exists($SphinxAPI)) { fwrite($stderr, "Config Error: SPHINX_API could not be found.\n"); exit(1); } include $SphinxAPI; /* Sphinx API likes to throw PHP errors *AND* use it's own error * handling. */ assert_options(ASSERT_WARNING, 0); /* Execute the Sphinx query. */ $sphinx = new SphinxClient(); $sphinx->SetServer(SPHINX_HOST, SPHINX_PORT); $sphinx->SetWeights(array(0, 100, 0, 0, 50)); $sphinx->SetMatchMode(SPH_MATCH_BOOLEAN); $sphinx->SetLimits(0, 10); $sphinx->SetSortMode(SPH_SORT_TIME_SEGMENTS, 'date_added'); $sphinx->SetFilter('site_id', TEST_SITE_ID); /* Execute the Sphinx query. Sphinx can ask us to retry if its * maxed out. Retry up to 5 times. */ $tries = 0; do { /* Wait for one second if this isn't out first attempt. */ if (++$tries > 1) { sleep(1); } $results = $sphinx->Query(TEST_QUERY, SPHINX_INDEX); $errorMessage = $sphinx->GetLastError(); } while ($results === false && strpos($errorMessage, 'server maxed out, retry') !== false && $tries <= 5);
public function index() { C('TOKEN_ON', false); $seo = seo(); $this->assign("seo", $seo); if (isset($_GET['q'])) { G('search'); //关键字 $q = Input::forSearch(safe_replace($this->_get("q"))); $q = htmlspecialchars(strip_tags($q)); //时间范围 $time = $this->_get("time"); //模型 $mid = (int) $this->_get("modelid"); //栏目 $catid = (int) $this->_get("catid"); //排序 $order = array("adddate" => "DESC", "searchid" => "DESC"); //搜索历史记录 $shistory = cookie("shistory"); if (!$shistory) { $shistory = array(); } $model = F("Model"); $category = F("Category"); if (trim($_GET['q']) == '') { header('Location: ' . U("Search/Index/index")); exit; } array_unshift($shistory, $q); $shistory = array_slice(array_unique($shistory), 0, 10); //加入搜索历史 cookie("shistory", $shistory); $where = array(); //每页显示条数 $pagesize = $this->config['pagesize'] ? $this->config['pagesize'] : 10; //缓存时间 $cachetime = (int) $this->config['cachetime']; //按时间搜索 if ($time == 'day') { //一天 $search_time = time() - 86400; $where['adddate'] = array("GT", $search_time); } elseif ($time == 'week') { //一周 $search_time = time() - 604800; $where['adddate'] = array("GT", $search_time); } elseif ($time == 'month') { //一月 $search_time = time() - 2592000; $where['adddate'] = array("GT", $search_time); } elseif ($time == 'year') { //一年 $search_time = time() - 31536000; $where['adddate'] = array("GT", $search_time); } else { $search_time = 0; } //可用数据源 $this->config['modelid'] = $this->config['modelid'] ? $this->config['modelid'] : array(); //按模型搜索 if ($mid && in_array($mid, $this->config['modelid'])) { $where['modelid'] = array("EQ", (int) $mid); } //按栏目搜索 if ($catid) { //不支持多栏目搜索,和父栏目搜索。 $where['catid'] = array("EQ", (int) $catid); } //分页模板 $TP = '共有{recordcount}条信息 {pageindex}/{pagecount} {first}{prev}{liststart}{list}{listend}{next}{last}'; //如果开启sphinx if ($this->config['sphinxenable']) { import("Sphinxapi", APP_PATH . C("APP_GROUP_PATH") . '/Search/Class/'); $sphinxhost = $this->config['sphinxhost']; $sphinxport = $this->config['sphinxport']; $cl = new SphinxClient(); //设置searchd的主机名和TCP端口 $cl->SetServer($sphinxhost, $sphinxport); //设置连接超时 $cl->SetConnectTimeout(1); //控制搜索结果集的返回格式 $cl->SetArrayResult(true); //设置全文查询的匹配模式 api http://docs.php.net/manual/zh/sphinxclient.setmatchmode.php $cl->SetMatchMode(SPH_MATCH_EXTENDED2); //设置排名模式 api http://docs.php.net/manual/zh/sphinxclient.setrankingmode.php $cl->SetRankingMode(SPH_RANK_PROXIMITY_BM25); //按一种类似SQL的方式将列组合起来,升序或降序排列。用weight是权重排序 $cl->SetSortMode(SPH_SORT_EXTENDED, "@weight desc"); //设置返回结果集偏移量和数目 $page = (int) $this->_get(C("VAR_PAGE")); $page = $page < 1 ? 1 : $page; $offset = $pagesize * ($page - 1); $cl->SetLimits($offset, $pagesize, $pagesize > 1000 ? $pagesize : 1000); if (in_array($time, array("day", "week", "month", "year"))) { //过滤时间 $cl->SetFilterRange('adddate', $search_time, time(), false); } if ($mid && in_array($mid, $this->config['modelid'])) { //过滤模型 $cl->SetFilter('modelid', (int) $mid); } if ($catid) { //过滤栏目 $cl->SetFilter('catid', (int) $catid); } //执行搜索 api http://docs.php.net/manual/zh/sphinxclient.query.php $res = $cl->Query($q, "*"); //信息总数 $count = $res['total']; //如果结果不为空 if (!empty($res['matches'])) { $result_sphinx = $res['matches']; } $result = array(); //数组重新组合 foreach ($result_sphinx as $k => $v) { $result[$k] = array("searchid" => $v['id'], "adddate" => $v['attrs']['adddate'], "catid" => $v['attrs']['catid'], "id" => $v['attrs']['id'], "modelid" => $v['attrs']['modelid']); } $words = array(); //搜索关键字 foreach ($res['words'] as $k => $v) { $words[] = $k; } $page = page($count, $pagesize); $page->SetPager('default', $TP, array("listlong" => "6", "first" => "首页", "last" => "尾页", "prev" => "上一页", "next" => "下一页", "list" => "*", "disabledclass" => "")); $this->assign("Page", $page->show('default')); } else { import("Segment", APP_PATH . C("APP_GROUP_PATH") . '/Search/Class/'); $Segment = new Segment(); //分词结果 $segment_q = $Segment->get_keyword($Segment->split_result($q)); $words = explode(" ", $segment_q); if (!empty($segment_q)) { $where['_string'] = " MATCH (`data`) AGAINST ('{$segment_q}' IN BOOLEAN MODE) "; } else { //这种搜索最不行 $where['data'] = array('like', "%{$q}%"); } //查询结果缓存 if ($cachetime) { //统计 $count = M("Search")->cache(true, $cachetime)->where($where)->count(); $page = page($count, $pagesize); $result = M("Search")->cache(true, $cachetime)->where($where)->limit($page->firstRow . ',' . $page->listRows)->order($order)->select(); } else { $count = M("Search")->where($where)->count(); $page = $this->page($count, $pagesize); $result = M("Search")->where($where)->limit($page->firstRow . ',' . $page->listRows)->order($order)->select(); } $page->SetPager('default', $TP, array("listlong" => "6", "first" => "首页", "last" => "尾页", "prev" => "上一页", "next" => "下一页", "list" => "*", "disabledclass" => "")); $this->assign("Page", $page->show('default')); } //搜索结果处理 if ($result && is_array($result)) { foreach ($result as $k => $r) { $modelid = $r['modelid']; $id = $r['id']; $tablename = ucwords($model[$modelid]['tablename']); if ($tablename) { $result[$k] = M($tablename)->where(array("id" => $id))->find(); } } } //搜索记录 if (strlen($q) < 17 && strlen($q) > 1 && $result) { $res = M("SearchKeyword")->where(array('keyword' => $q))->find(); if ($res) { //关键词搜索数+1 M("SearchKeyword")->where(array('keyword' => $q))->setInc("searchnums"); } else { //关键词转换为拼音 load("@.iconvfunc"); $pinyin = gbk_to_pinyin(iconv('utf-8', 'gbk//IGNORE', $q)); if (is_array($pinyin)) { $pinyin = implode('', $pinyin); } M("SearchKeyword")->add(array('keyword' => $q, 'searchnums' => 1, 'data' => $segment_q, 'pinyin' => $pinyin)); } } //相关搜索功能 if ($this->config['relationenble']) { $map = array(); //相关搜索 if (!empty($segment_q)) { $relation_q = str_replace(' ', '%', $segment_q); } else { $relation_q = $q; } $map['_string'] = " MATCH (`data`) AGAINST ('%{$relation_q}%' IN BOOLEAN MODE) "; $relation = M("SearchKeyword")->where($map)->select(); $this->assign("relation", $relation); } foreach ($this->config['modelid'] as $modelid) { $source[$modelid] = array("name" => $model[$modelid]['name'], "modelid" => $modelid); } //搜索结果 $this->assign("result", $result); //运行时间 $search_time = G('search', 'end', 6); $this->assign("count", $count ? $count : 0); $this->assign("search_time", $search_time); $this->assign("keyword", $q); $this->assign("category", $category); $this->assign("source", $source); $this->assign("time", $time); $this->assign("modelid", $mid); $this->assign("shistory", $shistory); //分词后的搜索关键字 $this->assign("words", $words); $this->display("search"); } else { $this->display(); } }
/** * {@inheritdoc }. * * SPH_MATCH_ALL, matches all query words (<b>default mode</b>); * SPH_MATCH_ANY, matches any of the query words; * SPH_MATCH_PHRASE, matches query as a phrase, requiring perfect match; * SPH_MATCH_BOOLEAN, matches query as a boolean expression; * SPH_MATCH_EXTENDED, matches query as an expression in Sphinx internal query language; * SPH_MATCH_FULLSCAN, matches query, forcibly using the "full scan" mode as below. * * @see http://sphinxsearch.com/docs/current.html#matching-modes Matching modes */ public function SetMatchMode($mode) { return parent::SetMatchMode($mode); }
require_once '../tools/db.php'; require_once '../tools/main.php'; include_once "../tools/sphinxapi.php"; if (isset($_REQUEST["number"]) && intval($_REQUEST["number"]) > 9) { $number = $_REQUEST["number"]; } else { $number = 9; } $pageNum = isset($_REQUEST["page"]) ? intval($_REQUEST["page"]) : 0; $mdb = new MeekroDB(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_TABLE_NAME, DB_PORT, DB_CHARSET); $sphinx = new SphinxClient(); $sphinx->SetServer(SPHINX_HOST, SPHINX_PORT); $sphinx->SetArrayResult(true); $sphinx->SetLimits($pageNum * $number, $number, 5000); $sphinx->SetMaxQueryTime(10); $sphinx->SetMatchMode(SPH_MATCH_EXTENDED2); $sphinx->SetFilter('enabled', array(1)); if (isset($_REQUEST["type"])) { $r1 = $mdb->queryFirstRow("SELECT id, name FROM media_type WHERE name=%s", $_REQUEST["type"]); $type = $r1["id"]; //set type if ($type == "") { echo json_encode(array("result" => null, "status" => false, "error" => "请求的类型不存在")); exit; } $sphinx->SetFilter('type', array($type)); } if (isset($_REQUEST['language'])) { $r2 = $mdb->queryFirstRow("SELECT id, name FROM media_language WHERE name=%s", $_REQUEST["language"]); $language = $r2["id"]; //set language
function do_query($search_str) { //$tmp_var = array(array('itemName' => "test1"), array('itemName' => "test2"), array('itemName' => "test3")); //echo implode(",",tmp_var); //echo json_encode($tmp_var); //return tmp_var; $q = ""; $sql = ""; $mode = SPH_MATCH_ALL; $host = "localhost"; $port = 9312; $index = "*"; $groupby = ""; $groupsort = "@group desc"; $filter = "group_id"; $filtervals = array(); $distinct = ""; $sortby = ""; $sortexpr = ""; $limit = 20; $ranker = SPH_RANK_PROXIMITY_BM25; $select = "*"; $cl = new SphinxClient(); $cl->SetServer($host, $port); $cl->SetConnectTimeout(1); $cl->SetArrayResult(true); $cl->SetWeights(array(100, 1)); $cl->SetMatchMode($mode); if (count($filtervals)) { $cl->SetFilter($filter, $filtervals); } if ($groupby) { $cl->SetGroupBy($groupby, SPH_GROUPBY_ATTR, $groupsort); } if ($sortby) { $cl->SetSortMode(SPH_SORT_EXTENDED, $sortby); } if ($sortexpr) { $cl->SetSortMode(SPH_SORT_EXPR, $sortexpr); } if ($distinct) { $cl->SetGroupDistinct($distinct); } if ($select) { $cl->SetSelect($select); } if ($limit) { $cl->SetLimits(0, $limit, $limit > 1000 ? $limit : 1000); } $cl->SetRankingMode($ranker); $res = $cl->Query($search_str, $index); //return $res; if (is_array($res["matches"])) { $results = array(); $n = 1; //print "Matches:\n"; foreach ($res["matches"] as $docinfo) { //print "$n. doc_id=$docinfo[id], weight=$docinfo[weight]"; $attr_array = array(); $results[$docinfo[id]]; foreach ($res["attrs"] as $attrname => $attrtype) { $value = $docinfo["attrs"][$attrname]; if ($attrtype == SPH_ATTR_MULTI || $attrtype == SPH_ATTR_MULTI64) { $value = "(" . join(",", $value) . ")"; } else { if ($attrtype == SPH_ATTR_TIMESTAMP) { $value = date("Y-m-d H:i:s", $value); } } $attr_array[$attrname] = $value; //print $value; } $results[$docinfo[id]] = $attr_array; $n++; //print implode("",$results)."\n"; } return $results; } }
function getCategory($keywords) { $q = $keywords; $mode = SPH_MATCH_EXTENDED; $item = array(); $comma_separated = ""; //Init config $host = SPHINX_SERVER; $port = SPHINX_PORT; $index = '*'; $ranker = SPH_RANK_PROXIMITY_BM25; $cl = new SphinxClient(); $cl->SetServer($host, $port); $cl->SetConnectTimeout(1); $cl->SetWeights(array(100, 1)); $cl->SetMatchMode($mode); $cl->SetRankingMode($ranker); $cl->SetArrayResult(true); $cl->SetFilter('status', array('1')); $cl->SetGroupBy("level_1_category_id", SPH_GROUPBY_ATTR, "@count DESC"); $res = $cl->Query($q, $index); $arr = array(); if ($res && isset($res["matches"])) { if (is_array($res["matches"])) { foreach ($res["matches"] as $results) { $arr[] = $results["attrs"]; } } } return $arr; }
public function __construct($rowsPerPage, $currentPage, $siteID, $wildCardString, $sortBy, $sortDirection) { $this->_db = DatabaseConnection::getInstance(); $this->_siteID = $siteID; $this->_sortByFields = array('firstName', 'lastName', 'city', 'state', 'dateModifiedSort', 'dateCreatedSort', 'ownerSort'); if (ENABLE_SPHINX) { /* Sphinx API likes to throw PHP errors *AND* use it's own error * handling. */ assert_options(ASSERT_WARNING, 0); $sphinx = new SphinxClient(); $sphinx->SetServer(SPHINX_HOST, SPHINX_PORT); $sphinx->SetWeights(array(0, 100, 0, 0, 50)); $sphinx->SetMatchMode(SPH_MATCH_EXTENDED); $sphinx->SetLimits(0, 1000); $sphinx->SetSortMode(SPH_SORT_TIME_SEGMENTS, 'date_added'); // FIXME: This can be sped up a bit by actually grouping ranges of // site IDs into their own index's. Maybe every 500 or so at // least on the Hosted system. $sphinx->SetFilter('site_id', array($this->_siteID)); /* Create the Sphinx query string. */ $wildCardString = DatabaseSearch::humanToSphinxBoolean($wildCardString); /* Execute the Sphinx query. Sphinx can ask us to retry if its * maxed out. Retry up to 5 times. */ $tries = 0; do { /* Wait for one second if this isn't out first attempt. */ if (++$tries > 1) { sleep(1); } $results = $sphinx->Query($wildCardString, SPHINX_INDEX); $errorMessage = $sphinx->GetLastError(); } while ($results === false && strpos($errorMessage, 'server maxed out, retry') !== false && $tries <= 5); /* Throw a fatal error if Sphinx errors occurred. */ if ($results === false) { $this->fatal('Sphinx Error: ' . ucfirst($errorMessage) . '.'); } /* Throw a fatal error (for now) if Sphinx warnings occurred. */ $lastWarning = $sphinx->GetLastWarning(); if (!empty($lastWarning)) { // FIXME: Just display a warning, and notify dev team. $this->fatal('Sphinx Warning: ' . ucfirst($lastWarning) . '.'); } /* Show warnings for assert()s again. */ assert_options(ASSERT_WARNING, 1); if (empty($results['matches'])) { $this->_WHERE = '0'; } else { $attachmentIDs = implode(',', array_keys($results['matches'])); $this->_WHERE = 'attachment.attachment_id IN(' . $attachmentIDs . ')'; } } else { $this->_WHERE = DatabaseSearch::makeBooleanSQLWhere(DatabaseSearch::fulltextEncode($wildCardString), $this->_db, 'attachment.text'); } /* How many companies do we have? */ $sql = sprintf("SELECT\n COUNT(*) AS count\n FROM\n attachment\n LEFT JOIN candidate\n ON attachment.data_item_id = candidate.candidate_id\n AND attachment.data_item_type = %s\n AND attachment.site_id = candidate.site_id\n LEFT JOIN user AS owner_user\n ON candidate.owner = owner_user.user_id\n WHERE\n resume = 1\n AND\n %s\n AND\n (ISNULL(candidate.is_admin_hidden) OR (candidate.is_admin_hidden = 0))\n AND\n (ISNULL(candidate.is_active) OR (candidate.is_active = 1))\n AND\n attachment.site_id = %s", DATA_ITEM_CANDIDATE, $this->_WHERE, $this->_siteID); $rs = $this->_db->getAssoc($sql); /* Pass "Search By Resume"-specific parameters to Pager constructor. */ parent::__construct($rs['count'], $rowsPerPage, $currentPage); }
/** * ¬озвращает список публичных типовых услуг по заданным услови¤м и пагинацией * * @return array */ public function getList($excluded_ids = array()) { $criteria = array($this->category_id, $this->city_id, $this->country_id, $this->keywords, $this->limit, $this->offset, $this->price_ranges, $this->price_max, $this->order, $excluded_ids, $this->user_id); $membuf = new memBuff(); $memkey = __METHOD__ . '#' . md5(serialize($criteria)); if (false !== ($result = $membuf->get($memkey)) && is_release()) { return $result; } $sort = $this->getSort(); # @see http://sphinxsearch.com/forum/view.html?id=11538 about city = x or country = y $sphinxClient = new SphinxClient(); $sphinxClient->SetServer(SEARCHHOST, SEARCHPORT); $sphinxClient->SetLimits($this->offset, $this->limit, 20000); $sphinxClient->SetSortMode(SPH_SORT_EXTENDED, $sort); $sphinxClient->SetFieldWeights(array('title' => 2, 'extra_title' => 1)); //$sphinxClient->SetRankingMode(SPH_RANK_PROXIMITY_BM25); $selectExpression = '*'; // все колонки if ($this->user_id) { $selectExpression .= ", IF(user_id = {$this->user_id}, 1, 0) as match_user"; $sphinxClient->setFilter('match_user', array(1)); } if ($this->category_id) { $selectExpression .= ", IF(category_id = {$this->category_id} or category_parent_id = {$this->category_id}, 1, 0) as match_category"; $sphinxClient->setFilter('match_category', array(1)); } if ($this->country_id) { $selectExpression .= ", IF(user_country_id = {$this->country_id} or country_id = {$this->country_id}, 1, 0) as match_country"; $sphinxClient->setFilter('match_country', array(1)); } if ($this->city_id) { $selectExpression .= ", IF(user_city_id = {$this->city_id} or city_id = {$this->city_id}, 1, 0) as match_city"; $sphinxClient->setFilter('match_city', array(1)); } if (count($this->price_ranges)) { $match_price_exprs = array(); foreach ($this->getPriceRanges() as $i => $price_range) { if (!isset($this->price_ranges[$i])) { continue; } $match_price_exprs[] = "price_{$i} = 1"; } $match_price_exprs = implode(' or ', $match_price_exprs); $selectExpression .= ", IF({$match_price_exprs}, 1, 0) as match_price"; $sphinxClient->setFilter('match_price', array(1)); } if ($this->price_max > 0) { $selectExpression .= ", IF(price <= {$this->price_max}, 1, 0) as match_price_max"; $sphinxClient->setFilter('match_price_max', array(1)); } $searchString = ''; if (!empty($this->keywords)) { $keywords = implode(' ', array_filter(preg_split('/\\s*,\\s*/', $this->keywords))); $searchString = trim($keywords); //$searchString = $this->GetSphinxKeyword($searchString); $sphinxClient->SetMatchMode(SPH_MATCH_ANY); //SPH_MATCH_EXTENDED2); } if (count($excluded_ids)) { $sphinxClient->setFilter('tservice_id', $excluded_ids, true); } $sphinxClient->SetSelect($selectExpression); $queryResult = $sphinxClient->query($searchString, "tservices;delta_tservices"); //echo '<pre>error: ', $sphinxClient->GetLastError(), '</pre>'; //echo '<pre>warn : ', $sphinxClient->GetLastWarning(), '</pre>'; $list = array(); $total = 0; if (isset($queryResult['matches'])) { foreach ($queryResult['matches'] as $id => $row) { $row['attrs']['id'] = $id; $list[] = $row['attrs']; } $total = $queryResult['total_found'] < $queryResult['total'] ? $queryResult['total_found'] : $queryResult['total']; } $result = array('list' => $list, 'total' => $total); if ($this->_ttl) { $membuf->set($memkey, $result, $this->_ttl); } return $result; }