码迷,mamicode.com
首页 > Web开发 > 详细

使用coreseek完成php查询

时间:2017-07-02 13:47:02      阅读:234      评论:0      收藏:0      [点我收藏+]

标签: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 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查询

标签:names   iss   foreign   美食   ret   oca   div   html_   primary   

原文地址:http://www.cnblogs.com/ziyiang/p/7105771.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!