在Centos or redhat 安裝Sphinx1.首先安裝依賴包$ yum install postgresql-libs unixODBC2.安裝軟體$ rpm -Uhv sphinx-2.2.1-1.rhel6.x86_64.rpm3.啟動服務$ service searchd start...
在Centos or redhat 安裝Sphinx 1.首先安裝依賴包 $ yum install postgresql-libs unixODBC 2.安裝軟體 $ rpm -Uhv sphinx-2.2.1-1.rhel6.x86_64.rpm 3.啟動服務 $ service searchd start [root@face sphinx-2.2.10]# find / -name sphinx /var/run/sphinx /var/log/sphinx /var/lib/sphinx /etc/logrotate.d/sphinx /etc/sphinx /usr/share/sphinx /usr/share/sphinx/api/ruby/spec/sphinx /usr/share/sphinx/api/ruby/lib/sphinx mysql> #創建測試庫 mysql> create database test; Query OK, 1 row affected (0.00 sec) #插入測試數據 mysql -uroot -ppassword test < /usr/share/doc/sphinx-2.2.10/example.sql mysql> desc tags; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | docid | int(11) | NO | PRI | NULL | | | tagid | int(11) | NO | PRI | NULL | | +-------+---------+------+-----+---------+-------+ 2 rows in set (0.00 sec) #表結構 mysql> desc documents; +------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | group_id | int(11) | NO | | NULL | | | group_id2 | int(11) | NO | | NULL | | | date_added | datetime | NO | | NULL | | | title | varchar(255) | NO | | NULL | | | content | text | NO | | NULL | | +------------+--------------+------+-----+---------+----------------+ 6 rows in set (0.00 sec) #表數據 mysql> select * from documents; +----+----------+-----------+---------------------+-----------------+---------------------------------------------------------------------------+ | id | group_id | group_id2 | date_added | title | content | +----+----------+-----------+---------------------+-----------------+---------------------------------------------------------------------------+ | 1 | 1 | 5 | 2016-01-15 14:24:23 | test one | this is my test document number one. also checking search within phrases. | | 2 | 1 | 6 | 2016-01-15 14:24:23 | test two | this is my test document number two | | 3 | 2 | 7 | 2016-01-15 14:24:23 | another doc | this is another group | | 4 | 2 | 8 | 2016-01-15 14:24:23 | doc number four | this is to test groups | +----+----------+-----------+---------------------+-----------------+---------------------------------------------------------------------------+ 4 rows in set (0.00 sec) mysql> select * from tags; +-------+-------+ | docid | tagid | +-------+-------+ | 1 | 1 | | 1 | 3 | | 1 | 5 | | 1 | 7 | | 2 | 2 | | 2 | 4 | | 2 | 6 | | 3 | 15 | | 4 | 7 | | 4 | 40 | +-------+-------+ 10 rows in set (0.00 sec) #sphinx的配置文件 [root@face sphinx]# grep "^$\|^#" -v sphinx.conf source src1 {
#資料庫類型 數據來源信息 type = mysql
#資料庫的IP sql_host = localhost #資料庫用戶
sql_user = root
#資料庫密碼 sql_pass =xxxxxx
#資料庫庫名 sql_db = test
#預設的配置文件沒有這一句 需要註意 sql_sock = /tmp/mysql.sock sql_port = 3306 # optional, default is 3306
#定義取數據的SQL,第一列ID列必須為唯一的正整數值
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 } index test1 {
#定義索引的源 source = src1
#設置生成的索引存放路徑 path = /var/lib/sphinx/test1 } index testrt {
#實時索引類型 type = rt rt_mem_limit = 128M path = /var/lib/sphinx/testrt rt_field = title rt_field = content rt_attr_uint = gid }
#定義indexer配置選項 indexer {
#定義生成索引過程使用索引的限制 mem_limit = 128M }
##定義searchd守護進程的相關選項 searchd {
#tcp 0 0 0.0.0.0:9312 0.0.0.0:* LISTEN 9289/searchd listen = 9312 listen = 9306:mysql41 #進程服務日誌
log = /var/log/sphinx/searchd.log
#查詢日誌 query_log = /var/log/sphinx/query.log
#網路客服端請求的讀超時 時間 read_timeout = 5 #子進程數
max_children = 30 #進程文件的路徑
pid_file = /var/run/sphinx/searchd.pid
#啟用無縫seamless輪轉,防止searchd輪轉在需要預取大量數據的索引時停止響應
#也就是說在任何時刻查詢都可用,或者使用舊索引,或者使用新索引
seamless_rotate = 1 preopen_indexes = 1 unlink_old = 1 workers = threads # for RT to work binlog_path = /var/lib/sphinx/ } [root@face sphinx]# service searchd start Starting searchd: Sphinx 2.2.10-id64-release (2c212e0) Copyright (c) 2001-2015, Andrew Aksyonoff Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com) using config file '/etc/sphinx/sphinx.conf'... listening on all interfaces, port=9312 listening on all interfaces, port=9306 precaching index 'test1' WARNING: index 'test1': preload: /var/lib/sphinx/test1.sph is invalid header file (too old index version?); NOT SERVING precaching index 'testrt' precached 2 indexes in 0.004 sec [ OK ] #如果啟動有warning 註意這兒的許可權 [root@face sphinx]# pwd /var/lib/sphinx [root@face sphinx]# chown sphinx.sphinx ./ -R [root@face sphinx]# [root@face sphinx]# ll total 36 -rw-------. 1 sphinx sphinx 8 Jan 15 15:28 binlog.001 -rw-------. 1 sphinx sphinx 0 Jan 15 15:28 binlog.lock -rw-------. 1 sphinx sphinx 11 Jan 15 15:28 binlog.meta -rw-r--r--. 1 sphinx sphinx 128 Jan 15 15:32 test1.spa -rw-r--r--. 1 sphinx sphinx 148 Jan 15 15:32 test1.spd -rw-r--r--. 1 sphinx sphinx 1 Jan 15 15:32 test1.spe -rw-r--r--. 1 sphinx sphinx 371 Jan 15 15:32 test1.sph -rw-r--r--. 1 sphinx sphinx 190 Jan 15 15:32 test1.spi -rw-r--r--. 1 sphinx sphinx 0 Jan 15 15:32 test1.spk -rw-r--r--. 1 sphinx sphinx 0 Jan 15 15:32 test1.spm -rw-r--r--. 1 sphinx sphinx 37 Jan 15 15:32 test1.spp -rw-r--r--. 1 sphinx sphinx 1 Jan 15 15:32 test1.sps -rw-------. 1 sphinx sphinx 0 Jan 15 15:28 testrt.lock #不能有warning 開始的時候 [root@face sphinx]# service searchd restart Stopping searchd: [ OK ] Starting searchd: Sphinx 2.2.10-id64-release (2c212e0) Copyright (c) 2001-2015, Andrew Aksyonoff Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com) using config file '/etc/sphinx/sphinx.conf'... listening on all interfaces, port=9312 listening on all interfaces, port=9306 precaching index 'test1' precaching index 'testrt' precached 2 indexes in 0.001 sec [ OK ] #不+ --rotate有可能起不來 [root@face sphinx]# indexer --all --rotate Sphinx 2.2.10-id64-release (2c212e0) Copyright (c) 2001-2015, Andrew Aksyonoff Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com) using config file '/etc/sphinx/sphinx.conf'... indexing index 'test1'... collected 4 docs, 0.0 MB sorted 0.0 Mhits, 100.0% done total 4 docs, 193 bytes total 0.003 sec, 58949 bytes/sec, 1221.74 docs/sec skipping non-plain index 'testrt'... total 4 reads, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg total 12 writes, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg rotating indices: successfully sent SIGHUP to searchd (pid=9289). #連接server 查看數據狀態 [root@face sphinx]# mysql -h0 -P9306 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 2.2.10-id64-release (2c212e0) Copyright (c) 2000, 2015, 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> show tables; +--------+-------+ | Index | Type | +--------+-------+ | test1 | local | | testrt | rt | +--------+-------+ 2 rows in set (0.00 sec) mysql> select * from test1; +------+----------+------------+ | id | group_id | date_added | +------+----------+------------+ | 1 | 1 | 1452839063 | | 2 | 1 | 1452839063 | | 3 | 2 | 1452839063 | | 4 | 2 | 1452839063 | +------+----------+------------+ 4 rows in set (0.00 sec) mysql> select * from testrt; Empty set (0.00 sec)
mysql> INSERT INTO testrt VALUES ( 1, 'first record', 'test one', 123 );
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO testrt VALUES ( 2, 'second record', 'test two', 234 );
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO testrt VALUES ( 3, 'three record', 'three', 334 );
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM testrt;
+------+------+
| id | gid |
+------+------+
| 1 | 123 |
| 2 | 234 |
| 3 | 334 |
+------+------+
3 rows in set (0.00 sec)
mysql> SELECT * FROM testrt WHERE MATCH('test');
+------+------+
| id | gid |
+------+------+
| 1 | 123 |
| 2 | 234 |
+------+------+
2 rows in set (0.00 sec)
#模糊匹配 mysql> SELECT * FROM test1 WHERE MATCH('my document'); +------+----------+------------+ | id | group_id | date_added | +------+----------+------------+ | 1 | 1 | 1452839063 | | 2 | 1 | 1452839063 | +------+----------+------------+ 2 rows in set (0.00 sec) mysql> SELECT *, WEIGHT() FROM test1 WHERE MATCH('"document one"/1');SHOW META; +------+----------+------------+----------+ | id | group_id | date_added | weight() | +------+----------+------------+----------+ | 1 | 1 | 1452839063 | 2663 | | 2 | 1 | 1452839063 | 1528 | +------+----------+------------+----------+ 2 rows in set (0.18 sec) #相關的元數據信息 +---------------+----------+ | Variable_name | Value | +---------------+----------+ | total | 2 | | total_found | 2 | | time | 0.175 | | keyword[0] | document | | docs[0] | 2 | | hits[0] | 2 | | keyword[1] | one | | docs[1] | 1 | | hits[1] | 2 | +---------------+----------+ 9 rows in set (0.00 sec) #相關的統計信息 mysql> SET profiling=1;SELECT * FROM test1 WHERE id IN (1,2,4);SHOW PROFILE; Query OK, 0 rows affected (0.00 sec) +------+----------+------------+ | id | group_id | date_added | +------+----------+------------+ | 1 | 1 | 1452839063 | | 2 | 1 | 1452839063 | | 4 | 2 | 1452839063 | +------+----------+------------+ 3 rows in set (0.00 sec) +--------------+----------+----------+---------+ | Status | Duration | Switches | Percent | +--------------+----------+----------+---------+ | unknown | 0.000206 | 4 | 65.61 | | net_read | 0.000004 | 1 | 1.27 | | local_search | 0.000040 | 1 | 12.74 | | sql_parse | 0.000027 | 1 | 8.60 | | fullscan | 0.000002 | 1 | 0.64 | | finalize | 0.000015 | 1 | 4.78 | | aggregate | 0.000008 | 2 | 2.55 | | net_write | 0.000012 | 1 | 3.82 | | eval_post | 0.000000 | 1 | 0.00 | | total | 0.000314 | 13 | 0 | +--------------+----------+----------+---------+ 10 rows in set (0.00 sec) mysql> SELECT id, id%3 idd FROM test1 WHERE MATCH('this is | nothing') GROUP BY idd;SHOW PROFILE; +------+------+ | id | idd | +------+------+ | 1 | 1 | | 2 | 2 | | 3 | 0 | +------+------+ 3 rows in set (0.18 sec) +--------------+----------+----------+---------+ | Status | Duration | Switches | Percent | +--------------+----------+----------+---------+ | unknown | 0.000430 | 6 | 0.24 | | net_read | 0.000014 | 1 | 0.01 | | local_search | 0.000192 | 1 | 0.11 | | sql_parse | 0.000056 | 1 | 0.03 | | dict_setup | 0.000002 | 1 | 0.00 | | parse | 0.000040 | 1 | 0.02 | | transforms | 0.000003 | 1 | 0.00 | | init | 0.177525 | 3 | 99.51 | | read_docs | 0.000053 | 2 | 0.03 | | get_docs | 0.000008 | 5 | 0.00 | | get_hits | 0.000005 | 2 | 0.00 | | filter | 0.000002 | 1 | 0.00 | | rank | 0.000001 | 3 | 0.00 | | sort | 0.000014 | 2 | 0.01 | | finalize | 0.000004 | 1 | 0.00 | | aggregate | 0.000017 | 2 | 0.01 | | net_write | 0.000027 | 1 | 0.02 | | eval_post | 0.000001 | 1 | 0.00 | | total | 0.178394 | 35 | 0 | +--------------+----------+----------+---------+ 19 rows in set (0.00 sec) mysql> CALL KEYWORDS ('one two three', 'test1'); +------+-----------+------------+ | qpos | tokenized | normalized | +------+-----------+------------+ | 1 | one | one | | 2 | two | two | | 3 | three | three | +------+-----------+------------+ 3 rows in set (0.00 sec) #hits表示是否命中 mysql> CALL KEYWORDS ('one two three', 'test1', 1); +------+-----------+------------+------+------+ | qpos | tokenized | normalized | docs | hits | +------+-----------+------------+------+------+ | 1 | one | one | 1 | 2 | | 2 | two | two | 1 | 2 | | 3 | three | three | 0 | 0 | +------+-----------+------------+------+------+ 3 rows in set (0.00 sec)
試驗到這兒吧