sphinx安装及使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wt1286331074/article/details/82778429

参考:https://ke.qq.com/webcourse/index.html#cid=309238&term_id=100366592&taid=2108644259051510(腾讯课堂)
http://www.php.cn/code/24992.html(php中文网)
https://blog.csdn.net/lzw5240/article/details/43451587(主旨思想)

首先用sphinx有两种方法,一种是php装扩展,一种是调用sphinx服务,它提供很丰富的接口所以我是用的后者。
然后还要注意网上大部分都是2.*的sphinx教程需要安装编译和make,然后3.0后就是直接下载安装使用了。

首先直接去官网下载,我找的是3.1的,然后重点来了,复制etc下面的sphinx.mini.conf.dist复制到bin下面不然会报错。具体如下

 yum install mariadb-devel postgresql-devel unixODBC-devel libmysqlclient18 libmysqlclient-dev libmysqlcppconn7 libmysqlcppconn-dev
#
# Minimal Sphinx configuration sample (clean, simple, functional)
#

source question
{
	type			= mysql

	sql_host		= localhost
	sql_user		= root
	sql_pass		= 456123wt
	sql_db			= blog
	sql_port		= 3306	# optional, default is 3306

	sql_query		= SELECT questionid,question,questionanswer as attr_questionanswer,questionid as attr_questionid,question as attr_question from x2_questions
#	sql_attr_string 	= attr_question
#	sql_attr_string		= attr_questionanswer
	sql_field_string	= attr_question
	sql_attr_uint		= attr_questionid
	#sql_attr_timestamp	= date_added
}


index questionindex
{
	source			= question
	path			= /var/data/questionindex
#	infix_fields		= attr_questionanswer,attr_question
}


#index testrt
#{
#	type			= rt
#	rt_mem_limit		= 128M
#
#	path			= /var/data/testrt
#
#	rt_field		= title
#	rt_field		= content
#	rt_attr_uint		= gid
#}


indexer
{
	mem_limit		= 128M
}


searchd
{
	listen			= 9312
	listen			= 9306:mysql41
	log			= /var/log/searchd.log
	query_log		= /var/log/query.log
	read_timeout		= 5
	max_children		= 30
	pid_file		= /var/log/searchd.pid
	seamless_rotate		= 1
	preopen_indexes		= 1
	unlink_old		= 1
	workers			= threads # for RT to work
	binlog_path		= /var/data
}

配置好了后用indexer生成索引命令是:

indexer --all  	// 生成全部索引
// 或者
./indexer -c sphinx.conf questionindex  // 生成一个索引

然后进入api文件夹复制cp sphinxapi.php /usr/local/nginx/html/blog/public/sphinx
新建个文件try.php内容如下:

<?php
header ( 'Content-Type: text/html;charset="UTF-8"' );
if ($_GET) {
    // 关键词
    $keyword = urldecode ( trim ( strip_tags ( $_GET ['keyword'] ) ) );
    if ($keyword) {
        // 包含Sphinx的api文件
        require_once 'sphinxapi.php';
        // sphinx服务器地址
        $server = '127.0.0.1';
        // 端口
        $port = 9312;
        // 索引名 为*时表示搜索所有索引,多个用逗号
        $indexName = 'questionindex';
        // 分页页码
        $page = intval ( $_GET ['page'] ) > 1 ? intval ( $_GET ['page'] ) : 1;
        // 每页显示的数量
        $pageSize = 30;
        $sphinx = new SphinxClient ();
        // 建立连接
        $sphinx->SetServer ( $server, $port );
        $sphinx->SetArrayResult ( true );// 设置返回为数组
        // 连接超时时间(非常必要,比如sphinx服务器挂了等异常情况) 单位为s,秒
        $sphinx->SetConnectTimeout ( 3 );
        // 最大查询时间 单位为ms,毫秒
        $sphinx->SetMaxQueryTime ( 2000 );
        // 按分页取结果
        $sphinx->SetLimits ( ($page - 1) * $pageSize, $pageSize ); //第一个参数为offset,第二个参数为limit
        // 模式
        // $sphinx->SetMatchMode(SPH_MATCH_EXTENDED);
//        $sphinx->SetFilter('attr_questionid', array(3262,2), false);
        // 取到的原始数据
        $orgDatas = $sphinx->Query ( $keyword, $indexName );

        // 调试用,如果有错误的话,可以打印$errors的值
        $errors = $sphinx->GetLastError ();
        echo "<pre>";
        print_r($orgDatas);die;
//        var_dump ( $errors );

        // 下面是对结果的处理
        $datas = array('goods'=>array(),'total'=>0);
        if ($orgDatas['total'] > 0) {
            $datas['total'] = $orgDatas['total'];
            foreach ($orgDatas['matches'] AS $val) {
            $val['attrs']['goods_id'] = $val['attrs']['goods_id_new'];
            unset($val['attrs']['goods_id_new']);
            $datas['goods'][] = $val['attrs'];
        }
        }
        var_dump($datas);
      
    }
} else {
    echo '<form method="get"><input type="type" name="keyword"><input type="submit" value="商品搜索"></form>';
}
?>

打印结果
在这里插入图片描述

最后一般的sphinx比较倾向用于十几万条的查询,查出来的id集合然后到mysql里面再in查询下就ok了,另外sphinx支持分页,注意了。

猜你喜欢

转载自blog.csdn.net/wt1286331074/article/details/82778429
今日推荐