sphinx的简单实例

sphinx.conf中的配置:

 1 source indexLocation
 2 {
 3     type            = mysql
 4 
 5     sql_host        = 192.168.1.113
 6     sql_user        = root
 7     sql_pass        = redidai@@2013
 8     sql_db            = redidai
 9     sql_port        = 3306    # optional, default is 3306
10     
11     sql_query_pre = SET NAMES utf8
12     sql_query_pre = SET SESSION query_cache_type=OFF
13     
14     sql_query        = \
15         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
16     #sql_attr_string =  name
17     sql_attr_uint        = status
18     #sql_attr_timestamp    = date_added
19     sql_query_info        = SELECT * FROM `ts_rdd_location`  WHERE location_id = $id
20 }
21 
22 
23 index indexLocation
24 {
25     source            = indexLocation
26     path            = /var/lib/sphinx/test1
27     docinfo            = extern
28     mlock          = 0                       #缓存数据内存锁定
29     morphology     = none                    #形态学(对中文无效)
30     min_word_len   = 2                       #索引的词最小长度
31     charset_type   = utf-8
32     min_prefix_len = 0                       #最小前缀
33     html_strip     = 1
34     ngram_len      = 1                       #对于非字母型数据的长度切割
35     ngram_chars    = U+3000..U+2FA1F         #则会对每个中文,英文字词进行分割,速度会慢
36 
37     #字符表,注意:如使用这种方式,则sphinx会对中文进行单字切分,即进行字索引。
38     #若要使用中文分词,必须使用其他分词插件如 coreseek,sfc
39     charset_table  = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
40 }
41 
42 
43 
44 
45 indexer
46 {
47     mem_limit        = 32M
48 }
49 
50 
51 searchd
52 {
53     listen                  = 9312
54     #listen            = 9306:mysql41
55     log            = /var/log/sphinx/searchd.log
56     query_log        = /var/log/sphinx/query.log
57     read_timeout        = 5
58     max_children        = 30
59     pid_file        = /var/run/sphinx/searchd.pid
60     max_matches        = 1000
61     seamless_rotate        = 1
62     preopen_indexes        = 1
63     unlink_old        = 1
64     compat_sphinxql_magics = 0
65     #workers            = threads # for RT to work
66     binlog_path        = /var/lib/sphinx
67 }

php代码:

 1 <?php
 2 class SearchAction extends AdministratorAction {
 3 
 4     public $sphinxClient = null;
 5 
 6     public function __construct() {
 7         parent::__construct();
 8 
 9         require_once SITE_PATH . '/sphinxapi.php';
10 
11         if ($this->sphinxClient == null) {
12             $this->sphinxClient = new SphinxClient ();
13         }
14 
15         $this->sphinxClient->SetServer('192.168.0.0', 9312);
16         $this->sphinxClient->open();
17     }
18 
19     public function QLocation($params = array()) {
20         $searchWord = '';
21         
22         if (isset($params['keyword'])) {
23             $searchWord = $params['keyword'];
24         }
25         
26         $result = $this->suggestHandle($this->sphinxClient, $searchWord);
27 
28         $nodeResult = array();
29 
30         if ($result['total'] > 0) {
31             $matches = array();
32             
33             foreach($result['matches'] as $mvale) {
34                 $matches[] = $mvale['id'];
35             }
36             
37             $matches_id = implode(",", $matches);
38             
39             $locationModel = D('RddLocation', 'admin');
40             $list = $locationModel->getLocationListByID($matches_id);
41             $nodeResult = $list;
42         }
43         
44         print_r($nodeResult);exit;
45 
46         return $nodeResult;
47     }
48 
49     public function suggestHandle($spx, $searchWord = ''){
50         //$spx->SetSortMode(SPH_SORT_EXTENDED, "@weight DESC, larea DESC, is_recommend DESC, like_count DESC");
51         $spx->SetSortMode(SPH_SORT_EXTENDED, "@weight DESC");
52         $spx->ResetFilters();
53         //$cityId = filter_input(INPUT_GET, 'area', FILTER_SANITIZE_STRING);
54         //$spx->SetFilter('showstatus', array('Y'));
55         //$spx->SetFilter('is_del', array('0'));
56         $spx->SetFilter('status', array(1));
57         //$result = $spx->Query($searchWord, 'plan14_location,delta_plan14_location');
58         $spx->SetArrayResult(true);
59         $result = $spx->Query($searchWord, 'indexLocation');
60         //var_dump($spx);
61         return $result;
62     }
63 
64 }

调用:

$searchAction = new SearchAction();
        $list = $searchAction->QLocation(array('keyword' => $name));

 

posted @ 2013-11-18 10:26  幻星宇  阅读(648)  评论(0编辑  收藏  举报