Esempio n. 1
0
 /**
  * Передает текущие параметры элемента поиска API SphinxClient.
  * Необходимо определить все переменные до вызова этой функции (например, в перегруженной версии).
  */
 public function setEngine()
 {
     $this->_engine->SetServer($this->_host, $this->_port);
     $this->_engine->SetLimits($this->_offset, $this->_limit, $this->_maxmatches, $this->_cutoff);
     $this->_engine->SetMaxQueryTime($this->_maxquerytime);
     $this->_engine->SetRankingMode($this->_ranker);
     $this->_engine->SetMatchMode($this->_mode);
     $this->_engine->SetFieldWeights($this->_fieldweights);
     $this->_engine->SetIndexWeights($this->_indexweights);
     $this->_engine->SetRetries($this->_retrycount, $this->_retrydelay);
     $this->_engine->SetArrayResult($this->_arrayresult);
     $this->_engine->ResetFilters();
     $this->_engine->ResetGroupBy();
     $this->_engine->ResetOverrides();
     $this->_engine->SetIDRange($this->_min_id, $this->_max_id);
     $this->_engine->SetSelect($this->_select);
     if ($this->_overrides) {
         call_user_func_array(array($this->_engine, 'SetOverride'), $this->_overrides);
     }
     if ($this->_anchor) {
         call_user_func_array(array($this->_engine, 'SetGeoAnchor'), $this->_anchor);
     }
     foreach ($this->_filtersV as $f) {
         call_user_func_array(array($this->_engine, 'SetFilter'), $f);
     }
     foreach ($this->_filtersR as $f) {
         call_user_func_array(array($this->_engine, 'SetFilterRange'), $f);
     }
     foreach ($this->_filtersRF as $f) {
         call_user_func_array(array($this->_engine, 'SetFilterFloatRange'), $f);
     }
     $this->_engine->SetGroupBy($this->_groupby, $this->_groupfunc, $this->_groupsort);
     $this->_engine->SetGroupDistinct($this->_groupdistinct);
     $this->_engine->SetSortMode($this->_sort, $this->_sortby);
 }
 /**
  * Run the search query. 
  * This allows us to set different indexes
  *
  * @access	public
  * @param	object	$sphinxClient	The sphinx client reference
  * @param	string 	$search_term	The search term
  * @param	bool	$group_results	Group posts by topic
  * @return	array 	Sphinx result array
  **/
 public function runSearchQuery($sphinxClient, $search_term = '', $group_results = false)
 {
     if (ipsRegistry::$settings['search_method'] == 'sphinx') {
         if (ipsRegistry::$request['content_title_only'] == 1) {
             return $sphinxClient->Query($search_term, 'forums_search_topics_main,forums_search_topics_delta');
         } else {
             if ($group_results) {
                 $sphinxClient->SetGroupBy('tid', SPH_GROUPBY_ATTR, '@group DESC');
             }
             if (ipsRegistry::$request['f_content_only']) {
                 return $sphinxClient->Query($search_term, 'forums_search_posts_main,forums_search_posts_delta');
             } else {
                 return $sphinxClient->Query($search_term, 'forums_search_posts_main,forums_search_topics_main,forums_search_posts_delta,forums_search_topics_delta');
             }
         }
     } else {
         return array();
     }
 }
 /**
  * Does search
  *
  * @access	private
  * @param	string	$search_term
  * @param	array	$limit_clause	The erray should be array( begin, end )
  * @param	string	$sort_by		Either relevance or date
  * @param	string	[$group_by]		Field to group on
  * @param	bool	[$count_only]	Set to true for a count(*) query
  * @param	bool	[$content_title_only]	Only search titles
  * @return	array
  **/
 private function _searchQuery($search_term, $limit_clause, $sort_by, $group_by = '', $count_only = false, $content_title_only = false)
 {
     /* Do we only need to count results? */
     if (!$count_only) {
         if ($limit_clause[1]) {
             /* Limit Results */
             $this->sphinxClient->SetLimits(intval($limit_clause[0]), intval($limit_clause[1]));
         } else {
             if ($limit_clause[0]) {
                 $this->sphinxClient->SetLimits(0, intval($limit_clause[0]));
             }
         }
         /* Sort By */
         if (isset($sort_by) && in_array($sort_by, array('date', 'relevance'))) {
             if ($sort_by == 'date') {
                 if ($this->request['search_sort_order'] == 0) {
                     $this->sphinxClient->SetSortMode(SPH_SORT_ATTR_DESC, $this->appSearchPlugin->getDateField());
                 } else {
                     $this->sphinxClient->SetSortMode(SPH_SORT_ATTR_ASC, $this->appSearchPlugin->getDateField());
                 }
             } else {
                 $this->sphinxClient->SetSortMode(SPH_SORT_RELEVANCE);
             }
         } else {
             $this->sphinxClient->SetSortMode(SPH_SORT_RELEVANCE);
         }
     }
     /* Exclude Apps */
     if (count($this->exclude_apps)) {
         $app_id_exclude = array();
         foreach ($this->exclude_apps as $app_dir) {
             $app_id_exclude[] = ipsRegistry::$applications[$app_dir]['app_id'];
         }
         $this->sphinxClient->SetFilter('app', $app_id_exclude, TRUE);
     }
     /* Permissions */
     $perm_array = $this->member->perm_id_array;
     $perm_array[] = 0;
     /* Need to remove empty values... */
     $final_perms = array();
     foreach ($perm_array as $perm_id) {
         if (is_numeric($perm_id)) {
             $final_perms[] = $perm_id;
         }
     }
     $this->sphinxClient->SetFilter('perm_view', $final_perms);
     /* Exclude some items */
     if (!$this->memberData['g_is_supmod']) {
         /* Owner only */
         $this->sphinxClient->SetFilter('owner_only', array(0, $this->memberData['member_id']));
         /* Friend only */
         $this->DB->build(array('select' => 'friends_member_id', 'from' => 'profile_friends', 'where' => "friends_friend_id={$this->memberData['member_id']}"));
         $this->DB->execute();
         $friends_ids = array(0);
         while ($r = $this->DB->fetch()) {
             $friends_ids[] = $r['friends_member_id'];
         }
         $this->sphinxClient->SetFilter('friend_only', $friends_ids);
         /* Authorized users only */
         $this->sphinxClient->SetFilter('authorized_users', array(0, $this->memberData['member_id']));
     }
     /* Loop through all the search plugins and let them modify the search query */
     foreach (ipsRegistry::$applications as $app) {
         if (IPSSearchIndex::appisSearchable($app['app_directory'])) {
             if (!isset($this->display_plugins[$app['app_directory']]) || !$this->display_plugins[$app['app_directory']]) {
                 require_once IPSLib::getAppDir($app['app_directory']) . '/extensions/searchDisplay.php';
                 $_class = $app['app_directory'] . 'SearchDisplay';
                 $this->display_plugins[$app['app_directory']] = new $_class();
             }
             $this->display_plugins[$app['app_directory']]->search_plugin = $this->appSearchPlugin;
             if (method_exists($this->display_plugins[$app['app_directory']], 'modifySearchQuery')) {
                 /* Get the modified query */
                 $this->display_plugins[$app['app_directory']]->modifySearchQuery($this->sphinxClient, $count_only);
             }
         }
     }
     $groupby = $this->request['show_as_titles'] ? true : false;
     /* Perform the search */
     if (method_exists($this->display_plugins[$this->request['search_app']], 'runSearchQuery')) {
         $result = $this->display_plugins[$this->request['search_app']]->runSearchQuery($this->sphinxClient, $search_term, $groupby);
     } else {
         if ($groupby) {
             $this->sphinxClient->SetGroupBy('search_id', SPH_GROUPBY_ATTR, '@group DESC');
         }
         $result = $this->sphinxClient->Query($search_term, $this->request['search_app'] . '_search_main,' . $this->request['search_app'] . '_search_delta');
     }
     /* Return the total number of results */
     if ($count_only) {
         return $result['total'];
     } else {
         $search_ids = array();
         if (is_array($result['matches']) && count($result['matches'])) {
             foreach ($result['matches'] as $res) {
                 $search_ids[] = $res['attrs']['search_id'];
             }
         }
         return $search_ids;
     }
 }