SetConnectTimeout() public method

set server connection timeout (0 to remove)
public SetConnectTimeout ( $timeout )
Example #1
0
 /**
  * Constructor.
  *
  * @param string $host The server's host name/IP.
  * @param string $port The port that the server is listening on.
  * @param array $indexes The list of indexes that can be used.
  */
 function __construct()
 {
     //sphinx
     $sphinxconf = Yaf_Registry::get("config")->get('sphinx')->toArray();
     $this->host = $sphinxconf['host'];
     $this->port = $sphinxconf['port'];
     $this->indexes = $sphinxconf['indexes'];
     $this->sphinx = new SphinxClient();
     $this->sphinx->SetServer($this->host, $this->port);
     //sphinx连接超时时间,单位为秒
     $this->sphinx->SetConnectTimeout(5);
     //sphinx搜索结果集返回方式:TRUE为普通数组返回,FALSE为PHP hash格式返回
     $this->sphinx->SetArrayResult(false);
     //sphinx最大搜索时间,单位为毫秒
     $this->sphinx->SetMaxQueryTime(5000);
     //匹配模式
     $this->sphinx->SetMatchMode(SPH_MATCH_EXTENDED2);
     //设置权重评分模式,详见sphinx文档第6.3.2节:SetRankingMode
     $this->sphinx->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
     //设置排序模式排序模式
     $this->sphinx->SetSortMode(SPH_SORT_EXTENDED, '@weight DESC,@id DESC');
     //中文分词配置
     $httpcwsconf = Yaf_Registry::get("config")->get('httpcws')->toArray();
     //中文分词HTTPCWS服务器接口地址
     $this->httpcws_url = 'http://' . $httpcwsconf['host'] . ':' . $httpcwsconf['port'] . '/?w=';
 }
 /**
  * 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;
 }
Example #3
0
 /**
  * 初始化搜索引擎
  */
 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);
     //匹配模式
 }
Example #4
0
 /**
  * 初始化搜索引擎
  */
 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);
     //匹配模式
 }
