sphinx.conf中的配置:

 source indexLocation
{
type = mysql sql_host = 192.168.1.113
sql_user = root
sql_pass = redidai@@2013
sql_db = redidai
sql_port = 3306 # optional, default is 3306 sql_query_pre = SET NAMES utf8
sql_query_pre = SET SESSION query_cache_type=OFF sql_query = \
SELECT a.location_id as id,a.location_id,a.location_name as `location_name`,a.location_name as `name`,a.location_bname,a.attach_id,a.showstatus,a.is_del,a.status,b.area_name as city_name FROM `ts_rdd_location` a LEFT JOIN `ts_rdd_area` b ON a.city_id = b.area_id
#sql_attr_string = name
sql_attr_uint = status
#sql_attr_timestamp = date_added
sql_query_info = SELECT * FROM `ts_rdd_location` WHERE location_id = $id
} index indexLocation
{
source = indexLocation
path = /var/lib/sphinx/test1
docinfo = extern
mlock = 0 #缓存数据内存锁定
morphology = none #形态学(对中文无效)
min_word_len = 2 #索引的词最小长度
charset_type = utf-8
min_prefix_len = 0 #最小前缀
html_strip = 1
ngram_len = 1 #对于非字母型数据的长度切割
ngram_chars = U+3000..U+2FA1F #则会对每个中文,英文字词进行分割,速度会慢 #字符表,注意:如使用这种方式,则sphinx会对中文进行单字切分,即进行字索引。
#若要使用中文分词,必须使用其他分词插件如 coreseek,sfc
charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
} indexer
{
mem_limit = 32M
} searchd
{
listen = 9312
#listen = 9306:mysql41
log = /var/log/sphinx/searchd.log
query_log = /var/log/sphinx/query.log
read_timeout = 5
max_children = 30
pid_file = /var/run/sphinx/searchd.pid
max_matches = 1000
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
compat_sphinxql_magics = 0
#workers = threads # for RT to work
binlog_path = /var/lib/sphinx
}

php代码:

 <?php
class SearchAction extends AdministratorAction { public $sphinxClient = null; public function __construct() {
parent::__construct(); require_once SITE_PATH . '/sphinxapi.php'; if ($this->sphinxClient == null) {
$this->sphinxClient = new SphinxClient ();
} $this->sphinxClient->SetServer('192.168.0.0', 9312);
$this->sphinxClient->open();
} public function QLocation($params = array()) {
$searchWord = ''; if (isset($params['keyword'])) {
$searchWord = $params['keyword'];
} $result = $this->suggestHandle($this->sphinxClient, $searchWord); $nodeResult = array(); if ($result['total'] > 0) {
$matches = array(); foreach($result['matches'] as $mvale) {
$matches[] = $mvale['id'];
} $matches_id = implode(",", $matches); $locationModel = D('RddLocation', 'admin');
$list = $locationModel->getLocationListByID($matches_id);
$nodeResult = $list;
} print_r($nodeResult);exit; return $nodeResult;
} public function suggestHandle($spx, $searchWord = ''){
//$spx->SetSortMode(SPH_SORT_EXTENDED, "@weight DESC, larea DESC, is_recommend DESC, like_count DESC");
$spx->SetSortMode(SPH_SORT_EXTENDED, "@weight DESC");
$spx->ResetFilters();
//$cityId = filter_input(INPUT_GET, 'area', FILTER_SANITIZE_STRING);
//$spx->SetFilter('showstatus', array('Y'));
//$spx->SetFilter('is_del', array('0'));
$spx->SetFilter('status', array(1));
//$result = $spx->Query($searchWord, 'plan14_location,delta_plan14_location');
$spx->SetArrayResult(true);
$result = $spx->Query($searchWord, 'indexLocation');
//var_dump($spx);
return $result;
} }

调用:

$searchAction = new SearchAction();
$list = $searchAction->QLocation(array('keyword' => $name));
05-08 15:31