windows+sphinx+php 安装+配置+使用

时间:2021-11-14 19:35:10

本文将讲述:sphinx在windows下的安装配置;sphinx与PHP的实时索引使用;sphinx服务加入windows服务。

参考手册:http://www.coreseek.cn/docs/coreseek_4.1-sphinx_2.0.1-beta.html

一、sphinx在windows下的安装配置

1.下载地址:

http://sphinxsearch.com/downloads/    尽量下高一点的版本,我用的Sphinx 2.0.6-release,

下载后解压到,f:/sphinx(根据你的情况安装).

将sphinx.conf.in拷贝重名到/bin/sphinx.conf

新建:f:/sphinx/data 文件夹

新建:f:/sphinx/log 文件夹

2.修改配置文件(详细配置见http://blog.csdn.net/design321/article/details/8895608)

#
# Minimal Sphinx configuration sample (clean, simple, functional)
#

source src1
{

#数据库信息
type = mysql

sql_host = localhost
sql_user = root
sql_pass = 123456
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 = SELECT * FROM documents WHERE id=$id
}


index test2
{
source = src1
path = f:/sphinx/data/test1 //新建的data目录
docinfo = extern
charset_type = utf-8
min_prefix_len = 0
min_infix_len = 0
min_word_len = 1
ngram_len = 1
ngram_chars = U+3000..U+2FA1F

charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
}


indexer
{
mem_limit = 32M
}


searchd
{
port = 3312
log =f:/sphinx/log/searchd.log #新建的log目录
#query_log = F:/sphinx/log/query.log #新建的log目录
read_timeout = 5
max_children = 30
pid_file = f:/sphinx/log/searchd.pid #新建的log目录
max_matches = 1000
preopen_indexes = 0
unlink_old = 1
seamless_rotate = 0
}
# eof


配置完成


3.导入测试数据到mysql

将example.sql文件在test数据库执行。

注意,导入的数据非utf8格式,需要自已去数据库改一下字条集。


4.建立索引

F:\sphinx\bin>indexer.exe --config f:/sphinx/bin/sphinx.conf --all


Sphinx 2.0.6-release (r3473)
Copyright (c) 2001-2012, Andrew Aksyonoff
Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphinxsearch.com)

using config file 'f:/sphinx/bin/sphinx.conf'...
indexing index 'test2'...
collected 3 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 3 docs, 138 bytes
total 0.011 sec, 12402 bytes/sec, 269.61 docs/sec
total 2 reads, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg
total 9 writes, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg


5.查找

F:\sphinx\bin>search.exe test


Sphinx 2.0.6-release (r3473)
Copyright (c) 2001-2012, Andrew Aksyonoff
Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphinxsearch.com)

using config file './sphinx.conf'...
index 'test2': query 'test ': returned 3 matches of 3 total in 0.000 sec

displaying matches:
1. document=2, weight=2252, group_id=1, date_added=Mon May 06 18:43:15 2013
        id=2
        group_id=1
        group_id2=6
        date_added=2013-05-06 18:43:15
        title=test two
        content=?? this is my test document number two
2. document=3, weight=1319, group_id=2, date_added=Mon May 06 18:43:15 2013
        id=3
        group_id=2
        group_id2=7
        date_added=2013-05-06 18:43:15
        title=another doc
        content=this is another group ?? test
3. document=4, weight=1319, group_id=2, date_added=Mon May 06 18:43:15 2013
        id=4
        group_id=2
        group_id2=8
        date_added=2013-05-06 18:43:15
        title=doc number four
        content=this is to test groups ??

words:
1. 'test': 3 documents, 4 hits


得到以上内容,安装成功。

二、让PHP 能使用sphinx

1.启动sphinx服务

F:\sphinx\bin>searchd.exe


Sphinx 2.0.6-release (r3473)
Copyright (c) 2001-2012, Andrew Aksyonoff
Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphinxsearch.com)

using config file './sphinx.conf'...
WARNING: compat_sphinxql_magics=1 is deprecated; please update your application
and config
listening on all interfaces, port=3312
precaching index 'test2'
precached 1 indexes in 0.002 sec
binlog: replaying log ./binlog.001

2,将F:\sphinx\api\sphinxapi.php拷贝到apach下的网站目录,以供调用.

3.利用现有索引的简单示例

<?php
require 'sphinxapi.php';
$s = new SphinxClient();
$s->SetServer('127.0.0.1',3312); //服务器名,sphinx端口号
$result = $s->Query('test','test2'); //关键词,索引名(与配置文件里一致,为*时表示全部)
echo '<pre>';
var_dump($result);
echo '<pre>';
?>



4.创建实时索引的示例

  

<?php    
require 'sphinxapi.php';
$keyword='中文';
$sphinx=new SphinxClient();
$sphinx->SetServer("localhost",3312);
$sphinx->SetMatchMode(SPH_MATCH_ANY);
//$sphinx->setLimits(0,0); //偏移量
$result=$sphinx->query("$keyword","test2");
//echo "<pre>";
//print_r($result);
//echo "</pre>";
$ids=join(",",array_keys($result['matches']));
mysql_connect("localhost","root","123456");
mysql_select_db("test");
$sql="select * from documents where id in({$ids})";
mysql_query("set names utf8");
$rst=mysql_query($sql);
$opts=array(
"before_match"=>"<button style='font-weight:bold;color:#f00'>",
"after_match"=>"</button>"
);

while($row=mysql_fetch_assoc($rst)){
$rst2=$sphinx->buildExcerpts($row,"main",$keyword,$opts);
echo '<pre>';
var_dump($row);
echo '';
/* echo "第{$rst2[0]}篇贴子<br>";
echo "标题: {$rst2[1]}<br>";
echo "内容: {$rst2[2]}<br>";
echo "<hr>"; */
}

三、将sphinx服务添加到windows服务

F:\sphinx\bin>searchd.exe --install -c f:\sphinx\bin\sphinx.conf --servicename s


phinx
Sphinx 2.0.6-release (r3473)
Copyright (c) 2001-2012, Andrew Aksyonoff
Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphinxsearch.com)

Installing service...
Service 'sphinx' installed successfully.

F:\sphinx\bin>net start sphinx


sphinx 服务正在启动 .
sphinx 服务已经启动成功。


F:\sphinx\bin>net stop sphinx


sphinx 服务正在停止..

sphinx 服务已成功停止。


F:\sphinx\bin>sc delete sphinx

[SC] DeleteService SUCCESS




end.