Example #5
0
 /**
  *
  * @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);
 }
Example #6
0
 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());
     }
 }
Example #7
0
 public function __construct(array $config)
 {
     $this->_sphinx = new \SphinxClient();
     if (isset($config['host'])) {
         if (!isset($config['host'])) {
             $config['port'] = 9312;
         }
         $this->_sphinx->SetServer($config['host'], $config['port']);
     }
     if (isset($config['retries'])) {
         if (!isset($config['delay'])) {
             $config['delay'] = 0;
         }
         $this->_sphinx->SetRetries($config['retries'], $config['delay']);
     }
     if (isset($config['timeout'])) {
         $this->_sphinx->SetConnectTimeout($config['timeout']);
     }
 }
 public function __construct(Application $app, $host, $port, $rt_host, $rt_port)
 {
     $this->app = $app;
     $this->sphinx = new \SphinxClient();
     $this->sphinx->SetServer($host, $port);
     $this->sphinx->SetArrayResult(true);
     $this->sphinx->SetConnectTimeout(1);
     $this->suggestionClient = new \SphinxClient();
     $this->suggestionClient->SetServer($host, $port);
     $this->suggestionClient->SetArrayResult(true);
     $this->suggestionClient->SetConnectTimeout(1);
     try {
         $this->rt_conn = @new \PDO(sprintf('mysql:host=%s;port=%s;', $rt_host, $rt_port));
         $this->rt_conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
     } catch (\PDOException $e) {
         $this->rt_conn = null;
     }
     return $this;
 }
Example #9
0
 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;
 }
Example #10
0
 /**
  * @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;
 }
Example #11
0
 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;
 }
 public function setConnectionTimeout($timeout)
 {
     $this->sphinxClient->SetConnectTimeout((int) $timeout);
 }
Example #13
0
<?php

require "sphinxapi.php";
$cl = new SphinxClient();
$cl->SetServer('127.0.0.1', 9312);
$cl->SetConnectTimeout(1);
$cl->SetArrayResult(true);
// $cl->SetWeights ( array ( 100, 1 ) );
$cl->SetMatchMode(SPH_MATCH_EXTENDED2);
$cl->SetRankingMode(SPH_RANK_WORDCOUNT);
// $cl->SetSortMode ( SPH_SORT_EXTENDED, '@weight DESC' );
// $cl->SetSortMode ( SPH_SORT_EXPR, $sortexpr );
// $cl->SetFieldWeights(array('title'=>10,'content'=>1));
$res = $cl->Query('sphinxse', "*");
print_r($res['matches']);
Example #14
0
function sphinx_search($query, $offset = 0, $limit = 30)
{
    require_once 'lib/sphinxapi.php';
    $sphinxClient = new SphinxClient();
    $sphinxClient->SetServer('localhost', 9312);
    $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($query, 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);
        }
    }
    return $ids;
}
Example #15
0
<?php

require 'sphinxapi.php';
$cl = new SphinxClient();
$cl->SetServer('localhost');
$cl->SetConnectTimeout(5);
$cl->SetMatchMode(SPH_MATCH_ANY);
$cl->SetLimits(0, 25, 1000);
$cl->SetArrayResult(true);
$result = $cl->Query('Codeine');
if ($result === false) {
    echo "Query failed: " . $cl->GetLastError() . ".\n";
} else {
    if ($cl->GetLastWarning()) {
        echo "WARNING: " . $cl->GetLastWarning();
    }
    print '<pre>';
    //	if(!empty($result["matches"])){
    //		print '<pre>';
    //		foreach ( $result["matches"] as $doc => $docinfo ) {
    print_r($result["matches"]);
    //		}
    //	}
}
Example #16
0
function CsDataUpAttr($ids, $index = '*', $attrs = array('status'), $values = 0, $timeout = 1, $host = 'localhost', $port = 9312)
{
    require C('INTERFACE_PATH') . 'coreseek/api/sphinxapi.php';
    $cl = new SphinxClient();
    $cl->SetServer($host, $port);
    $cl->SetConnectTimeout($timeout);
    foreach ($ids as $id) {
        $cl->UpdateAttributes($index, $attrs, array($id => array($values)));
    }
}
Example #17
0
 private function resetSphinxClient()
 {
     $cl = new SphinxClient();
     $dbConf = Propel::getConfiguration();
     $dsn = $dbConf['datasources']['propel']['connection']['dsn'];
     $sphinxServer = sfConfig::get('sf_sphinx_server');
     $cl->SetServer($sphinxServer, 3312);
     $cl->SetConnectTimeout(1);
     $this->limit = 15;
     $cl->SetArrayResult(true);
     return $cl;
 }
Example #18
0
    $limit = '';
    $res = $Products->SetProductsListOldSearch($where_arr, $limit, $gid, array('order_by' => isset($orderby) ? $orderby : null, 'rel_search' => isset($rel_order) ? $rel_order : null));
    if (!empty($res)) {
        foreach ($res as $k => $r) {
            if ($r['price_mopt'] != 0) {
                $prices[$k] = number_format($r['price_mopt'], 0, ".", "");
            }
        }
    }
    // Поиск Sphinx ============================================
} elseif ($GLOBALS['CONFIG']['search_engine'] == 'sphinx') {
    // Инициализация соединения со Sphinx
    $sphinx = new SphinxClient();
    // $sphinx->SetServer("localhost", 9312);
    $sphinx->SetServer('31.131.16.159', 9312);
    $sphinx->SetConnectTimeout(1);
    $sphinx->SetArrayResult(true);
    $sphinx->setMaxQueryTime(100);
    $sphinx->setLimits(0, 10000);
    $sphinx->SetSortMode(SPH_SORT_RELEVANCE);
    $sphinx->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
    // разбор строки запроса
    if (ctype_digit($query)) {
        $result = $sphinx->Query($query, 'art' . $GLOBALS['CONFIG']['search_index_prefix']);
    } else {
        $words = explode(' ', $query);
        $i = 0;
        foreach ($words as &$w) {
            if (strlen($w) > 2) {
                $sphinx->SetMatchMode(SPH_MATCH_ALL);
                $result = $sphinx->Query('( ' . $w . ' | ' . $w . '* | *' . $w . '* | *' . $w . ' ) ', 'name' . $GLOBALS['CONFIG']['search_index_prefix']);
Example #19
0
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;
    }
}
Example #20
0
 public function run($subject_id, $clean = true, $query_offset = 0, $from, $to)
 {
     $this->load->helper('sphinxapi');
     $this->load->helper('mood');
     // skip if matching_status is "matching"
     $matching_status = $this->custom_model->get_value('subject', 'matching_status', $subject_id);
     if ($matching_status == 'matching') {
         echo "subject is matching";
         return false;
     }
     // flag subject as matching.. do other bot runs this queue.
     $this->db->update('subject', array('matching_status' => 'matching'), array('id' => $subject_id));
     // clear all match record for this subject
     if ($clean) {
         $this->db->delete('matchs', array('subject_id' => $subject_id));
     }
     //
     // begin re-matching this subject
     //
     // get search string from subject_id
     $query = $this->custom_model->get_value('subject', 'query', $subject_id);
     // sphinx init
     $cl = new SphinxClient();
     $q = $query;
     $sql = "";
     $mode = SPH_MATCH_EXTENDED;
     $host = "192.168.1.102";
     $port = 9312;
     $index = "*";
     $groupby = "";
     $groupsort = "@group desc";
     $filter = "group_id";
     $filtervals = array();
     $distinct = "";
     $sortby = "@id ASC";
     $sortexpr = "";
     $offset = $query_offset;
     $limit = 1000000;
     $ranker = SPH_RANK_PROXIMITY_BM25;
     $select = "";
     echo 'limit=' . $limit . ' offset=' . $offset . PHP_EOL;
     //Extract subject keyword from search string
     $keywords = get_keywords($q);
     ////////////
     // do query
     ////////////
     $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 > 1000000 ? $limit : 1000000);
     }
     $cl->SetRankingMode($ranker);
     $res = $cl->Query($q, $index);
     ////////////
     // do Insert to DB
     ////////////
     // Current matching
     $current_matching = array();
     $query_matchs = $this->db->get_where('matchs', array('subject_id' => $subject_id));
     if ($query_matchs->num_rows() > 0) {
         echo PHP_EOL . 'currents matching :' . $query_matchs->num_rows();
         foreach ($query_matchs->result() as $match) {
             $current_matching[] = $match->post_id;
         }
     }
     // set matching date range from-to
     $from = strtotime($from);
     $to = strtotime($to);
     // Search and Update
     if ($res === false) {
         echo "Query failed: " . $cl->GetLastError() . ".\n";
     } else {
         if ($cl->GetLastWarning()) {
             echo "WARNING: " . $cl->GetLastWarning() . "\n\n";
         }
         echo "Query '{$q}' \nretrieved {$res['total']} of {$res['total_found']} matches in {$res['time']} sec.\n";
         if ($res['total'] == 0) {
             echo "no result<br/>\n";
         } else {
             if ($res['total'] > $limit + $offset) {
                 $this->run($subject_id, $limit + $offset);
             } else {
                 echo "Updating...";
                 foreach ($res["matches"] as $k => $docinfo) {
                     //					echo '('.$k.')'.$docinfo["id"]." ";
                     // Reset PHP Timeout to 1min
                     // if found in $current_matching then skip
                     if (in_array($docinfo["id"], $current_matching)) {
                         continue;
                     } else {
                         // else insert new match
                         set_time_limit(60);
                         $post = new Post_model();
                         $post->init($docinfo["id"]);
                         // if post_date is our of range then skip
                         $post_date = strtotime($post->post_date);
                         if ($post_date < $from || $post_date > $to) {
                             continue;
                         }
                         $mood = get_mood($post->body, $keywords);
                         $data = array('post_id' => $post->id, 'subject_id' => $subject_id, 'matching_date' => null, 'sentiment' => $mood, 'by' => 'system', 'system_correct' => $mood, 'system_correct_date' => mdate('%Y-%m-%d %H:%i', time()));
                         $this->db->insert('matchs', $data);
                     }
                 }
             }
         }
     }
     // flag subject as update..
     $data = array('matching_status' => 'update', 'latest_matching' => mdate('%Y-%m-%d %H:%i:%s', time()), 'from' => mdate('%Y-%m-%d %H:%i:%s', $from), 'to' => mdate('%Y-%m-%d %H:%i:%s', $to), 'bot_id' => 0);
     $this->db->update('subject', $data, array('id' => $subject_id));
 }
Example #21
0
 public function run($subject_id, $clean = true, $query_offset = 0, $from, $to)
 {
     $this->load->helper('sphinxapi');
     $this->load->helper('mood');
     // skip if matching_status is "matching"
     $matching_status = $this->custom_model->get_value('subject', 'matching_status', $subject_id);
     if ($matching_status == 'matching') {
         echo "subject is matching";
         return false;
     }
     // flag subject as matching.. do other bot runs this queue.
     //$this->db->update('subject',array('matching_status'=>'matching'),array('id'=>$subject_id));
     // clear all match record for this subject
     $config['hostname'] = "192.168.1.102";
     $config['username'] = "******";
     $config['password'] = "******";
     $config['database'] = "thothconnect";
     $config['dbdriver'] = "mysql";
     $config['dbprefix'] = "";
     $config['pconnect'] = FALSE;
     $config['db_debug'] = TRUE;
     $config['cache_on'] = FALSE;
     $config['cachedir'] = "";
     $config['char_set'] = "utf8";
     $config['dbcollat'] = "utf8_general_ci";
     $thothconnect_db = $this->load->database($config, true);
     $query = $this->db->query("SELECT client_id FROM subject WHERE id = " . $subject_id);
     $row = $query->row();
     $client_id = $row->client_id;
     if ($clean) {
         $thothconnect_db->delete('website_c' . $client_id, array('subject_id' => $subject_id));
         $thothconnect_db->delete('twitter_c' . $client_id, array('subject_id' => $subject_id));
         $thothconnect_db->delete('facebook_c' . $client_id, array('subject_id' => $subject_id));
     }
     //
     // begin re-matching this subject
     //
     // get search string from subject_id
     $query = $this->custom_model->get_value('subject', 'query', $subject_id);
     // sphinx init
     $cl = new SphinxClient();
     $q = $query;
     $sql = "";
     $mode = SPH_MATCH_EXTENDED;
     $host = "192.168.1.102";
     $port = 9312;
     $index = "*";
     $groupby = "";
     $groupsort = "@group desc";
     $filter = "group_id";
     $filtervals = array();
     $distinct = "";
     $sortby = "@id ASC";
     $sortexpr = "";
     $offset = $query_offset;
     $limit = 1000000;
     $ranker = SPH_RANK_PROXIMITY_BM25;
     $select = "";
     echo 'limit=' . $limit . ' offset=' . $offset . PHP_EOL;
     //Extract subject keyword from search string
     $keywords = get_keywords($q);
     ////////////
     // do query
     ////////////
     $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 > 1000000 ? $limit : 1000000);
     }
     $cl->SetRankingMode($ranker);
     $res = $cl->Query($q, $index);
     //$res = true;
     ////////////
     // do Insert to DB
     ////////////
     // Current matching
     $current_matching = array();
     /*$query_matchs = $this->db->get_where('matchs',array('subject_id'=>$subject_id));
     		if($query_matchs->num_rows() > 0)
     		{
     			echo PHP_EOL.'currents matching :'.$query_matchs->num_rows();
     			foreach($query_matchs->result() as $match)
     			{
     				$current_matching[] = $match->post_id;
     			}
     		}*/
     // set matching date range from-to
     $from = strtotime($from);
     $to = strtotime($to);
     // Search and Update
     if ($res === false) {
         echo "Query failed: " . $cl->GetLastError() . ".\n";
     } else {
         if ($cl->GetLastWarning()) {
             echo "WARNING: " . $cl->GetLastWarning() . "\n\n";
         }
         echo "Query '{$q}' \nretrieved {$res['total']} of {$res['total_found']} matches in {$res['time']} sec.\n";
         if ($res['total'] == 0) {
             echo "no result<br/>\n";
         } else {
             if ($res['total'] > $limit + $offset) {
                 $this->run($subject_id, $limit + $offset);
             } else {
                 echo "Updating...";
                 foreach ($res["matches"] as $k => $docinfo) {
                     //					echo '('.$k.')'.$docinfo["id"]." ";
                     // Reset PHP Timeout to 1min
                     // if found in $current_matching then skip
                     if (in_array($docinfo["id"], $current_matching)) {
                         continue;
                     } else {
                         // else insert new match
                         set_time_limit(60);
                         $post = new Post_model();
                         $post->init($docinfo["id"]);
                         // if post_date is our of range then skip
                         $post_date = strtotime($post->post_date);
                         if ($post_date < $from || $post_date > $to) {
                             continue;
                         }
                         $mood = get_mood($post->body, $keywords);
                         //-----------------------------------------------------
                         $subject = $post->get_subject($subject_id);
                         //print_r($subject);
                         if ($post->type == "post" || $post->type == "comment") {
                             $postData = $post->get_post_website($post->id);
                             if ($postData != null) {
                                 $data = array();
                                 $data["post_id"] = $postData->post_id;
                                 $data["post_date"] = $postData->post_date;
                                 $data["title"] = $postData->title;
                                 $data["body"] = $postData->body;
                                 $data["type"] = $postData->type;
                                 $data["author_id"] = $postData->author_id;
                                 $data["author"] = $postData->author;
                                 $data["website_id"] = $postData->website_id;
                                 $data["website_name"] = $postData->website_name;
                                 $data["website_cate_id"] = $postData->website_cate_id;
                                 $data["website_cate"] = $postData->website_cate;
                                 $data["website_type_id"] = $postData->website_type_id;
                                 $data["website_type"] = $postData->website_type;
                                 $data["group_id"] = $subject->group_id;
                                 $data["group"] = $subject->group;
                                 $data["url"] = substr($postData->root_url, 0, -1) . "" . $postData->url;
                                 $data["page_id"] = $postData->page_id;
                                 $data["subject_id"] = $subject->subject_id;
                                 $data["subject_name"] = $subject->subject_name;
                                 $data["mood"] = $mood;
                                 $data["mood_by"] = 'system';
                                 $thothconnect_db->insert("website_c" . $subject->client_id, $data);
                                 $post->insert_post_comment($postData->page_id, $subject->client_id, $thothconnect_db);
                             }
                         } else {
                             if ($post->type == "tweet" || $post->type == "retweet") {
                                 $postData = $post->get_post_twitter($post->id);
                                 if ($postData != null) {
                                     $data = array();
                                     $data["post_id"] = $postData->post_id;
                                     $data["post_date"] = $postData->post_date;
                                     $data["body"] = $postData->body;
                                     $data["type"] = $postData->type;
                                     $data["author_id"] = $postData->author_id;
                                     $data["author"] = $postData->author;
                                     $data["group_id"] = $subject->group_id;
                                     $data["group"] = $subject->group;
                                     $data["tweet_id"] = $postData->tweet_id;
                                     $data["subject_id"] = $subject->subject_id;
                                     $data["subject_name"] = $subject->subject_name;
                                     $data["mood"] = $mood;
                                     $data["mood_by"] = 'system';
                                     $thothconnect_db->insert("twitter_c" . $subject->client_id, $data);
                                 }
                             } else {
                                 if ($post->type == "fb_post" || $post->type == "fb_comment") {
                                     $postData = $post->get_post_facebook($post->id);
                                     if ($postData != null) {
                                         $data = array();
                                         $data["post_id"] = $postData->post_id;
                                         $data["post_date"] = $postData->post_date;
                                         $data["body"] = $postData->body;
                                         $data["type"] = $postData->type;
                                         $data["author_id"] = $postData->author_id;
                                         $data["author"] = $postData->author;
                                         $data["group_id"] = $subject->group_id;
                                         $data["group"] = $subject->group;
                                         $data["facebook_page_id"] = $postData->facebook_page_id;
                                         $data["facebook_page_name"] = $postData->facebook_page_name;
                                         $data["subject_id"] = $subject->subject_id;
                                         $data["subject_name"] = $subject->subject_name;
                                         $data["facebook_id"] = $postData->facebook_id;
                                         $data["parent_post_id"] = $postData->parent_post_id;
                                         $data["likes"] = $postData->likes;
                                         $data["shares"] = $postData->shares;
                                         $data["mood"] = $mood;
                                         $data["mood_by"] = 'system';
                                         $thothconnect_db->insert("facebook_c" . $subject->client_id, $data);
                                     }
                                 }
                             }
                         }
                         /*
                         $data = array(
                         	'post_id'=> $post->id, 
                         	'subject_id' => $subject_id , 
                         	'matching_date' => null,
                         	'sentiment' => $mood,
                         	'by' => 'system',
                         	'system_correct' => $mood,
                         	'system_correct_date' => mdate('%Y-%m-%d %H:%i',time())
                         );						
                         $this->db->insert('matchs',$data);	
                         */
                         //---------------------------------------
                     }
                 }
             }
         }
     }
     // flag subject as update..
     $data = array('matching_status' => 'update', 'latest_matching' => mdate('%Y-%m-%d %H:%i:%s', time()), 'from' => mdate('%Y-%m-%d %H:%i:%s', $from), 'to' => mdate('%Y-%m-%d %H:%i:%s', $to));
     $this->db->update('subject', $data, array('id' => $subject_id));
 }
