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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM