/** * 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='; }
/** * 初始化搜索引擎 */ 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); //匹配模式 }
/** * 初始化搜索引擎 */ 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); //匹配模式 }
public function __construct() { parent::__construct(); // 获取关键字 $this->keyword = t($this->data['keyword']); $this->type = $this->data['type'] ? intval($this->data['type']) : 1; // 分页数 $page = intval($this->data['page']); $page <= 0 && ($page = 1); // 分页大小 $pageSize = 10; // 使用sphinx进行搜索功能 $sphinx = new SphinxClient(); // 配置sphinx服务器信息 $sphinx->SetServer(self::SPHINX_HOST, self::SPHINX_PORT); // 配置返回结果集 $sphinx->SetArrayResult(true); // 匹配结果偏移量 $sphinx->SetLimits(($page - 1) * $pageSize, $pageSize, 1000); // 设置最大搜索时间 $sphinx->SetMaxQueryTime(3); // 设置搜索模式 $sphinx->setMatchMode(SPH_MATCH_PHRASE); $this->sphinx = $sphinx; }
protected function applyCriteria(ESphinxSearchCriteria $criteria) { $this->applyMatchMode($criteria->matchMode); $this->applyRankMode($criteria); if ($criteria->sortMode == ESphinxSort::EXTENDED) { $orders = ''; if ($orderArray = $criteria->getOrders()) { $fields = array(); foreach ($orderArray as $attr => $type) { $fields[] = $attr . ' ' . $type; } $orders = implode(', ', $fields); } $this->applySortMode($criteria->sortMode, $orders); } else { $this->applySortMode($criteria->sortMode, $criteria->getSortBy()); } // apply select if (strlen($criteria->select)) { $this->sphinxClient->SetSelect($criteria->select); } // apply limit if ($criteria->limit) { $this->sphinxClient->SetLimits($criteria->offset, $criteria->limit, $criteria->maxMatches, $criteria->cutOff); } // apply group if ($criteria->groupBy) { $this->sphinxClient->SetGroupBy($criteria->groupBy, $criteria->groupByFunc, $criteria->groupBySort); } if ($criteria->groupDistinct) { $this->sphinxClient->SetGroupDistinct($criteria->groupDistinct); } // apply id range if ($criteria->getIsIdRangeSetted()) { $this->sphinxClient->SetIDRange($criteria->getMinId(), $criteria->getMaxId()); } // apply weights $this->applyFieldWeights($criteria->getFieldWeights()); $this->applyIndexWeights($criteria->getIndexWeights()); $this->applyFilters($criteria->getFilters()); $this->applyRanges($criteria->getRangeFilters()); $this->sphinxClient->SetMaxQueryTime($criteria->queryTimeout !== null ? $criteria->queryTimeout : $this->_queryTimeout); if (VER_COMMAND_SEARCH >= 0x11d) { $this->applyOptions($criteria); } }
/** * sphinx search */ public function sphinxSearch(Request $request, \SphinxClient $sphinx) { $search = $request->get('search'); //sphinx的主机名和端口 //mysql -h 127.0.0.1 -P 9306 $sphinx->SetServer('127.0.0.1', 9312); //设定搜索模式 SPH_MATCH_ALL(匹配所有的查询词) $sphinx->SetMatchMode(SPH_MATCH_ALL); //设置返回结果集为数组格式 $sphinx->SetArrayResult(true); //匹配结果的偏移量,参数的意义依次为:起始位置,返回结果条数,最大匹配条数 $sphinx->SetLimits(0, 20, 1000); //最大搜索时间 $sphinx->SetMaxQueryTime(10); //索引源是配置文件中的 index 类,如果有多个索引源可使用,号隔开:'email,diary' 或者使用'*'号代表全部索引源 $result = $sphinx->query($search, '*'); //返回值说明 total 本次查询返回条数 total_found 一共检索到多少条 docs 在多少文档中出现 hits——共出现了多少次 //关闭查询连接 $sphinx->close(); //打印结果 /*echo "<pre>"; print_r($result); echo "</pre>";exit();*/ $ids = [0]; if (!empty($result)) { foreach ($result['matches'] as $key => $val) { $ids[] = $val['id']; } } $ids = implode(',', array_unique($ids)); $list = DB::select("SELECT * from documents WHERE id IN ({$ids})"); if (!empty($list)) { foreach ($list as $key => $val) { $val->content = str_replace($search, '<span style="color: red;">' . $search . '</span>', $val->content); $val->title = str_replace($search, '<span style="color: red;">' . $search . '</span>', $val->title); } } return view('/sphinx.search')->with('data', array('total' => $result['total'] ? $result['total'] : 0, 'time' => $result['time'] ? $result['time'] : 0, 'list' => $list)); }
header("content-type:application/json"); require_once '../tools/db.php'; require_once '../tools/main.php'; include_once "../tools/sphinxapi.php"; if (isset($_REQUEST["number"]) && intval($_REQUEST["number"]) > 9) { $number = $_REQUEST["number"]; } else { $number = 9; } $pageNum = isset($_REQUEST["page"]) ? intval($_REQUEST["page"]) : 0; $mdb = new MeekroDB(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_TABLE_NAME, DB_PORT, DB_CHARSET); $sphinx = new SphinxClient(); $sphinx->SetServer(SPHINX_HOST, SPHINX_PORT); $sphinx->SetArrayResult(true); $sphinx->SetLimits($pageNum * $number, $number, 5000); $sphinx->SetMaxQueryTime(10); $sphinx->SetMatchMode(SPH_MATCH_EXTENDED2); $sphinx->SetFilter('enabled', array(1)); if (isset($_REQUEST["type"])) { $r1 = $mdb->queryFirstRow("SELECT id, name FROM media_type WHERE name=%s", $_REQUEST["type"]); $type = $r1["id"]; //set type if ($type == "") { echo json_encode(array("result" => null, "status" => false, "error" => "请求的类型不存在")); exit; } $sphinx->SetFilter('type', array($type)); } if (isset($_REQUEST['language'])) { $r2 = $mdb->queryFirstRow("SELECT id, name FROM media_language WHERE name=%s", $_REQUEST["language"]); $language = $r2["id"];
public function setQueryTimeout($timeout) { $this->sphinxClient->SetMaxQueryTime((int) $timeout); }
<?php require "spec/fixtures/sphinxapi.php"; $cl = new SphinxClient(); $cl->SetMaxQueryTime(1000); $cl->Query('query');