Example #22
0
 protected function QuerySearch($index, $sort, $order, $port = 9312, $host = 'localhost')
 {
     $sphinx = new SphinxClient();
     $sphinx->SetServer($host, $port);
     $sphinx->SetConnectTimeout(1);
     $sphinx->SetArrayResult(true);
     $sphinx->SetLimits(0, static::LIMIT_SEARCH);
     $sphinx->SetMatchMode(SPH_MATCH_EXTENDED2);
     $sphinx->SetSortMode($order, $sort);
     // Limit results to a certain period
     if (!is_null($this->search_range)) {
         $sphinx->SetFilterRange($sort, time() - $this->search_range, time());
     }
     // Check for multi-query search
     if (is_array($this->search)) {
         foreach ($this->search as $query) {
             if (!empty($query)) {
                 $sphinx->AddQuery($this->FilterSearch($query), $index);
             }
         }
         $result = $sphinx->RunQueries();
     } else {
         $result = $sphinx->Query($this->FilterSearch($this->search), $index);
     }
     if ($result === false) {
         throw new ErrorException('Search failed: ' . $sphinx->GetLastError());
     }
     // Return result
     $ids = array();
     $ids[] = 0;
     // Make IN() valid even if Sphinx returned nothing
     if (is_array($this->search)) {
         // Merge results from multi-query search
         foreach ($result as $r) {
             if (isset($r['matches'])) {
                 foreach ($r['matches'] as $match) {
                     $ids[] = $match['id'];
                 }
             }
         }
     } elseif (isset($result['matches'])) {
         foreach ($result['matches'] as $match) {
             $ids[] = $match['id'];
         }
     }
     return $ids;
 }
