以sphinx.conf中默认的数据为例:
1.先在mysql中插入一个计数表和两个索引表
CREATE TABLE sph_counter(
counter_id INTEGER PRIMARY KEY NOT NULL,
max_doc_id INTEGER NOT NULL
);
//主索引使用(确认以前是否已经创建过该表,若是已经创建,这里就不须要从新建了)
CREATE TABLE `sphinx` (
`id` int(11) NOT NULL,
`weight` int(11) NOT NULL,
`query` varchar(255) NOT NULL,
`CATALOGID` INT NOT NULL,
`EDITUSERID` INT NOT NULL,
`HITS` INT NULL,
`ADDTIME` INT NOT NULL, KEY
`Query` (`Query`)
) ENGINE=SPHINX DEFAULT CHARSET=utf8CONNECTION='sphinx://localhost:3312/test1'
//增量索引使用
CREATE TABLE `sphinx1` (
`id` int(11) NOT NULL,
`weight` int(11) NOT NULL,
`query` varchar(255) NOT NULL,
`CATALOGID` INT NOT NULL,
`EDITUSERID` INT NOT NULL,
`HITS` INT NULL,
`ADDTIME` INT NOT NULL, KEY
`Query` (`Query`)
)ENGINE=SPHINX DEFAULT CHARSET=utf8 CONNECTION='sphinx://localhost:3312/ test1stemmed '
2.修改sphinx.conf
source src1
{
sql_query_pre = SET NAMES utf8
sql_query_pre = SET SESSION query_cache_type=OFF
sql_query_pre = REPLACE INTO sph_counter SELECT 1, MAX(id) FROM documents
sql_query= SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content FROM documents \
WHERE id<=( SELECT max_doc_id FROM sph_counter WHERE counter_id=1 )
... //其余能够默认
}
// 注意:sql_query_pre的个数需和src1对应,不然可能搜索不出相应结果
source src1throttled : src1
{
sql_ranged_throttle = 100
sql_query_pre = SET NAMES utf8
sql_query_pre = SET SESSION query_cache_type=OFF
sql_query_pre =
sql_query = SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content FROM documents \
WHERE id>( SELECT max_doc_id FROM sph_counter WHERE counter_id=1 )
}
index test1//主索引
{
source= src1
...
}
index test1stemmed : test1//增量索引
{
source = src1throttled
...
}
3.重建索引
/usr/local/sphinx/bin/searchd --stop
/usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf --all
/usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/sphinx.conf
插入测试数据
INSERT INTO `test`.`documents` (
`id` ,
`group_id` ,
`group_id2` ,
`date_added` ,
`title` ,
`content`
)
VALUES (
NULL , '3', '11', NOW( ) , '索引合并', '合并两个已有的索引比从新对全部数据作索引更有效率,并且有时候必须这样作(例如在“ 主索引+增量索引”分区模式中应合并主索引和增量索引,而不是简单地从新索引“主索引对应的数据)。所以indexer有这个选项。合并索引通常比从新索引快,但在大型索引上仍然不是一蹴而就。基本上,待合并的两个索引都会被读入内存一次,而合并后的内容须要写入磁盘一次。例如,合并100GB和1GB的两个索引将致使202GB的IO操做(但极可能仍是比从新索引少)'
);
执行
SELECT doc . *
FROM documents doc
JOIN sphinx ON ( doc.id = sphinx.id )
WHERE query = '索引'
你会发现你刚添加的数据没有被检索出来
而后执行:
SELECT doc.* FROM documents doc join sphinx1 on(doc.id=sphinx1.id) where query='索引'
你会发现数据是空的,这时咱们就须要来更新增量索引了。
经过执行:
/usr/local/sphinx/bin/indexer --rotate --config /usr/local/sphinx/etc/sphinx.conf test1stemmed
命令来更新增量索引(正式使用时,咱们能够将该命令配置到系统计划任务中,每隔几分钟执行一次)
–rotate: 该参数可使咱们在不须要中止searchd的状况下,直接加载索引
执行完命令该命令后,咱们再来查看一下增量索引的数据
SELECT doc.* FROM documents doc join sphinx1 on(doc.id=sphinx1.id) where query='索引'
你会发现新添加的数据被检索出来的。
主索引的更新:
/usr/local/sphinx/bin/indexer --rotate --config /usr/local/sphinx/etc/sphinx.conf test1
collected 997 docs, 1.4 MB
sorted 0.3 Mhits, 100.0% done
total 997 docs, 1430054 bytes
total 1.428 sec, 1001459.38 bytes/sec, 698.19 docs/sec
(咱们能够设置成天天的午夜执行)
只有在更新了主索引后,结果才会被更新
SELECT doc.* FROM documents doc join sphinx on(doc.id=sphinx.id) where query='索引'
咱们也能够经过合并索引的方式使主索引的数据保持更新
/usr/local/sphinx/bin/indexer --merge test1 test1stemmed --rotate
能够将增量索引test1stemmed合并到主索引test1中去
为建立2个shell脚本,一个用来建立主索引、一个用来建立增量索引(此步能够省略)
1.建立主索引脚本build_main_index.sh
#!/bin/sh
#/usr/local/sphinx/bin/searchd --stop
/usr/local/sphinx/bin/indexer test1 --config /usr/local/sphinx/etc/sphinx.conf >> /var/log/sphinx/mainindexlog
#/usr/local/sphinx/bin/searchd
2.建立增量索引脚本build_delta_index.sh
#!/bin/sh
#/usr/local/sphinx/bin/searchd --stop
/usr/local/sphinx/bin/indexer test1stemmed --config /usr/local/sphinx/etc/sphinx.conf --rotate>> /var/log/sphinx/deltaindexlog
/usr/local/sphinx/bin/indexer --merge test1 test1stemmed --config /usr/local/sphinx/etc/sphinx.conf --rotate >> /var/log/sphinx/deltaindexlog
#/usr/local/sphinx/bin/searchd
每隔5分钟进行索引增量合并,天天2:30重建索引
*/5 * * * * /bin/sh /opt/shell/build_delta_index.sh > /dev/null 2>&1
30 2* * * /bin/sh /opt/shell/build_main_index.sh > /dev/null 2>&1
每周一至周六上早6点增量合并,同日重建索引
1 6 * * 1-6 /bin/sh /opt/shell/build_delta_index.sh > /dev/null 2>&1
1 6 * * 7 /bin/sh /opt/shell/build_main_index.sh > /dev/null 2>&1
参考:
http://www.coreseek.com/uploads/pdf/sphinx_doc_zhcn_0.9.pdf
#Linux