Important three commands in the Sphinx, Splinx installed in the bin directory
Indexer CREATE INDEX command, searchd START process command, Search command searching command (no longer exists in new version)
Download Sphinx and install
./configure--prefix=/usr/local/sphinx–with-mysql=/usr/local/mysql
Make && make install
sphinx.conf configuration file Configuration
#主数据源修改
SOURCE main{
Sql_query_pre = SET NAMES UTF8
Sql_query_pre = SET Sessionquery_cache_type=off
Sql_query_pre = Replace into sph_counterselect 1,max (ID) from post
Sql_query = Select Id,title,content from post where id<= (select max_doc_id from Sph_ocunter where C ounter_id = 1)
}
#增量数据源
SOURCE Delta:main
{
Sql_query_pre = set Names UTF8
sql_query= Select Id,title,content from post where ID > (selectmax_doc_id from sph_counter where counter_id = 1)
}
#主索引:
Index Main
{
Source = Main
Path =/usr/local/coreseek/var/data/main
Charset_type = Zh_cn.utf-8
Charset_dictpath =/usr/local/mmseg/etc/
}
#增量索引
Index Delta:main
{
Source = Delta
Path =/usr/local/coreseek/var/data/delta
}
There are two ways to integrate Sphinx into a PHP program
1.Sphinx PHP Module
2. Using the Sphinxapi class
We need to do the following several things to use Sphinx
1. First, there must be data.
2. Establish Sphinx configuration file
3. Generate index
4. Start SEARCHD service process, open port 9312
5. Use the PHP client program to connect the Sphinx service
PHP Load Sphinx Module
CD sphinx-1.3.3
Phpize
CD csft-3.2.14/api/libsphinxclient/
./configure
Make && make install
cd/root/sphinx-1.3.3/
./configure--with-php-config=/usr/bin/php-config--with-sphinx
/usr/lib64/php/modules/
Add sphinx.so to the php.ini
PHP Load Sphinx Complete
<?php
Include (' api/sphinxapi.php ');
$sphinx = new Sphinxclient ();
Host name and port for Sphinx
$sphinx->setserver (' 127.0.0.1 ', 9312);
Sph_match_all (synonymous with)
$sphinx->setmatchmode (Sph_match_any);
Set return result set to PHP array format
$sphinx->setarrayresult (TRUE);
The offset of the matching result, and the value of the parameter is: Starting position, returning the number of result bars, the maximum number of matching bars
$sphinx->setlimits (0,20, 1000);
Maximum Search Time
$sphinx->setmaxquerytime (10);
To perform a simple search, this search will query all fields for information
$query = $_get[' query '];
$res = $sphinx->query ($query, ' * ');
$ids = Array ();
foreach ($res [' matches '] as $row) {
$ids []= $row [' id '];
}
$id _str= implode (', ', $ids);
$ids = Join (', ', Array_keys ($res [' matches ']));
mysql_connect (' localhost ', ' root ', ' root ');
mysql_select_db (' Test ');
$sql = "SELECT * from post where ID to ({$id _str})";
mysql_query ("Setnames utf8");
$rst = mysql_query ($sql);
$opts =array (
' Before_match ' => ' <fontstyle= ' font-weight:bold;color:red; > ',
' After_match ' => ' </font> '
);
while ($row = Mysql_fetch_assoc ($rst)) {
/*echo "<pre>";
Print_r ($row);
echo "</pre>";
$r = $sphinx->buildexcerpts ($row, "main", $query, $opts);
echo "{$r [0]} post <br/>";
echo "title: {$r [1]}<br/>";
echo "content: {$r [2]}<br/>";
}
Sphinx Real-time indexing (the above Sphinx is not implemented in real time)
Primary index generation (typically when there is less traffic on a site)
Main.sh:
/usr/local/coreseek/bin/indexer main--rotate >>/usr/local/coreseek/var/log/main.log
Incremental index generation (generated once every 5 minutes)
Delta.sh:
/usr/local/coreseek/bin/indexer delta--rotate >>/usr/local/coreseek/var/log/delta.log
#进入定时器编辑界面
Crontab–e
#每过5分钟生成一次
*/5 * * * */usr/local/coreseek/init/delta.sh
#每天凌晨三点生成一次
* * */usr/local/coreseek/init/main.sh
#退出编辑器后添加执行的权限
Chmod A+x *
#启动任务计划
Crontab–l
Sphinx focuses on knowledge of sphinx, Chinese participle, highlighting, summarization, matching, incremental indexing, real-time indexing, and distributed indexing.