Example #23
0
function sphinx_search($keyword)
{
    $fid = 0;
    $daterange = 0;
    $orderby = 'match';
    $page = 1;
    $pagesize = 60;
    global $conf, $time;
    $cl = new SphinxClient();
    $cl->SetServer($conf['sphinx_host'], $conf['sphinx_port']);
    $cl->SetConnectTimeout(3);
    $cl->SetArrayResult(TRUE);
    $cl->SetWeights(array(100, 1, 5));
    // 标题权重100,内容权重1,作者权重10
    $fid && $cl->SetFilter('fid', array($fid));
    $daterange && $cl->setFilterRange('dateline', $time - $daterange * 86400, $time);
    $cl->SetMatchMode(SPH_MATCH_ALL);
    if ($orderby == 'match') {
        $cl->SetSortMode(SPH_SORT_RELEVANCE);
        // 如果不设置,默认按照权重排序!但是TMD是正序!
    } elseif ($orderby == 'timeasc') {
        $cl->SetSortMode(SPH_SORT_ATTR_ASC, 'tid');
    } elseif ($orderby == 'timedesc') {
        $cl->SetSortMode(SPH_SORT_ATTR_DESC, 'tid');
    }
    //$cl->SetSortMode (SPH_SORT_ATTR_DESC, 'tid');	// 如果不设置,默认按照权重排序!但是TMD是正序!
    /*
    $cl->SetMatchMode ( SPH_MATCH_EXTENDED );	//设置模式
    $cl->SetRankingMode ( SPH_RANK_PROXIMITY );	//设置评分模式
    $cl->SetFieldWeights (array('subject'=>100,'message'=>10,'username'=>1));//设置字段的权重,如果area命中,那么权重算2
    $cl->SetSortMode ('SPH_SORT_EXPR','@weight');	//按照权重排序
    */
    // --------------> 优先搜索增量索引
    $newlist = array();
    $forums = array();
    if ($page == 1) {
        $cl->SetLimits(0, $pagesize, 1000);
        // 最大结果集
        $res = $cl->Query($keyword, $conf['sphinx_deltasrc']);
        // * 为所有的索引
        if (!empty($cl->_error)) {
            return xn_error(-1, 'Sphinx 错误:' . $cl->_error);
        }
        if (!empty($res) && !empty($res['total'])) {
            $deltamatch = $res['matches'];
        }
        $res['matches'] && arrlist_change_key($res['matches'], 'id');
        $newlist = array();
        $forums = array();
        foreach ((array) $res['matches'] as $v) {
            if (empty($v['attrs'])) {
                continue;
            }
            if (empty($v['attrs']['fid'])) {
                continue;
            }
            $fid = $v['attrs']['fid'];
            $thread = thread_read($v['attrs']['tid']);
            if (empty($thread)) {
                continue;
            }
            if (stripos($thread['subject'], $keyword) === FALSE) {
                continue;
            }
            $thread['subject'] = str_replace($keyword, '<span class="red">' . $keyword . '</span>', $thread['subject']);
            $newlist[] = $thread;
        }
    }
    // --------------> 再搜索主索引
    $start = ($page - 1) * $pagesize;
    $cl->SetLimits($start, $pagesize, 1000);
    // 最大结果集
    $res = $cl->Query($keyword, $conf['sphinx_datasrc']);
    if (!empty($cl->_error)) {
        return xn_error(-1, 'Sphinx 错误:' . $cl->_error);
    }
    if (empty($res) || empty($res['total'])) {
        $res['matches'] = $deltamatch;
    } else {
        arrlist_change_key($res['matches'], 'id');
    }
    $threadlist = array();
    foreach ((array) $res['matches'] as $v) {
        if (empty($v['attrs'])) {
            continue;
        }
        if (empty($v['attrs']['fid'])) {
            continue;
        }
        $fid = $v['attrs']['fid'];
        $thread = thread_read($v['attrs']['tid']);
        if (empty($thread)) {
            continue;
        }
        $thread['subject'] = str_replace($keyword, '<span class="red">' . $keyword . '</span>', $thread['subject']);
        $threadlist[] = $thread;
    }
    $arrlist = $newlist + $threadlist;
    return $arrlist;
}
 function actionIndex()
 {
     $this->_pathway->addStep('检索下载');
     $type = $this->_context->type;
     if ($type == 1) {
         $this->_pathway->addStep('视频资料');
     } else {
         if ($type == 2) {
             $this->_pathway->addStep('音频资料');
         } else {
             if ($type == 3) {
                 $this->_pathway->addStep('图片资料');
             } else {
                 if ($type == 4) {
                     $this->_pathway->addStep('富媒体资料');
                 } else {
                     $type = 0;
                 }
             }
         }
     }
     $this->_view['type'] = $type;
     require Q::ini('appini/search/sphinxApi');
     $host = Q::ini('appini/search/sphinxHost');
     $port = Q::ini('appini/search/sphinxPort');
     $limit = Q::ini('appini/search/sphinxLimit');
     $level = $this->_view['currentUser']['level_id'];
     $group_id = $this->_view['currentUser']['group_id'];
     $page = intval($this->_context->page);
     if ($page < 1) {
         $page = 1;
     }
     $query = $this->_view['query'] = $this->_context->query;
     $s = new SphinxClient();
     $s->SetServer($host, $port);
     $s->SetConnectTimeout(10);
     $s->SetWeights(array(100, 1));
     if ($type >= 1 && $type <= 4) {
         $s->SetFilter('type', array($type));
     }
     $s->SetFilter('status', array(2));
     //0:新节目;1:待审核;2:已发布;3:打回;4:删除.
     $s->SetFilterRange('level', 0, $level);
     $s->SetLimits(($page - 1) * $limit, $limit, 1000);
     $s->SetArrayResult(true);
     $s->SetMatchMode(SPH_MATCH_EXTENDED);
     //设置匹配模式为Sphinx内部语言表达式
     $s->SetSortMode(SPH_SORT_EXPR, '@id');
     //设置排序模式
     $result = $s->Query("{$query} @groups '(,{$group_id},)|(all)'");
     if ($result) {
         //获得文件
         if (isset($result['matches'])) {
             $ids = array();
             foreach ($result['matches'] as $v) {
                 $ids[] = $v['id'];
             }
             $files = Files::find('id in (?)', $ids)->order('id desc')->getAll();
             $this->_view['files'] = $files;
         }
         $result['start'] = ($page - 1) * $limit + 1 > $result['total'] ? $result['total'] : ($page - 1) * $limit + 1;
         $result['end'] = $result['start'] + $limit - 1 > $result['total'] ? $result['total'] : $result['start'] + $limit - 1;
         $this->_view['result'] = $result;
         //生成页码控制
         $pagination = array();
         $pagination['record_count'] = $result['total'];
         $pagination['page_count'] = ceil($result['total'] / $limit);
         $pagination['first'] = 1;
         $pagination['last'] = $pagination['page_count'];
         if ($pagination['last'] < $pagination['first']) {
             $pagination['last'] = $pagination['first'];
         }
         if ($page >= $pagination['page_count'] + 1) {
             $page = $pagination['last'];
         }
         if ($page < 1) {
             $page = $pagination['first'];
         }
         if ($page < $pagination['last'] - 1) {
             $pagination['next'] = $page + 1;
         } else {
             $pagination['next'] = $pagination['last'];
         }
         if ($page > 1) {
             $pagination['prev'] = $page - 1;
         } else {
             $pagination['prev'] = $pagination['first'];
         }
         $pagination['current'] = $page;
         $pagination['page_size'] = $limit;
         $pagination['page_base'] = 1;
         $this->_view['pagination'] = $pagination;
     }
     //            $categoryId = $this->_context->category_id;
     //            $categoryId = isset($categoryId) ? $categoryId : 1;
     //            $category = Category::find()->getById($categoryId);
     //            $this->_view['category'] = $category;
     //            $categoryIds = Category::getChildrenIds($categoryId);
     //            if(count($categoryIds)){//所有编目文件
     //                // 分页查询内容列表
     //                $page = intval($this->_context->page);
     //                if ($page < 1) $page = 1;
     //                $select = Files::find('category_id in (?) and type=? and upload_username=? and status=2 and (groups=\'\' or groups like \'%,?,%\') and level <= ?', $categoryIds, $type, $this->_view['currentUser']['username'], $this->_view['currentUser']['group_id'], $this->_view['currentUser']['level_id'])->order('upload_at desc');
     //                $select->limitPage($page, 12);
     //                // 将分页信息和查询到的数据传递到视图
     //                $this->_view['pagination'] = $select->getPagination();
     //                $this->_view['files'] = $select->getAll();
     //            }
 }
