本文整理汇总了PHP中SphinxClient::SetWeights方法的典型用法代码示例。如果您正苦于以下问题:PHP SphinxClient::SetWeights方法的具体用法?PHP SphinxClient::SetWeights怎么用?PHP SphinxClient::SetWeights使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SphinxClient
的用法示例。
在下文中一共展示了SphinxClient::SetWeights方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _getSphinxClient
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;
}
示例2: fopen
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);
示例3:
}
}
}
}
}
}
}
}
}
////////////
// 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);
示例4: draw
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) {
//.........这里部分代码省略.........
示例5: run
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'] = "root";
$config['password'] = "usrobotic";
$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;
}
//.........这里部分代码省略.........
示例6: sphinx_do_search
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;
}
示例7: search_by_forum
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;
}
示例8: do_query
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;
}
}
示例9: SphinxClient
<?php
require "sphinxapi.php";
$cl = new SphinxClient();
$cl->SetWeights(array(10, 20, 30, 40));
$cl->Query('query');
示例10: sphinx_search
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;
}
示例11: doSearch
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;
}
示例12: sphinx_get_search_link
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;
}
示例13: __construct
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'
);
}
//.........这里部分代码省略.........
示例14: run
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"]." ";
//.........这里部分代码省略.........
示例15: searchAction
/**
* Действие поиск поста:
*/
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;
}