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; }
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"); fwrite($file, $client->_reqs[$client->AddQuery("test ")]); fclose($file); $client->SetWeights(array()); // anchor $client->SetGeoAnchor("latitude", "longitude", 10.0, 95.0); $file = fopen("spec/fixtures/data/anchor.bin", "w"); fwrite($file, $client->_reqs[$client->AddQuery("test ")]); fclose($file); $client->ResetFilters(); // rank_mode $client->SetRankingMode(SPH_RANK_WORDCOUNT); $file = fopen("spec/fixtures/data/rank_mode.bin", "w"); fwrite($file, $client->_reqs[$client->AddQuery("test ")]); fclose($file);
} } } } } } } } } //////////// // 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);
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']] = " - " . $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']] = " - " . $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']] = " + " . $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(); }
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)); }
function sphinx_do_search($by_date = false, $start = 0, $count = 50) { global $globals; $start_time = microtime(true); $indices = $_REQUEST['w'] . ' ' . $_REQUEST['w'] . '_delta'; $cl = new SphinxClient(); $cl->SetServer($globals['sphinx_server'], $globals['sphinx_port']); $cl->SetLimits($start, $count); // status, title, tags, url, content $cl->SetWeights(array(0, 4, 2, 1, 1)); $response = array(); $queries = array(); $recorded = array(); $response['rows'] = 0; $response['time'] = 0; if (empty($_REQUEST['words'])) { return $response; } $words_array = explode(" ", $_REQUEST['words']); $words_count = count($words_array); $words = $_REQUEST['words']; if ($_REQUEST['t']) { $max_date = time(); $min_date = intval($_REQUEST['t']); $cl->SetFilterRange('date', $min_date, $max_date); } if ($_REQUEST['h']) { $max_date = time(); $min_date = $max_date - intval($_REQUEST['h']) * 3600; $cl->SetFilterRange('date', $min_date, $max_date); } if ($_REQUEST['w'] == 'links' && $_REQUEST['s']) { $cl->SetFilter('status', array($_REQUEST['s_id'])); } if ($_REQUEST['u']) { $u = new User(); $u->username = $_REQUEST['u']; $u->read(); $cl->SetFilterRange('user', $u->id, $u->id); } if ($_REQUEST['w'] == 'links' && $_REQUEST['p']) { $f = '@' . $_REQUEST['p']; } else { $f = '@*'; } if ($by_date || $_REQUEST['o'] == 'date') { $cl->SetSortMode(SPH_SORT_ATTR_DESC, 'date'); } else { $cl->SetSortMode(SPH_SORT_TIME_SEGMENTS, 'date'); //$cl->SetSortMode (SPH_SORT_RELEVANCE); } $cl->SetMatchMode(SPH_MATCH_EXTENDED2); if ($_REQUEST['p'] == 'url') { $q = $cl->AddQuery("{$f} \"{$words}\"", $indices); } else { $q = $cl->AddQuery("{$f} {$words}", $indices); } array_push($queries, $q); // If there are no boolean opertions, add a new search for ANY of the terms if (!preg_match('/( and | or | [\\-\\+\\&\\|])/i', $words) && $words_count > 1) { $n = 0; foreach ($words_array as $w) { if ($n > 0) { $f .= ' |'; } $f .= " {$w}"; $n++; } $q = $cl->AddQuery($f, $indices); array_push($queries, $q); } $results = $cl->RunQueries(); $n = 0; $response['error'] = $results['error']; foreach ($queries as $q) { $res = $results[$q]; if (is_array($res["matches"])) { $response['rows'] += $res["total_found"]; // $response['time'] += $res["time"]; foreach ($res["matches"] as $doc => $docinfo) { if (!$recorded[$doc]) { $response['ids'][$n] = $doc; $recorded[$doc] = true; $n++; } else { $response['rows']--; } } } } $response['time'] = microtime(true) - $start_time; return $response; }
public function search_by_forum($keyword, $fid, $astars, $start, $pagesize) { //$fid = intval($fid); //$astars = intval($astars); //include FRAMEWORK_PATH.'lib/sphinxapi.class.php'; $cl = new SphinxClient(); $host = '10.0.0.248'; $port = '9312'; $cl->SetServer($host, $port); $cl->SetConnectTimeout(3); $cl->SetArrayResult(true); $cl->SetWeights(array(100, 1)); //匹配所有查询词(默认模式) $cl->SetMatchMode(SPH_MATCH_ALL); //$cl->SetRankingMode ( SPH_RANK_PROXIMITY_BM25); //匹配查询词中的任意一个 //$cl->SetMatchMode(SPH_MATCH_ANY); //将整个查询看作一个词组,要求按顺序完整匹配 //$cl->SetMatchMode(SPH_MATCH_PHRASE); //$cl->SetSortMode(SPH_SORT_EXTENDED, 'fid desc'); //将查询看作一个Sphinx/Coreseek内部查询语言的表达式 //$cl->SetMatchMode(SPH_MATCH_EXTENDED2); $cl->SetSortMode(SPH_SORT_EXTENDED, 'dateline desc'); //$cl->SetFilter('fid', array(2), false ); if ($astars != 'default') { $cl->SetFilter('fid', array($fid), false); $cl->SetFilter('astars', array($astars), false); //$cl->SetFilter ('isindex', array(0), false ); } else { $cl->SetFilter('fid', array($fid), false); } //print_r(123);exit; $cl->SetLimits($start, $pagesize, $pagesize > 1000 ? $pagesize : 1000); $res = $cl->Query($keyword, "kaoder_thread"); return $res; }
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; } }
<?php require "sphinxapi.php"; $cl = new SphinxClient(); $cl->SetWeights(array(10, 20, 30, 40)); $cl->Query('query');
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; }
private function doSearch($q, $page) { global $wgOut; $mode = SPH_MATCH_ALL; $index = 'suggested_titles'; $page_size = 20; $limit = 1000; $ranker = SPH_RANK_PROXIMITY_BM25; $host = 'localhost'; $port = 9312; $cl = new SphinxClient(); $cl->SetServer($host, $port); //$cl->SetConnectTimeout(1); $cl->SetSortMode(SPH_SORT_RELEVANCE); $cl->SetArrayResult(true); $cl->SetWeights(array('wst_title' => 5, 'wst_text' => 2)); $cl->SetMatchMode($mode); $cl->SetRankingMode($ranker); $cl->SetLimits(($page - 1) * $page_size, $page_size, $limit); // don't search w/ leading "how to" if user added it $q_prime = preg_replace('@^\\s*how\\s+to\\s+@i', '', $q); $res = $cl->Query($q_prime, $index); $error = $res === false ? $cl->GetLastError() : ''; $warning = $cl->GetLastWarning(); /*$spelling = $this->getSpellingInfo($q); if ($spelling) { $res['spelling'] = $spelling; } else { $res['spelling'] = ''; }*/ if (count($res['matches']) > 0) { $titles = $this->getInfo($res['matches']); $keys = array_keys($titles); $excerpts = $cl->BuildExcerpts($titles, 'suggested_titles', $q); foreach ($excerpts as $i => $excerpt) { $excerpts[$keys[$i]] = $excerpt; unset($excerpts[$i]); } foreach ($res['matches'] as $i => &$docinfo) { $id = $docinfo['id']; $docinfo['attrs']['excerpt'] = $excerpts[$id]; } } else { $error = wfMsg('search-keywords-not-found', $q); } // construct paging bar $total = (int) ceil(1.0 * $res['total_found'] / $page_size); $paging = array(); if ($page > 1) { $paging[] = 'prev'; } if ($page > 1) { $paging[] = 1; } if ($page >= 5) { $paging[] = '...'; } if ($page >= 4) { $paging[] = $page - 2; } if ($page >= 3) { $paging[] = $page - 1; } $paging[] = $page; if ($page < $total) { $paging[] = $page + 1; } if ($page + 1 < $total) { $paging[] = $page + 2; } if ($page + 2 < $total) { $paging[] = '...'; } if ($page < $total) { $paging[] = 'next'; } $vars = array('results' => $res, 'q' => $q, 'error' => $error, 'warning' => $warning, 'page' => $page, 'page_size' => $page_size, 'paging' => $paging); return $vars; }
function sphinx_get_search_link($by_date = false, $start = 0, $count = 50) { global $globals; $cl = new SphinxClient(); $cl->SetServer($globals['sphinx_server'], $globals['sphinx_port']); $cl->SetLimits($start, $count); // status, title, tags, url, content $cl->SetWeights(array(0, 4, 2, 1, 1)); $response = array(); $queries = array(); $recorded = array(); $response['rows'] = 0; $response['time'] = 0; $words = $_REQUEST['q'] = trim(substr(strip_tags($_REQUEST['q']), 0, 250)); if (empty($words)) { return $response; } if (preg_match('/^ *(\\w+): *(.*)/', $words, $matches)) { $prefix = $matches[1]; $words = $matches[2]; } if (preg_match('/^http[s]*/', $prefix)) { // It's an url search $words = "{$prefix}:{$words}"; $prefix = false; $field = 'url'; } if ($prefix) { switch ($prefix) { case 'date': $by_date = true; break; case 'url': $field = 'url'; break; case 'title': $field = 'title'; break; case 'tag': case 'tags': $field = 'tags'; break; } } $words_count = count(explode(" ", $words)); if ($field) { $cl->SetSortMode(SPH_SORT_ATTR_DESC, 'date'); $cl->SetMatchMode(SPH_MATCH_EXTENDED); $q = $cl->AddQuery("@{$field} \"{$words}\"", 'main delta'); array_push($queries, $q); } elseif ($words_count < 2 || $by_date) { $cl->SetSortMode(SPH_SORT_ATTR_DESC, 'date'); $cl->SetMatchMode(SPH_MATCH_ALL); $q = $cl->AddQuery($words, 'main delta'); array_push($queries, $q); } else { if ($words_count > 2) { $cl->SetMatchMode(SPH_MATCH_PHRASE); //$cl->SetSortMode (SPH_SORT_ATTR_DESC, 'date'); $cl->SetSortMode(SPH_SORT_RELEVANCE); $q = $cl->AddQuery($words, 'main delta'); array_push($queries, $q); } $cl->SetMatchMode(SPH_MATCH_ALL); $cl->SetSortMode(SPH_SORT_ATTR_DESC, 'date'); //$cl->SetSortMode (SPH_SORT_RELEVANCE); $q = $cl->AddQuery($words, 'main delta'); array_push($queries, $q); $cl->SetMatchMode(SPH_MATCH_ANY); $cl->SetSortMode(SPH_SORT_RELEVANCE); $q = $cl->AddQuery($words, 'main delta'); array_push($queries, $q); } $results = $cl->RunQueries(); $n = 0; $response['error'] = $results['error']; foreach ($queries as $q) { $res = $results[$q]; if (is_array($res["matches"])) { $response['rows'] += $res["total_found"]; $response['time'] += $res["time"]; foreach ($res["matches"] as $doc => $docinfo) { if (!$recorded[$doc]) { $response['ids'][$n] = $doc; $recorded[$doc] = true; $n++; } else { $response['rows']--; } } } } return $response; }
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 { $tmp = DatabaseSearch::makeBooleanSQLWhere(DatabaseSearch::fulltextEncode($wildCardString), $this->_db, 'attachment.text'); $this->_WHERE = str_replace(") (", ") AND (", $tmp); /*trace($tmp); $matches=array(); preg_match_all('/"(?:\\\\.|[^\\\\"])*"|\S+/', $wildCardString, $matches); $arrWhere=array(); $arrNew=array(); for($m=0;$m<count($matches[0]);$m++) { $match=$matches[0][$m]; if($match[0]=='"') { $arrNew[]=$match; } else { $tmp=""; for($n=$m;$n<count($matches[0]);$n++) { $match=$matches[0][$n]; if($match[0]=='"' || strtolower($match)=="and" || strtolower($match)=="or") { $m=$n-1; break; } if($tmp==="") { $tmp=$match; } else { $tmp=$tmp." ".$match; } } $tmp='"'.$tmp.'"'; $arrNew[]=$tmp; } } foreach($arrNew as $match) { $arrWhere[] = DatabaseSearch::makeBooleanSQLWhere( DatabaseSearch::fulltextEncode($match), $this->_db, 'attachment.text' ); } $this->_WHERE=implode(" AND ",$arrWhere);*/ } $sbase = new SearchBase(); $arrFilter = $sbase->buildFilter(); $filter = $arrFilter["where"]; $column = $arrFilter["extra_column"]; $join = $arrFilter["extra_join"]; $isSearchAttachment = isset($_REQUEST["bulk_resume"]) && $_REQUEST["bulk_resume"]; if ($isSearchAttachment) { /* How many companies do we have? */ $sql = sprintf("SELECT\r\n count(*) AS count\r\n FROM\r\n attachment\r\n LEFT JOIN candidate\r\n ON attachment.data_item_id = candidate.candidate_id\r\n AND attachment.site_id = candidate.site_id\r\n LEFT JOIN user AS owner_user\r\n ON candidate.owner = owner_user.user_id\r\n %s\r\n WHERE\r\n resume = 1\r\n AND\r\n %s\r\n AND\r\n (attachment.data_item_type = %s OR attachment.data_item_type = %s)\r\n AND\r\n attachment.site_id = %s\r\n AND\r\n (ISNULL(candidate.is_admin_hidden) OR (candidate.is_admin_hidden = 0))\r\n AND\r\n (ISNULL(candidate.is_active) OR (candidate.is_active = 1))\r\n %s\r\n", $join, $this->_WHERE, DATA_ITEM_CANDIDATE, DATA_ITEM_BULKRESUME, $this->_siteID, $filter); } else { $sql = sprintf("SELECT\r\n count(*) AS count\r\n FROM\r\n attachment\r\n LEFT JOIN candidate\r\n ON attachment.data_item_id = candidate.candidate_id\r\n AND attachment.site_id = candidate.site_id\r\n LEFT JOIN user AS owner_user\r\n ON candidate.owner = owner_user.user_id\r\n %s\r\n WHERE\r\n resume = 1\r\n AND\r\n %s\r\n AND\r\n (attachment.data_item_type = %s)\r\n AND\r\n attachment.site_id = %s\r\n AND\r\n (ISNULL(candidate.is_admin_hidden) OR (candidate.is_admin_hidden = 0))\r\n AND\r\n (ISNULL(candidate.is_active) OR (candidate.is_active = 1))\r\n %s\r\n", $join, $this->_WHERE, DATA_ITEM_CANDIDATE, $this->_siteID, $filter); } $rs = $this->_db->getAssoc($sql); /* Pass "Search By Resume"-specific parameters to Pager constructor. */ parent::__construct($rs['count'], $rowsPerPage, $currentPage); }
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)); }
/** * Действие поиск поста: */ public function searchAction(Application $application, Template $template) { $session = Session::getInstance(); $this['form_errors'] = array(); $this['blog_form'] = array(); $template->setParameter('title', 'Поиск постов'); $template->setParameter('board_id', 'news'); if ($_SERVER['REQUEST_METHOD'] == 'POST') { $validator = new ValidatorHelper($_POST); //$validator -> assertExists('query', 'Введите текст запроса'); $validator->assertLength('query', 80, 'Слишком длинный запрос поиска'); $validator->assertExists('order', 'Не указан порядок сортировки'); $validator->assertExists('sort', 'Не указано направление сортировки'); $this['form_errors'] = $validator->getValidationResults(); $this['blog_form'] = $_POST; if ($validator->isValid()) { $search = new SphinxClient(); $search->SetServer('localhost', 3312); switch ($_POST['order']) { default: case 'relevance': $sort = $_POST['sort'] == 'desc' ? '@relevance DESC, created_at DESC, @id DESC' : '@relevance ASC, created_at ASC, @id ASC'; $search->SetSortMode(SPH_SORT_EXTENDED, $sort); break; case 'created_at': $sort = $_POST['sort'] == 'desc' ? SPH_SORT_ATTR_DESC : SPH_SORT_ATTR_ASC; $search->SetSortMode($sort, 'created_at'); break; case 'updated_at': $sort = $_POST['sort'] == 'desc' ? SPH_SORT_ATTR_DESC : SPH_SORT_ATTR_ASC; $search->SetSortMode($sort, 'updated_at'); break; case 'rate': $sort = $_POST['sort'] == 'desc' ? SPH_SORT_ATTR_DESC : SPH_SORT_ATTR_ASC; $search->SetSortMode($sort, 'rate'); break; case 'comments': $sort = $_POST['sort'] == 'desc' ? SPH_SORT_ATTR_DESC : SPH_SORT_ATTR_ASC; $search->SetSortMode($sort, 'comments'); break; } $search->SetWeights(array('link' => 50, 'title' => 40, 'text' => 20, 'text_full' => 10)); $search->SetLimits(0, 60); if (isset($_POST['category']) && $_POST['category'] != 0) { $search->SetFilter('category', array($_POST['category'])); } $result = $search->Query($_POST['query'], 'posts'); if ($result && array_key_exists('matches', $result) && is_array($result['matches'])) { $ids = array_keys($result['matches']); $posts = Blog_BlogPostsModel::GetPostsByIds($ids); $this['posts'] = $posts; $template->setParameter('total', sizeof($result['matches'])); $template->setParameter('total_found', $result['total_found']); } else { $this['form_errors'] = array('По данному запросу ничего не найдено'); } } } EventModel::getInstance()->Broadcast('view_search_post'); return true; }
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); }
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; }
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(); // } }
exit(0); } $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();
/** * Поиск постов: */ public function postSearchAction(Application $application, Template $template) { $template->setParameter('menu', 'posts'); $template->setParameter('submenu', 'post_search'); if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (!empty($_POST['id'])) { $post = Blog_BlogPostsModel::GetPost($_POST['id']); if ($post) { $this['posts'] = array($post); } } else { $search = new SphinxClient(); $search->SetServer('localhost', 3312); $search->SetSortMode(SPH_SORT_EXTENDED, '@relevance DESC, created_at DESC, @id DESC'); $search->SetLimits(0, 60); $search->SetWeights(array('title' => 40, 'text' => 20, 'text_full' => 10)); if (isset($_POST['category'])) { $search->SetFilter('category', $_POST['category']); } $result = $search->Query($_POST['query'], '*'); if (is_array($result['matches'])) { $ids = array_keys($result['matches']); $this['posts'] = Blog_BlogPostsModel::GetPostsByIds($ids); } } } return true; }