Example #25
0
 /**
  * 全文搜索
  *
  */
 private function full_search($search_txt)
 {
     $conf = C('fullindexer');
     uk86_import('libraries.sphinx');
     $cl = new SphinxClient();
     $cl->SetServer($conf['host'], $conf['port']);
     $cl->SetConnectTimeout(1);
     $cl->SetArrayResult(true);
     $cl->SetRankingMode($conf['rankingmode'] ? $conf['rankingmode'] : 0);
     $cl->setLimits(0, $conf['querylimit']);
     $matchmode = $conf['matchmode'];
     $cl->setMatchMode($matchmode);
     //可以使用全文搜索进行状态筛选及排序,但需要经常重新生成索引,否则结果不太准,所以暂不使用。使用数据库,速度会慢些
     //		$cl->SetFilter('store_state',array(1),false);
     //		if ($_GET['key'] == 'store_credit'){
     //			$order = $_GET['order'] == 'desc' ? SPH_SORT_ATTR_DESC : SPH_SORT_ATTR_ASC;
     //			$cl->SetSortMode($order,'store_sort');
     //		}
     $res = $cl->Query($search_txt, $conf['index_shop']);
     if ($res) {
         if (is_array($res['matches'])) {
             foreach ($res['matches'] as $value) {
                 $matchs_id[] = $value['id'];
             }
         }
     }
     if ($search_txt != '') {
         $condition['store.store_id'] = array('in', $matchs_id);
     }
     return $condition;
 }
