假设有两张那个表,分别为articles,article_photos两张表。搜索的时候,要匹配articles.title、articles.intro、article_photos.caption这三个字段。
PHP页面
header("content-type:text/html;charset=utf8");
include './sphinxapi.php'; //包含sphinxapi类
$sphinx = new SphinxClient(); //实例化
$sphinx->SetServer('localhost', 9312);//链接
$sphinx->SetLimits(0, 1000, 1000); //默认匹配20条记录。加上这行表示匹配1000条记录。(可用于分页)
$res = $sphinx->Query("暴动", "*");//查询的字段第二参数是你配置文件里面写得规则这里是*就会匹配所有规则
echo "<pre>";
print_r($res['matches']);
coreseek.conf 内容配置如下
#源定义
#mysql类只实现连接数据库
source mysql {
type = mysql
sql_host = localhost
sql_user = root
sql_pass =
sql_db = nextmgz_archive
sql_port = 3306
sql_query_pre = SET NAMES utf8
# 命令行查询时,设置正确的字符集
sql_query_info_pre = SET NAMES utf8
}
# 继承mysql
source articles : mysql
{
#sql_query第一列id需为整数
#title、content作为字符串/文本字段,被全文索引
sql_query = SELECT art_id,title,intro FROM articles
# 从SQL读取到的值必须为整数
sql_attr_uint = art_id
# 从SQL读取到的值必须为整数,作为时间属性
# sql_attr_timestamp = date_added
# 命令行查询时,从数据库读取原始数据信息
sql_query_info = SELECT * FROM articles WHERE art_id=$id
}
#index定义
index articles
{
# 对应的source名称
source = articles
#请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/
path = E:/__Work/coreseek-4.1-win32/var/data/articles
docinfo = extern
mlock = 0
morphology = none
min_word_len = 1
html_strip = 0
#中文分词配置,详情请查看:http://www.coreseek.cn/products-install/coreseek_mmseg/
#charset_dictpath = /usr/local/mmseg3/etc/ #BSD、Linux环境下设置,/符号结尾
charset_dictpath = E:/__Work/coreseek-4.1-win32/etc/ #Windows环境下设置,/符号结尾,最好给出绝对路径,例如:C:/usr/local/coreseek/etc/...
charset_type = zh_cn.utf-8
}
source article_photos : mysql
{
sql_query = SELECT art_id,caption FROM article_photos
sql_attr_uint = art_id
sql_query_info = SELECT * FROM article_photos WHERE art_id=$id
}
index article_photos
{
source = article_photos
path = E:/__Work/coreseek-4.1-win32/var/data/article_photos
docinfo = extern
mlock = 0
morphology = none
min_word_len = 1
html_strip = 0
charset_dictpath = E:/__Work/coreseek-4.1-win32/etc/ #Windows环境下设置,/符号结尾,最好给出绝对路径,例如:C:/usr/local/coreseek/etc/...
charset_type = zh_cn.utf-8
}
#全局index定义
indexer
{
mem_limit = 128M
}
#searchd服务定义
searchd
{
listen = 9312
read_timeout = 5
max_children = 30
max_matches = 1000
seamless_rotate = 0
preopen_indexes = 0
unlink_old = 1
pid_file = E:/__Work/coreseek-4.1-win32/var/log/searchd_mysql.pid #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
log = E:/__Work/coreseek-4.1-win32/var/log/searchd_mysql.log #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
query_log = E:/__Work/coreseek-4.1-win32/var/log/query_mysql.log #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
binlog_path = #关闭binlog日志
}
建立索引。启动服务
# 建立所有索引(或者执行下面两条)
E:/__Work/coreseek-4.1-win32/bin/indexer -c E:/__Work/coreseek-4.1-win32/bin/coreseek.conf --all --rotate
# 建立指定索引
E:/__Work/coreseek-4.1-win32/bin/indexer -c E:/__Work/coreseek-4.1-win32/bin/coreseek.conf articles
E:/__Work/coreseek-4.1-win32/bin/indexer -c E:/__Work/coreseek-4.1-win32/bin/coreseek.conf article_photos
# sphinx啟動
E:/__Work/coreseek-4.1-win32/bin/searchd -c E:/__Work/coreseek-4.1-win32/bin/coreseek.conf