1. 程式人生 > >使用coreseek完成php查詢

使用coreseek完成php查詢

key extern 如何使用 reat tab navi true 啟動 nbsp

本文整理了如何使用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 Transfer

Date: 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查詢