Example #26
0
 function draw()
 {
     //$tbl_source     = "category_bk";
     $tbl_source = "category";
     global $display;
     $keywords = AZLib::getParam('searchKeyword');
     $src_catid = (int) Url::get('sourceCategories');
     $src_l1_catid = 0;
     $src_l2_catid = 0;
     $src_l3_catid = 0;
     if ($src_catid) {
         //Kiểm tra danh mục nguồn
         $src_cat = DB::select("{$tbl_source}", "id={$src_catid}");
         if ($src_cat) {
             if ($src_cat && $src_cat['parent_id']) {
                 //DM cấp 2
                 /*$src_l1_catid = $src_cat['parent_id'];
                 		$src_l2_catid = $src_catid;
                 		*/
                 $src_cat_parent = DB::select("{$tbl_source}", "id={$src_cat['id']}");
                 if (!$src_cat_parent || $src_cat_parent && $src_cat_parent['parent_id']) {
                     //DM cấp 3
                     $src_l1_catid = $src_cat_parent['parent_id'];
                     $src_l2_catid = $src_cat['parent_id'];
                     $src_l3_catid = $src_catid;
                 } else {
                     $src_l1_catid = $src_cat['parent_id'];
                     $src_l2_catid = $src_catid;
                 }
             } else {
                 $src_l1_catid = $src_catid;
             }
         }
     }
     $des_catid = (int) AZLib::getParam('desCategories');
     $search_result = false;
     $items = array();
     $total = 0;
     if ($keywords) {
         //Nếu tìm theo từ khóa
         $q = $keywords;
         $mode = SPH_MATCH_ALL;
         //Init config
         $host = SPHINX_SERVER;
         $port = SPHINX_PORT;
         $index = SPHINX_INDEX;
         $ranker = SPH_RANK_PROXIMITY_BM25;
         $cl = new SphinxClient();
         $cl->SetServer($host, $port);
         $cl->SetConnectTimeout(1);
         $cl->_limit = 50000;
         $cl->_maxmatches = 50000;
         $cl->SetWeights(array(100, 1));
         $cl->SetMatchMode($mode);
         if ($src_l2_catid) {
             $cl->SetFilter('category_id', array($src_catid));
         } elseif ($src_l1_catid) {
             $cl->SetFilter('level_1_catid', array($src_catid));
         }
         //$cl->SetLimits( $offset , $limit, 10000 );
         $cl->SetRankingMode($ranker);
         $cl->SetArrayResult(true);
         $res = $cl->Query($q, $index);
         if ($res && isset($res["matches"])) {
             if (is_array($res["matches"])) {
                 $itemIDs = '';
                 $count = 0;
                 foreach ($res["matches"] as $results) {
                     $itemIDs .= ($itemIDs != '' ? ',' : '') . $results['id'];
                 }
                 if ($itemIDs != '') {
                     //Đếm lại số bản ghi chính xác
                     $sql = 'SELECT count(*) AS totalItem FROM item WHERE id IN(' . $itemIDs . ')';
                     if ($src_catid) {
                         if ($src_l3_catid) {
                             // Nếu tìm kiếm theo từ khóa trong danh mục cấp 3
                             $sql .= ' AND category_id = ' . $src_l3_catid;
                         } elseif ($src_l2_catid) {
                             // Nếu tìm kiếm theo từ khóa trong danh mục nào đó
                             $sql .= ' AND level_2_catid = ' . $src_l2_catid;
                         } elseif ($src_l1_catid) {
                             $sql .= ' AND level_1_catid = ' . $src_l1_catid;
                         }
                     }
                     if ($des_catid) {
                         $sql .= ' AND category_id != ' . $des_catid;
                     }
                     $re = DB::Query($sql);
                     if ($re) {
                         $row = mysql_fetch_assoc($re);
                         $total += (int) $row['totalItem'];
                     }
                     $display->add('itemids', $itemIDs);
                 }
             }
         }
     } elseif ($src_catid) {
         // Nếu giới hạn theo danh mục
         $sql = "SELECT count(*) AS itemTotal FROM item";
         if ($src_l3_catid) {
             $sql .= ' WHERE category_id = ' . $src_l3_catid;
         } elseif ($src_l2_catid) {
             $sql .= ' WHERE level_3_category_id = ' . $src_l2_catid;
         } elseif ($src_l1_catid) {
             $sql .= ' WHERE level_1_catid = ' . $src_l1_catid;
         }
         $re = DB::query($sql);
         if ($re) {
             $row = mysql_fetch_assoc($re);
             $total = $row['itemTotal'];
         }
     }
     $this->beginForm();
     //Build source categories list
     $cat_search_name = '';
     $re = DB::query("SELECT id,name,parent_id ,position,status FROM {$tbl_source} ORDER BY parent_id,position");
     $all_cats = array();
     $all_subcats = array();
     if ($re) {
         while ($cat = mysql_fetch_assoc($re)) {
             if ($cat['parent_id']) {
                 //Là danh mục cấp 2
                 if (isset($all_cats[$cat['parent_id']]) && $all_cats[$cat['parent_id']]['parent_id'] == 0) {
                     //Là danh mục cấp 2
                     $all_subcats[$cat['parent_id']][$cat['id']] = $cat;
                 }
             } else {
                 if (!isset($all_subcats[$cat['id']])) {
                     $all_subcats[$cat['id']] = array();
                 }
             }
             $all_cats[$cat['id']] = $cat;
         }
     }
     $all_top_cat = array();
     $all_top_cat[0] = 'Tất cả các danh mục';
     foreach ($all_subcats as $topid => $subcats) {
         if ($src_catid && $src_catid == $topid) {
             $cat_search_name = $all_cats[$topid]['name'];
         }
         if ($all_cats[$topid]['status'] == 'HIDE') {
             $all_cats[$topid]['name'] .= ' (ẨN)';
         }
         $all_top_cat[$topid] = $all_cats[$topid]['name'];
         foreach ($subcats as $subcat) {
             if ($src_catid && $src_catid == $subcat['id']) {
                 $cat_search_name = $subcat['name'];
             }
             if ($subcat['status'] == 'HIDE') {
                 $subcat['name'] .= ' (ẨN)';
             }
             $all_top_cat[$subcat['id']] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;" . $subcat['name'];
         }
     }
     //print_r($all_top_cat);
     $display->add('msg', $this->showFormSuccesMessages(1));
     $display->add('sourceCategories', $all_top_cat);
     //Build destination categories list
     $re = DB::query("SELECT id,name,parent_id,status,position FROM category ORDER BY parent_id,position");
     $all_cats = array();
     $all_subcats = array();
     $level1_cats = array();
     $level2_cats = array();
     $level3_cats = array();
     if ($re) {
         while ($cat = mysql_fetch_assoc($re)) {
             if ($cat['parent_id']) {
                 //Là danh mục cấp 2 hoặc 3
                 if (isset($all_cats[$cat['parent_id']]) && $all_cats[$cat['parent_id']]['parent_id'] == 0) {
                     //Là danh mục cấp 2
                     $all_subcats[$cat['parent_id']][$cat['id']] = $cat;
                     $cat['max'] = 0;
                     if ($cat['position'] > $level1_cats[$cat['parent_id']]['max']) {
                         $level1_cats[$cat['parent_id']]['max'] = $cat['position'];
                     }
                     $level2_cats[$cat['id']] = $cat;
                 } else {
                     //là danh mục cấp 3
                     if ($cat['position'] > $level2_cats[$cat['parent_id']]['max']) {
                         $level2_cats[$cat['parent_id']]['max'] = $cat['position'];
                     }
                     $level3_cats[$all_cats[$cat['parent_id']]['parent_id']][$cat['parent_id']][$cat['id']] = $cat;
                 }
             } else {
                 $cat['max'] = 0;
                 $level1_cats[$cat['id']] = $cat;
                 if (!isset($all_subcats[$cat['id']])) {
                     $all_subcats[$cat['id']] = array();
                 }
             }
             $all_cats[$cat['id']] = $cat;
         }
     }
     $all_top_cat = array();
     $categories = array();
     foreach ($all_subcats as $topid => $subcats) {
         if ($all_cats[$topid]['status'] == 'HIDE') {
             $all_cats[$topid]['name'] .= ' (ẨN)';
         }
         $categories[$topid] = $all_cats[$topid];
         $all_top_cat[$topid] = $all_cats[$topid]['name'];
         foreach ($subcats as $subcat) {
             if ($subcat['status'] == 'HIDE') {
                 $subcat['name'] .= ' (ẨN)';
             }
             $all_top_cat[$subcat['id']] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;" . $subcat['name'];
             $categories[$subcat['id']] = $subcat;
             if (isset($level2_cats[$subcat['id']]) && $level2_cats[$subcat['id']]['max']) {
                 $subcatsl3 = $level3_cats[$subcat['parent_id']][$subcat['id']];
                 foreach ($subcatsl3 as $subcatl3) {
                     if ($subcatl3['status'] == 'HIDE') {
                         $subcatl3['name'] .= ' (ẨN)';
                     }
                     $all_top_cat[$subcatl3['id']] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;" . $subcatl3['name'];
                     $subcatl3['parent_id'] .= ',' . $subcat['parent_id'];
                     $categories[$subcatl3['id']] = $subcatl3;
                 }
             }
         }
     }
     $display->add('desCategories', $all_top_cat);
     $display->add('desJSONCategories', json_encode($categories));
     $display->add("allrecord", $total);
     $display->add('keywords', $keywords);
     $display->add('cat_search_id', $src_catid);
     $display->add('cat_search_name', $cat_search_name);
     $display->add('category_id', $des_catid);
     $display->output("ManageContentCategory");
     $this->endForm();
 }
