function search($string, $index_name = NULL) { $string = urldecode($string); //用PHPCWS中文分词扩展对输入的关键字进行切分 if (mb_strlen($string, "UTF-8") > 4) { //大于4个汉字的关键词才调用中文分词接口 $string = mb_convert_encoding($string, "GBK", "UTF-8"); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->httpcws_url . urlencode($string)); curl_setopt($ch, CURLOPT_TIMEOUT, 3); //3秒超时时间 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回内容不是输出 $string_tmp = curl_exec($ch); curl_close($ch); if ($string_tmp != "") { $string = mb_convert_encoding($string_tmp, "UTF-8", "GBK"); } } $sphinx_query_oldarr = explode(" ", $string); foreach ($sphinx_query_oldarr as $key => $value) { if (substr($value, 0, 1) == "@" || $value == "|") { $sphinx_query_newarr[$key] = $value; //不加引号 } else { if ($this->is_chinese($value, "GBK") == 1 || $this->is_chinese($value, "GBK") == 2) { //如果该字符串全部是中文、中英文混合 $sphinx_query_newarr[$key] = "\"" . $value . "\""; //关键字加上引号 } else { $sphinx_query_newarr[$key] = $value; //如果不是汉字则不加引号 } } } $this->search_string = implode(" ", $sphinx_query_newarr); unset($sphinx_query_oldarr, $sphinx_query_newarr); if (NULL !== $index_name) { //多个索引查询 if (is_array($index_name)) { $index_names = ''; foreach ($index_name as &$label) { if (isset($this->indexes[$label])) { $index_names .= $this->indexes[$label] . ' '; } } $this->index_name = $index_names; } else { if (isset($this->indexes[$index_name])) { $this->index_name = $this->indexes[$index_name]; } } } if (empty($this->index_name)) { exit('没有设置该' . $this->index_name . '索引' . PHP_EOL); } //清除当前设置的过滤器 $this->sphinx->ResetFilters(); return $this; }
/** * Непосредственно сам поиск * * @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; }
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("*"); }
/** * @return SphinxClient */ public function getSphinxClient() { if (null === $this->_sphinxClient) { if (!class_exists("SphinxClient")) { $this->load->library('sphinx/sphinxapi'); } $sphinxClient = new SphinxClient(); $sphinxClient->SetServer($this->config->get('sphinx_search_server'), $this->config->get('sphinx_search_port')); $sphinxClient->SetConnectTimeout(1); //$sphinxClient->_mbenc = "UTF-8"; $sphinxClient->ResetFilters(); $this->_sphinxClient = $sphinxClient; } return $this->_sphinxClient; }
/** * Непосредственно сам поиск * * @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; }
$file = fopen("spec/fixtures/data/boolean.bin", "w"); fwrite($file, $client->_reqs[$client->AddQuery("test ")]); fclose($file); $client->SetMatchMode(SPH_MATCH_ALL); // phrase $client->SetMatchMode(SPH_MATCH_PHRASE); $file = fopen("spec/fixtures/data/phrase.bin", "w"); fwrite($file, $client->_reqs[$client->AddQuery("testing this ")]); fclose($file); $client->SetMatchMode(SPH_MATCH_ALL); // filter $client->SetFilter("id", array(10, 100, 1000)); $file = fopen("spec/fixtures/data/filter.bin", "w"); fwrite($file, $client->_reqs[$client->AddQuery("test ")]); fclose($file); $client->ResetFilters(); // group $client->SetGroupBy("id", SPH_GROUPBY_ATTR, "id"); $file = fopen("spec/fixtures/data/group.bin", "w"); fwrite($file, $client->_reqs[$client->AddQuery("test ")]); fclose($file); $client->ResetGroupBy(); // distinct $client->SetGroupDistinct("id"); $file = fopen("spec/fixtures/data/distinct.bin", "w"); fwrite($file, $client->_reqs[$client->AddQuery("test ")]); fclose($file); $client->ResetGroupBy(); // weights $client->SetWeights(array(100, 1)); $file = fopen("spec/fixtures/data/weights.bin", "w");
private function resetSphinx() { $this->sphinx->ResetGroupBy(); $this->sphinx->ResetFilters(); return $this; }
/** * @{inheritDoc} */ public function new_search_query() { $this->client->ResetFilters(); return $this; }
public function indexAction(Application $application, Template $template) { $template->setParameter('title', 'Форс-о-метр'); if (array_key_exists('query', $_GET)) { $query = $this['query'] = $_GET['query']; $search = new SphinxClient(); $search->SetServer('localhost', 3312); $search->SetGroupBy('created_at', SPH_GROUPBY_MONTH); // Полный поиск (запрос): $search->ResetFilters(); $search->SetMatchMode(SPH_MATCH_PHRASE); $search->AddQuery($query, 'forceometer'); // Поиск уникальных в месяц (запрос): $search->ResetFilters(); $search->SetMatchMode(SPH_MATCH_PHRASE); $search->SetFilter('uniq_m', array(1)); $search->AddQuery($query, 'forceometer'); // Поиск уникальных вообще (запрос): $search->ResetFilters(); $search->SetMatchMode(SPH_MATCH_PHRASE); $search->SetFilter('uniq_f', array(1)); $search->AddQuery($query, 'forceometer'); $query_result = $search->RunQueries(); // Поиск уникальных постеров месяца (вообще): $search->ResetFilters(); $search->SetMatchMode(SPH_MATCH_FULLSCAN); $search->SetFilter('uniq_m', array(1)); $bare_query = $search->Query('', 'forceometer'); /* $search -> ResetFilters(); $search -> SetMatchMode(SPH_MATCH_FULLSCAN); $bare_query2 = $search -> Query('', 'forceometer'); */ if (!$query_result || $query_result[0]['total_found'] == 0) { $this['not_found'] = 1; return true; } $result = array('posts' => array(), 'posters' => array(), 'uniq_m' => array(), 'uniq_f' => array()); for ($y = 2009; $y <= date('Y'); $y++) { for ($m = 1; $m <= 12; $m++) { // Пропуск несуществующих дат и текущего месяца: if ($y == 2009 && $m < 3) { continue; } if ($y == date('Y') && $m >= date('m')) { break; } $stamp = $y . '-' . ($m < 10 ? '0' . $m : $m) . '-01'; $result['posts'][$stamp] = 0; $result['posters'][$stamp] = 0; $result['uniq_m'][$stamp] = 0; $result['uniq_f'][$stamp] = 0; } } // Проходим результаты: if ($query_result[0]['matches']) { foreach ($query_result[0]['matches'] as $match) { $date = $match['attrs']['@groupby']; $stamp = substr($date, 0, 4) . '-' . substr($date, 4, 2) . '-01'; if (array_key_exists($stamp, $result['posts'])) { $result['posts'][$stamp] = $match['attrs']['@count']; } } } if ($bare_query['matches']) { foreach ($bare_query['matches'] as $match) { $date = $match['attrs']['@groupby']; $stamp = substr($date, 0, 4) . '-' . substr($date, 4, 2) . '-01'; if (array_key_exists($stamp, $result['posters'])) { $result['posters'][$stamp] = $match['attrs']['@count']; } } } if ($query_result[1]['matches']) { foreach ($query_result[1]['matches'] as $match) { $date = $match['attrs']['@groupby']; $stamp = substr($date, 0, 4) . '-' . substr($date, 4, 2) . '-01'; if (array_key_exists($stamp, $result['uniq_m'])) { $result['uniq_m'][$stamp] = $match['attrs']['@count']; } } } /* if ($bare_query2['matches']) { foreach($bare_query2['matches'] as $match) { $date = $match['attrs']['@groupby']; $stamp = substr($date, 0, 4) .'-'. substr($date, 4, 2) .'-01'; if (array_key_exists($stamp, $result['uniq_f'])) $result['uniq_f'][$stamp] = $match['attrs']['@count']; } }*/ foreach ($result['posts'] as $date => $count) { if ($count == 0) { unset($result['posts'][$date]); unset($result['posters'][$date]); unset($result['uniq_f'][$date]); unset($result['uniq_m'][$date]); } } $this['results'] = json_encode($result); unset($result); return true; } return true; }
if ($days) { $searcher->SetFilterRange("starttime", 0, time() - $days * 24 * 60 * 60, true); //exclude too old results } $prev_instanses_count += $total_tths; $start = max(0, $min - $prev_instanses_count); $len = min(RPP, max(1, $max - $prev_instanses_count)); $searcher->setLimits($start, $len); $dirs_result = $searcher->query($query, "dc_dirs dc_dirs_delta"); $total_dirs = $dirs_result['total']; if ($total_dirs && is_array($dirs_result['matches']) && count($out_array) < RPP) { $dirs = Searcher::getDirs(array_keys($dirs_result['matches'])); $out_array = array_merge($out_array, $dirs); } } $searcher->ResetFilters(); if ($days) { $searcher->SetFilterRange("starttime", 0, time() - $days * 24 * 60 * 60, true); //exclude too old results } if ($category) { $searcher->SetFilter("extension_crc32", Searcher::getExtsCrc32($category)); } $minsize_calc = Searcher::getSizeFromHuman($minsize); if ($minsize_calc) { $searcher->SetFilter("size", 0, $minsize_calc, true); //exclude too little results } //FILES $prev_instanses_count += $total_dirs; $start = max(0, $min - $prev_instanses_count);
/** * reset filter condition * 重置条件 */ public function resetFilter() { parent::ResetFilters(); }