环境:win7 64 wamp
解压sphinx安装包后,简历如下结构。注意,conf目录是我的配置文件目录
在conf目录下,简历newdefend.conf文件,配置内容如下
# 配置数据源 source domain_src { type = mysql sql_host = 192.168.185.210 sql_user = root sql_pass = sql_db = mydb sql_port = 3306 sql_ranged_throttle = 0 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 domain
# 注意这个地方,至少要有各自字段没有定义成sql_attr_***,好像是需要有一个字段是全文索引什么的。反正多查询一个字段就对了。或者定义一个sql_field_string也可以 sql_query = SELECT id,uid,type,package_level,ct,domain FROM domain \ WHERE id<=( SELECT max_id FROM sph_counter WHERE counter_id = 1 ) sql_attr_uint = uid sql_attr_uint = type sql_attr_uint = package_level # sql_field_string = ct sql_attr_timestamp = ct } # 配置增量数据源 source delta_domain_src:domain_src{ sql_ranged_throttle = 100 sql_query_pre = SET NAMES utf8 # sql_query_pre = SET SESSION query_cache_type=OFF sql_query = SELECT id,uid,type,package_level,ct,domain FROM domain \ WHERE id>( SELECT max_id FROM sph_counter WHERE counter_id = 1 ) } # 索引 index domain_suggest { source = domain_src path = D:/sphinx/data/domain_suggest charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F docinfo = extern dict = keywords mlock = 0 morphology = none min_word_len = 1 ngram_len = 1 ngram_chars = U+3000..U+2FA1F html_strip = 0 } # 增量索引 index delta_domain_suggest:domain_suggest { source = delta_domain_src path = D:/sphinx/data/delta_domain_suggest } indexer { mem_limit = 128M } # 配置Sphinx服务器的信息 searchd { listen = 9312 listen = 9306:mysql41 log = D:/sphinx/log/searchd.log query_log = D:/sphinx/log/query.log read_timeout = 5 client_timeout = 300 max_children = 30 persistent_connections_limit = 30 pid_file = D:/sphinx/log/searchd.pid preopen_indexes = 1 unlink_old = 1 mva_updates_pool = 1M max_packet_size = 8M max_filters = 256 max_filter_values = 4096 max_batch_queries = 32 workers = threads # windows下启动searchd服务一定要注释掉这个 # seamless_rotate = 1 # 开启sphinx服务时,会生成几个log文件,这里配置文件生成目录。默认会生成在searchd.exe同级目录下 binlog_path = D:/sphinx/log/ }
定义一个启动sphinx的脚本“sphinx重启.bat”。脚本内容如下,双击启动
@echo off ::停止sphinx服务 d:/sphinx/bin/searchd --stop ::建立配置文件中所有索引(也可以建立某个索引) d:/sphinx/bin/indexer -c d:/sphinx/conf/newdefend.conf --all ::启动sphinx服务 d:/sphinx/bin/searchd -c d:/sphinx/conf/newdefend.conf ::窗口不要关闭 pause
启动成功界面
测试代码
$keyword = ''; $sphinx = new SphinxClient; $sphinx->setServer("localhost", 9312); $sphinx->setMatchMode(SPH_MATCH_ALL); //匹配模式 ANY为关键词自动拆词,ALL为不拆词匹配(完全匹配) $sphinx->SetArrayResult ( true ); //返回的结果集为数组 $result = $sphinx->query($keyword,"domain_suggest delta_domain_suggest"); //星号为所有索引源 $count=$result['total']; //查到的结果条数 $time=$result['time']; //耗时 $arr=$result['matches']; //结果集 $id=''; foreach($arr as $i => $val){ $id.=$arr[$i]['id'].','; } $id=substr($id,0,-1); //结果集的id字符串 echo $id;
运行结果