Example #27
0
 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;
 }
Example #28
0
<?php

header('Content-Type: text/html;charset="UTF-8"');
if ($_GET) {
    // 关键词
    $keyword = urldecode(trim(strip_tags($_GET['keyword'])));
    if ($keyword) {
        require "sphinxapi.php";
        $cl = new SphinxClient();
        $cl->SetServer('127.0.0.1', 9312);
        $cl->SetConnectTimeout(3);
        $cl->SetArrayResult(true);
        $cl->SetMatchMode(SPH_MATCH_ANY);
        $res = $cl->Query($keyword, "*");
        echo '<pre>';
        print_r($res);
    }
} else {
    echo '<form method="get"><input type="type" name="keyword"><input type="submit" value="商品搜索"></form>';
}
 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}条信息&nbsp;{pageindex}/{pagecount}&nbsp;{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();
     }
 }
Example #30
0
 /**
  * 全文搜索
  *
  */
 protected function full_search($search_txt, $type)
 {
     $conf = C('fullindexer');
     $cl = new SphinxClient();
     $cl->SetServer($conf['host'], $conf['port']);
     $cl->SetConnectTimeout(1);
     $cl->SetArrayResult(true);
     $cl->SetRankingMode($conf['rankingmode'] ? $conf['rankingmode'] : 0);
     $cl->setLimits(0, $conf['querylimit']);
     $matchmode = $conf['matchmode'];
     $cl->setMatchMode($matchmode);
     $res = $cl->Query($search_txt, $conf[$type]);
     if ($res) {
         if (is_array($res['matches'])) {
             foreach ($res['matches'] as $value) {
                 $matchs_id[] = $value['id'];
             }
         }
     }
     return is_array($matchs_id) ? implode(',', $matchs_id) : '';
 }