sphinx3.1.1 window php入门实例

1.下载地址

http://sphinxsearch.com/downloads/current/

 

2.解压文件放到盘内

3.根目录创建 data 和 log 文件夹

 

4.复制 etc目录下的 min.conf 到 bin目录 并修改成 后缀.conf 文件

5.修改.conf内容

#
# Minimal Sphinx configuration sample (clean, simple, functional)
#
source src1
{
	type			= mysql
	sql_host		= 127.0.0.1
	sql_user		= root
	sql_pass		= root
	sql_db			= test
	sql_port		= 3306	# optional, default is 3306
	sql_query_pre	= SET NAMES utf8
	sql_query		= \
		SELECT id,question \
		FROM questions

	#sql_attr_uint		= group_id
	#sql_attr_timestamp	= date_added
}
index test1
{
	source			= src1
	path			= E:/sphinx-3.1.1/data/test1
	ngram_len = 1
	ngram_chars = U+3000..U+2FA1F
}

indexer
{
	mem_limit		= 128M
}

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

6.生成文件

indexer --config demo.conf test1

 

7.开启sphinx服务

searchd --config demo.conf

 8.mysql 查询 创建的数据

mysql -h127.0.0.1 -P9306

9.PHP查询数据

<?php
$t1 = microtime(true);
require("sphinxapi.php");
$sphinx = new SphinxClient();
$sphinx->setServer('127.0.0.1',9312);
$k = '酶'; //搜索的关键字
$sphinx->_limit = 100000;
$res = $sphinx->Query($k,'test1'); 
$ids = array_keys($res['matches']);
$ids = implode(',', $ids);

$m = mysqli_connect('127.0.0.1','root','root','test','3306');
mysqli_query($m,'set names utf8');
$sql = "select * from questions where id in($ids)";
$res = mysqli_query($m,$sql);
while ($row=mysqli_fetch_assoc($res)) {
	$data[] = $row;
}
$_arr = [];
foreach ($data as $key => $value) {
	$val = str_replace($k, "<font color='red'>{$k}</font>", $value['question']);
	$arr['id'] = $value['id'];
	$arr['question'] = $val;
	$_arr[] = $arr;
}
$t2 = microtime(true);
echo '耗时'.round($t2-$t1,3).'秒';
echo '<pre>';
var_dump($_arr);

10.效果

11.对比mysql查询

 

posted @ 2019-06-03 13:41  大智如蠢  阅读(1122)  评论(1编辑  收藏  举报