## Minimal Sphinx configuration sample (clean, simple, functional)#source wp_posts{ # data source type. mandatory, no default value # known types are mysql, pgsql, mssql, xmlpipe, xmlpipe2, odbc type = mysql ##################################################################### ## SQL settings (for 'mysql' and 'pgsql' types) ##################################################################### # some straightforward parameters for SQL source types sql_host = localhost sql_user = root sql_pass = root sql_db = test sql_port = 3306 # optional, default is 3306 #待索引数据获取前查询 sql_query_pre = SET NAMES utf8 sql_query_pre = SET SESSION query_cache_type=OFF sql_query = select ID,post_content,post_title,post_name,guid,UNIX_TIMESTAMP(post_date) post_date \ from wp_posts #sql_field_string = post_title #sql_field_string = post_content sql_attr_timestamp = post_date #sql_query_info = select * from wp_posts where ID=$id}index wp_posts{ source = wp_posts path = /home/coreseek/data/59n_posts docinfo = extern charset_dictpath = /usr/local/mmseg3/etc/ charset_type = zh_cn.utf-8}########增量索引,进行实时更新source wp_posts_rt:wp_posts{ sql_query = select ID,post_content,post_title,post_name,guid,UNIX_TIMESTAMP(post_date) \ post_date \ from wp_posts where UNIX_TIMESTAMP(post_modified) > UNIX_TIMESTAMP() - 300}index wp_posts_rt{ source = wp_posts_rt path = /home/coreseek/data/59n_posts_rt docinfo = extern charset_dictpath = /usr/local/mmseg3/etc/ charset_type = zh_cn.utf-8}source wp_comment{ type = mysql sql_host = localhost sql_user = root sql_pass = root sql_db = test sql_port = 3306 # optional, default is 3306 #待索引数据获取前查询 sql_query_pre = SET NAMES utf8 sql_query_pre = SET SESSION query_cache_type=OFF sql_query = select comment_ID,comment_post_ID,comment_author,comment_content,UNIX_TIMESTAMP(comment_date) comment_date from wp_comments sql_attr_uint = comment_post_ID #sql_field_string = comment_author #sql_field_string = comment_content sql_attr_timestamp = comment_date #sql_query_info = select * from wp_comments where comment_ID=$id}index wp_comment{ source = wp_comment path = /home/coreseek/data/59n_comment docinfo = extern charset_dictpath = /usr/local/mmseg3/etc/ charset_type = zh_cn.utf-8}########增量索引,进行实时更新source wp_comment_rt:wp_comment{ sql_query = select comment_ID,comment_post_ID,comment_author,comment_content,UNIX_TIMESTAMP(comment_date) comment_date from wp_comments \ where UNIX_TIMESTAMP(comment_date) > UNIX_TIMESTAMP() - 300}index wp_comment_rt{ source = wp_comment_rt path = /home/coreseek/data/59n_comment_rt docinfo = extern charset_dictpath = /usr/local/mmseg3/etc/ charset_type = zh_cn.utf-8}indexer{ mem_limit = 256M}searchd{ listen = 9312 listen = 9306:mysql41 log = /usr/local/coreseek/var/log/searchd.log query_log = /usr/local/coreseek/var/log/query.log read_timeout = 5 max_children = 10 pid_file = /usr/local/coreseek/var/log/searchd.pid max_matches = 1000 seamless_rotate = 1 preopen_indexes = 1 unlink_old = 1 workers = threads # for RT to work}
进行首次索引
./bin/indexer --all
使用crontab -e进行增量索引进行实时更新
*/4 * * * * /usr/local/coreseek/bin/indexer --rotate wp_posts_rt wp_comment_rt
*/4 * * * * /usr/local/coreseek/bin/indexer --rotate --merge wp_posts wp_posts_rt
*/4 * * * * /usr/local/coreseek/bin/indexer --rotate --merge wp_comment wp_comment_rt
在PHP中使用sphinx扩展进行搜索.
$sp = new SphinxClient(); $sp->setMatchMode(SPH_MATCH_ANY); $result = $sp->query($name); //$err 错误 $err = $sp->GetLastError(); echo ''; var_dump($result); var_dump($err); echo '';