sphinx3安装使用入门
2019-02-25 本文已影响0人
零一间
下载安装
tar -zxvf sphinx-3.1.1-612d99f-linux-amd64.tar.gz
cd sphinx-3.1.1/
mkdir data log
复制测试数据到test数据库
mysql -h127.0.0.1 -uroot -p test < example.sql
测试sql内容
DROP TABLE IF EXISTS test.documents;
CREATE TABLE test.documents
(
id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
group_id INTEGER NOT NULL,
group_id2 INTEGER NOT NULL,
date_added DATETIME NOT NULL,
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL
);
REPLACE INTO test.documents ( id, group_id, group_id2, date_added, title, content ) VALUES
( 1, 1, 5, NOW(), 'test one', 'this is my test document number one. also checking search within phrases.' ),
( 2, 1, 6, NOW(), 'test two', 'this is my test document number two' ),
( 3, 2, 7, NOW(), 'another doc', 'this is another group' ),
( 4, 2, 8, NOW(), 'doc number four', 'this is to test groups' );
DROP TABLE IF EXISTS test.tags;
CREATE TABLE test.tags
(
docid INTEGER NOT NULL,
tagid INTEGER NOT NULL,
UNIQUE(docid,tagid)
);
INSERT INTO test.tags VALUES
(1,1), (1,3), (1,5), (1,7),
(2,6), (2,4), (2,2),
(3,15),
(4,7), (4,40);
修改配置
cp sphinx-min.conf.dist sphinx.conf
vim etc/sphinx.conf
修改后的文件内容
#
# Minimal Sphinx configuration sample (clean, simple, functional)
#
source documents
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass = root
sql_db = test
sql_port = 3306
sql_query_pre = SET NAMES utf8
sql_query_pre = SET SESSION query_cache_type=OFF
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_field_string = title
sql_field_string = content
}
index testindex
{
source = documents
path = /home/jinchunguang/server/sphinx-3.1.1/data/test
ngram_len = 1
ngram_chars = U+3000..U+2FA1F
}
indexer
{
mem_limit = 128M
}
searchd
{
listen = 9312
listen = 9306:mysql41
log = /home/jinchunguang/server/sphinx-3.1.1/log/searchd.log
query_log = /home/jinchunguang/server/sphinx-3.1.1/log/query.log
read_timeout = 5
max_children = 30
pid_file = /home/jinchunguang/server/sphinx-3.1.1/logsearchd.pid
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
workers = threads
binlog_path = /home/jinchunguang/server/sphinx-3.1.1/data
}
测试配置
$ ./bin/indexer --config etc/sphinx.conf
Sphinx 3.1.1 (commit 612d99f)
Copyright (c) 2001-2018, Andrew Aksyonoff
Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)
ERROR: nothing to do.
生成索引数据
$ ./bin/indexer --all --config etc/sphinx.conf
Sphinx 3.1.1 (commit 612d99f)
Copyright (c) 2001-2018, Andrew Aksyonoff
Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file 'etc/sphinx.conf'...
indexing index 'testindex'...
collected 7 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 7 docs, 1.3 Kb
total 0.0 sec, 142.5 Kb/sec, 754 docs/sec
启动后台服务
$ ./bin/searchd --config etc/sphinx.conf
Sphinx 3.1.1 (commit 612d99f)
Copyright (c) 2001-2018, Andrew Aksyonoff
Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file 'etc/sphinx.conf'...
listening on all interfaces, port=9312
listening on all interfaces, port=9306
precaching index 'testindex'
rotating index 'testindex': success
precaching index 'testrt'
precached 2 indexes in 0.009 sec
停止服务
killall searchd
重新生成数据
$./bin/indexer testindex --config etc/sphinx.conf --rotate
php 测试
测试代码 test.php
<?php
include'./sphinxapi.php';
$sphinx=new SphinxClient();
$sphinx->SetServer('127.0.0.1',9312);
$res=$sphinx->Query("this","*");
print_r($res);//打印数据
?>
sphinxapi
在安装api目录
$ php test.php
Array
(
[error] =>
[warning] =>
[status] => 0
[fields] => Array
(
[0] => title
[1] => content
)
[attrs] => Array
(
[group_id] => 1
[date_added] => 2
[title] => 7
[content] => 7
)
[matches] => Array
(
[1] => Array
(
[weight] => 1304
[attrs] => Array
(
[group_id] => 1
[date_added] => 1551064565
[title] => test one
[content] => this is my test document number one. also checking search within phrases.
)
)
[2] => Array
(
[weight] => 1304
[attrs] => Array
(
[group_id] => 1
[date_added] => 1551064565
[title] => test two
[content] => this is my test document number two
)
)
[3] => Array
(
[weight] => 1304
[attrs] => Array
(
[group_id] => 2
[date_added] => 1551064565
[title] => another doc
[content] => this is another group
)
)
[4] => Array
(
[weight] => 1304
[attrs] => Array
(
[group_id] => 2
[date_added] => 1551064565
[title] => doc number four
[content] => this is to test groups
)
)
)
[total] => 4
[total_found] => 4
[time] => 0.000
[words] => Array
(
[this] => Array
(
[docs] => 4
[hits] => 4
)
)
)
php中文测试
测试数据
INSERT test.documents ( group_id, group_id2, date_added, title, content ) VALUES ( 1, 3, NOW(), '首都北京', '北京,简称“京”,是中华人民共和国省级行政区、首都、直辖市,是全国的政治、文化中心。北京地处中国华北地区,中心位于东经116°20′、北纬39°56′,东与天津毗连,其余均与河北相邻,北京市总面积16410.54平方千米。' );
INSERT test.documents ( group_id, group_id2, date_added, title, content ) VALUES ( 1, 3, NOW(), '离开北京:风口在哪里,他们的“首都”就在哪里', '我们访问了一组离开者。不管生存,生活,还是生意,总有一个理由,让那些曾经的到来者最终选择生活在别处。而不论去到故乡还是他乡,北京总成为标尺,用以参照过去现在未来的生活。这是北京的真谛。当我们讨论去留,其实讨论的是人生选择的问题。许多时候选择无关好坏,而是心归何方。' );
INSERT test.documents ( group_id, group_id2, date_added, title, content ) VALUES ( 1, 3, NOW(), '非首都功能', '非首都功能指那些与首都功能发展不相符的城市功能,非首都功能由习近平总书记在2015年2月10日的中央财经领导小组第九次会议上提出,他指出:要疏解北京“非首都功能”,“作为一个有13亿人口大国的首都,不应承担也没有足够的能力承担过多的功能。”' );
查询中文
$ php test.php
Array
(
[error] =>
[warning] =>
[status] => 0
[fields] => Array
(
[0] => title
[1] => content
)
[attrs] => Array
(
[group_id] => 1
[date_added] => 2
[title] => 7
[content] => 7
)
[matches] => Array
(
[5] => Array
(
[weight] => 4601
[attrs] => Array
(
[group_id] => 1
[date_added] => 1551071049
[title] => 首都北京
[content] => 北京,简称“京”,是中华人民共和国省级行政区、首都、直辖市,是全国的政治、文化中心。北京地处中国华北地区,中心位于东经116°20′、北纬39°56′,东与天津毗连,其余均与河北相邻,北京市总面积16410.54平方千米。
)
)
[6] => Array
(
[weight] => 4587
[attrs] => Array
(
[group_id] => 1
[date_added] => 1551071049
[title] => 离开北京:风口在哪里,他们的“首都”就在哪里
[content] => 我们访问了一组离开者。不管生存,生活,还是生意,总有一个理由,让那些曾经的到来者最终选择生活在别处。而不论去到故乡还是他乡,北京总成为标尺,用以参照过去现在未来的生活。这是北京的真谛。当我们讨论去留,其实讨论的是人生选择的问题。许多时候选择无关好坏,而是心归何方。
)
)
[7] => Array
(
[weight] => 2555
[attrs] => Array
(
[group_id] => 1
[date_added] => 1551071049
[title] => 非首都功能
[content] => 非首都功能指那些与首都功能发展不相符的城市功能,非首都功能由习近平总书记在2015年2月10日的中央财经领导小组第九次会议上提出,他指出:要疏解北京“非首都功能”,“作为一个有13亿人口大国的首都,不应承担也没有足够的能力承担过多的功能。”
)
)
)
[total] => 3
[total_found] => 3
[time] => 0.000
[words] => Array
(
[北] => Array
(
[docs] => 3
[hits] => 11
)
[京] => Array
(
[docs] => 3
[hits] => 9
)
)
)
sphinxQL测试
$ mysql -h0 -P9306
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 3.1.1 (commit 612d99f)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select * from testindex where match('北京') \G
*************************** 1. row ***************************
id: 5
group_id: 1
date_added: 1551071049
title: 首都北京
content: 北京,简称“京”,是中华人民共和国省级行政区、首都、直辖市,是全国的政治、文化中心。北京地处中国华北地区,中心位于东经116°20′、北纬39°56′,东与天津毗连,其余均与河北相邻,北京市总面积16410.54平方千米。
*************************** 2. row ***************************
id: 6
group_id: 1
date_added: 1551071049
title: 离开北京:风口在哪里,他们的“首都”就在哪里
content: 我们访问了一组离开者。不管生存,生活,还是生意,总有一个理由,让那些曾经的到来者最终选择生活在别处。而不论去到故乡还是他乡,北京总成为标尺,用以参照过去现在未来的生活。这是北京的真谛。当我们讨论去留,其实讨论的是人生选择的问题。许多时候选择无关好坏,而是心归何方。
*************************** 3. row ***************************
id: 7
group_id: 1
date_added: 1551071049
title: 非首都功能
content: 非首都功能指那些与首都功能发展不相符的城市功能,非首都功能由习近平总书记在2015年2月10日的中央财经领导小组第九次会议上提出,他指出:要疏解北京“非首都功能”,“作为一个有13亿人口大国的首都,不应承担也没有足够的能力承担过多的功能。”
3 rows in set (0.00 sec)