使用coreseek完成php查詢

來源:互聯網
上載者:User

標籤:names   iss   foreign   美食   ret   oca   div   html_   primary   

本文整理了如何使用php進行查詢,並整理csft.conf的設定檔。

 


查看服務狀態
ps -ef|grep searchd

##以下為正常開啟搜尋服務時的提示資訊:(csft-4.0版類似)
/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft.conf


##如要停止搜尋服務,請使用
/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft.conf --stop


##如要已啟動服務,要更新索引,請使用
/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft.conf --all --rotate

 

1,安裝(略)

2,匯入mysql資料庫:

/*Navicat MySQL Data TransferDate: 2017-07-02 13:10:34*/SET FOREIGN_KEY_CHECKS=0;-- ------------------------------ Table structure for documents-- ----------------------------DROP TABLE IF EXISTS `documents`;CREATE TABLE `documents` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `group_id` int(11) NOT NULL,  `date_added` datetime NOT NULL,  `title` varchar(255) CHARACTER SET utf8 DEFAULT NULL,  `content` text NOT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4;-- ------------------------------ Records of documents-- ----------------------------INSERT INTO `documents` VALUES (‘1‘, ‘1‘, ‘2017-06-06 21:45:58‘, ‘中國‘, ‘中國真美,地大物博‘);INSERT INTO `documents` VALUES (‘2‘, ‘1‘, ‘2017-06-06 21:45:58‘, ‘中國美食‘, ‘台北小吃,各地美食‘);INSERT INTO `documents` VALUES (‘3‘, ‘2‘, ‘2017-06-06 21:45:58‘, ‘美女之家‘, ‘美女之國‘);INSERT INTO `documents` VALUES (‘4‘, ‘2‘, ‘2017-06-06 21:45:58‘, ‘hello‘, ‘this is to test groups‘);

 

3,建立索引:

/usr/local/coreseek/etc/csft.conf的配置如下:

source src1{        type = mysql        sql_host = 127.0.0.1        sql_user =root        sql_pass =         sql_db = test        sql_port = 3306        sql_query_pre = SET NAMES utf8        sql_query        =         SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content         FROM documents        sql_attr_uint        = group_id        sql_attr_timestamp = date_added        sql_query_info_pre = SET NAMES utf8}index src1{        source = src1        path = /usr/local/coreseek/var/data/test1        docinfo = extern        mlock =0        morphology = none        min_word_len =1        html_strip =0        charset_type = zh_cn.utf-8        charset_dictpath = /usr/local/mmseg3/etc/        #charset-table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F        #ngram_len = 1        #ngram_chars = U+3000..U+2FA1F}searchd{        listen = 9312        listen = 9306:mysql41        read_timeout =5        max_children = 30        max_matches = 1000        seamless_rotate = 0        preopen_indexes = 0        unlink_old = 1        pid_file = /usr/local/coreseek/var/log/searchd.pid        log = /usr/local/coreseek/var/log/searchd.log        query_log = /usr/local/coreseek/var/log/query.log}


 

4,使用php進行查詢

<?php$t = new SphinxSearch();$t->index();class SphinxSearch{    public function index()    {        header("Content-type: text/html; charset=utf-8");        require("sphinxapi.php");        define(‘INDEX_SRC1‘, ‘src1‘);        $cl = new SphinxClient();        $cl->SetServer("127.0.0.1", 9312);        $cl->SetArrayResult(true);        $keyword = "hello";        $result = $cl->query($keyword, INDEX_SRC1);        $matches = isset($result[‘matches‘]) ? $result[‘matches‘] : ‘‘;        if (is_array($matches)) {            foreach ($matches as $v) {                $ids[] = $v[‘id‘];            }        } else {            print("<pre>");            print $cl->GetLastError();            print $cl->GetLastWarning();            //print "沒找到了親~";            print("<pre>");            return;        }        $ids = implode(‘,‘, $ids);//拿著id ,拉庫        $link = mysql_connect(‘127.0.0.1‘, ‘root‘, ‘password‘) or die(‘mysql link fail!‘);        mysql_select_db(‘test‘, $link);        mysql_query("SET NAMES UTF8");        $sql = "select * from documents where id in({$ids})";        $result = mysql_query($sql, $link);        $data = array();        while ($row = mysql_fetch_assoc($result)) {            $data[] = $row;        }        mysql_close($link);// 關鍵字高亮        $p_titles = array();        $p_contents = array();        $newData=array();        foreach ($data as $key => $value) {            $newData[$key][‘title‘]=$this->makeHighLight($keyword,$value[‘title‘]);            $newData[$key][‘content‘]=$this->makeHighLight($keyword,$value[‘content‘]);        }        var_dump($newData);    }    public function makeHighLight($keyword,$param)    {        $param = str_replace($keyword,                "<font style=‘color:#FD2C3F;‘>" . $keyword . "</font>", $param);        return $param;    }}?>

 

使用coreseek完成php查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.