下载安装
wget http://sphinxsearch.com/files/sphinx-2.3.1-beta.tar.gz
tar zxvf sphinx-2.3.1-beta.tar.gz
cd sphinx-2.3.1-beta
./configure --prefix=/usr/local/sphinx –with-mysql
make
sudo make install
测试数据:
配置
cd /usr/local/sphinx/etc
sudo cp sphinx.conf.dist sphinx.conf
vim sphinx.conf
最精简配置
source src_doc
{
type = mysql
sql_host = localhost
sql_user = xxx
sql_pass = xxx
sql_db = xxx
sql_sock = /var/lib/mysql/mysql.sock
sql_query_pre = SET NAMES utf8
sql_query_pre = SET SESSION query_cache_type=OFF
sql_query = SELECT id,title,intro FROM kb_doc
sql_attr_uint = id
sql_field_string = title
sql_field_string = intro
sql_ranged_throttle = 0
}
index index_doc
{
source = src_doc
path = /usr/local/sphinx/var/data/doc
docinfo = extern
dict = keywords
mlock = 0
morphology = none
min_word_len = 1
ngram_len = 1
ngram_chars = U+3000..U+2FA1F
html_strip = 0
}
indexer
{
mem_limit = 256M
}
searchd
{
listen = 9312
listen = 9306:mysql41
log = /usr/local/sphinx/var/log/searchd.log
query_log = /usr/local/sphinx/var/log/query.log
read_timeout = 5
client_timeout = 300
max_children = 30
persistent_connections_limit = 30
pid_file = /usr/local/sphinx/var/log/searchd.pid
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
mva_updates_pool = 1M
max_packet_size = 8M
max_filters = 256
max_filter_values = 4096
max_batch_queries = 32
}
SHOW VARIABLES LIKE "sock%";
启动服务
cd /usr/local/sphinx/bin
sudo ./indexer --all --rotate
sudo ./searchd
searchd 开启端口:
listening on all interfaces, port=9312
listening on all interfaces, port=9306
测试
<?php
header("content-type:text/html;charset=utf8");
include'./sphinxapi.php';
$sphinx=new SphinxClient();
$sphinx->SetServer('127.0.0.1',9312);
$res=$sphinx->Query("瑞典","*");
print_r($res);//打印数据
?>
执行脚本:
$ php demo.php
输出结果
Array
(
[error] =>
[warning] =>
[status] => 0
[fields] => Array
(
[0] => title
[1] => intro
)
[attrs] => Array
(
[title] => 7
[intro] => 7
)
[matches] => Array
(
[31] => Array
(
[weight] => 4681
[attrs] => Array
(
[title] => 瑞典哈马比生态城
[intro] => 位于瑞典首都的哈马比生态城是一个在可持续经济、环境和社会领域取得了显著成就的综合生态社区。该社区以高循环、低能耗等环保特征举世闻名。
为申办2004年奥运会,斯德哥尔摩市政府对哈马比社区进行规划和改造,虽然斯德哥尔摩未申报成功,但哈马比生态城的建设却继续进行了下去。
)
)
[32] => Array
(
[weight] => 4681
[attrs] => Array
(
[title] => 瑞典马尔默
[intro] => 马尔默是瑞典第三大城市,历史悠久,地理条件优越。这个曾经以重工业为主的城市,自上世纪九十年代以来,通过西港区城区改造等项目,大范围使用可再生清洁能源、采取垃圾分类回收和无害化处理,通过发展循环经济成功实现了从工业重镇向环保生态城市的转型。
)
)
[33] => Array
(
[weight] => 2632
[attrs] => Array
(
[title] => 瑞典斯德哥尔摩市Henriksdal污水厂
[intro] => Henriksdal是当今世界上最大规模的污水处理厂之一,所占空间为30万m3左右,拥有约长达18km的地下管道。该厂建设在岩洞内,地质结构特殊。在厂址上方生活的人口大约有1630人左右。
Henriksdal也是当今欧洲最大规模、最现代化的污水处理厂之一,于1941年建成开放,每天可轻度处理约15万m3的污水。Henriksdal表面的设施包括:控制中心、机械处理装置、淤泥存储箱、淤泥增稠装置、沼气池、沼气收集和存储装置。
)
)
)
[total] => 3
[total_found] => 3
[time] => 0.000
[words] => Array
(
[瑞] => Array
(
[docs] => 4
[hits] => 9
)
[典] => Array
(
[docs] => 17
[hits] => 19
)
)
)
网友评论