thinkphp使用sphinx使用
晚来的国庆快乐,向大家抱歉,国庆在家玩了几天,接到任务,优化搜索,
然后怎么办呢,在家里要打扫卫生,要做饭,忙不过啊,于是晚上搞到半夜1,2点查阅sphinx资料
当然是一大堆百度啊,前人栽树,后人乘凉啊,
苦心人天不负百二秦关终属楚,无薪尝胆可吞吴,不多扯,下面就让我为大家介绍windows的sphinx的使用.
first step:
在sphinx官网上下载sphinx
http://sphinxsearch.com/downloads/archive/
选择对应的版本download,
随后下载sphinx.dll,php拓展,然后查看phpinfo();
记住标箭头的地方 nts代表非线程安全
然后打开Package :: sphinx这个网站下载sphinx拓展
要选和上面标箭头一样的,php版本也要对
下载好之后找到php目录将sphinx.dll放到php目录下
然后编辑php.ini
增加 extension=php_sphinx.dll
然后重启nginx;再次查看phpinfo();
sphinx 拓展已经装好了
其实没有也是一样的
second step:
配置:下载好了之后我们将
sphinx-3.0.3\api\ruby\spec\sphinx\sphinx.conf 复制到
sphinx-3.0.3\bin 下,
然后进行一系列配置
source src1
{
type = mysql
sql_host = 127.0.0.1
sql_user = root
sql_pass = hjhj157
sql_db = emts
sql_port = 3306
sql_query = SELECT * FROM em_eqpt_fill
sql_attr_uint = id
#sql_attr_timestamp = CreateDate
sql_attr_float = Measure,Amount
#在attr中显示出数据
sql_field_string = EqptId
sql_field_string = Measure
sql_field_string = Amount
sql_field_string = eqptids
sql_field_string = FillType
sql_field_string = CreateDate
sql_field_string = openid
#sql_query_pre 在执行sql_query前执行的sql命令, 可以有多条。
## 试想一下,有一个文章系统,每篇文章都有多个标签,这个文章就叫做多值属性。
## 我要对某个标签进行查询过滤,那么在建立查询的时候就应该把这个标签的值放入到索引中。
## 这个字段,sql_attr_multi就是用来做这个事情的。
#sql_attr_multi = uint tags from query; SELECT link_id, tag_id FROM links_tags
sql_query_info = SELECT * FROM em_eqpt_fill WHERE EqptId=$id and CreateDate <=$create_date1 and CreateDate>=$create_date2
}
source src2
{
type = mysql
sql_host = 127.0.0.1
sql_user = root
sql_pass = hjhj157
sql_db = emts
sql_port = 3306
sql_query = SELECT * FROM em_eqpt_fill
sql_query_info = SELECT * FROM em_eqpt_fill WHERE EqptId=$id and CreateDate <=$create_date1 and CreateDate>=$create_date2
sql_attr_uint = id
sql_attr_timestamp = CreateDate
sql_query_info = SELECT * FROM em_eqpt_fill WHERE EqptId=$id and CreateDate <=$create_date1 and CreateDate>=$create_date2
sql_attr_float = Measure,Amount
}
index test1
{
source = src1
#存放索引目录
path = D:/sphinx/data/test1/hhh #没有目录要新建
docinfo = extern
morphology = none
stopwords =
charset_type = utf-8
}
index test2
{
source = src2
path = D:/sphinx/data/test2/hhh #没有目录要新建
docinfo = extern
morphology = none
stopwords =
charset_type = utf-8
}
indexer
{
mem_limit = 32M
}
searchd
{
#listen = 9312
listen = 127.0.0.1:9306:mysql41
log = D:/sphinx/data/log/searchd.log
query_log = D:/sphinx/data/log/query.log
read_timeout = 5
max_children = 30
pid_file = searchd.pid #如果没有就在bin目录下新建
#pid_file = /opt/sphinx-0.9.9/var/log/searchd.pid
max_matches = 1000
}
这里讲解下配置
source mysql{} 定义源名称为mysql,也可以叫其他的,比如:source xxx{}type 数据源类型 。
sql_* 数据相关的配置,比如sql_host,sql_pass什么的,这些不解释。
sql_query 建立索引时的查询命令,在这里尽可能不使用where或group by,将where与groupby的内容交给sphinx,由sphinx进行条件过滤与groupby效率会更高,注意:select 的字段必须包括一个唯一主键以及要全文检索的字段,where中要用到的字段也要select出来
sql_query_pre 在执行sql_query前执行的sql命令, 可以有多条。
sql_attr 以这个开头的配置项,表示属性字段,在where,orderby,groupby中出现的字段要分别定义一个属性,定义不同类型的字段要用不同的属性名,比如上面的sql_attr_timestamp就是时间戳类型。
index mysql{} 定义索引名称为mysql,也可以叫其他的,比如:index xxx{}
source 关联源,就是source xxx定义的。
path 索引文件存放路径,比如:E:/coreseek/var/data/mysql 实际存放在E:/coreseek/var/data/目录,然后创建多个名称为mysql后缀却不同的索引文件
charset_dictpath 指明分词法读取词典文件的位置,当启用分词法时,为必填项。在使用LibMMSeg作为分词 库时,需要确保词典文件uni.lib在指定的目录下
charset_type 字符集,比如charset_type = zh_cn.gbk
searchd{} sphinx守护进程配置
listen 监听端口
max_matches最大匹配数,也就是查找的数据再多也只返回这里设置的1000条
pid_file pid文件路径
log全文检索日志
query_log查询日志
然后打开cmd
到sphinx安装目录下,既然都学sphinx了,这里用命令进入指定目录我就不讲了
运行 indexer -c sphinx.conf --all (all 代表配置文件全部索引,如果不想的话就用indexer -c sphinx.conf 索引名称)
然后运行开启searchd
searchd -c sphinx.config --install
如果已经安装则会报如下错误
然后searchd -c sphinx.config
如果出现了sphinxdata exists not use ***
则吧bin/sphinxdata整个目录删掉就好了,我就是被这个坑难了很久
然后将其与框架结合
由于我用的是thinkphp框架
在sphinx安装目录下找到 api下面的sphinxapi.php
里面有一个名为 SphinxClient的类
在sphinxapi.php加入 namespace //申明命名空间再更名为SphinxClient.php
类名与文件名一致然后将其丢入tinnkphp目录下extend/org/下作为拓展
当然如果你的配置文件的拓展目录不是extend 就放到你配置的下面去咯
然后使用api 快速查询数据
if (false){//隔断代码 想看api接口是可以改为true
$sc = new SphinxClient();
$sc->setServer('127.0.0.1',9312); // 设置服务器
$sc->setSelect("*");
$sc->SetGroupBy('CreateDate', SPH_GROUPBY_ATTR, 'CreateDate desc');
$res = $sc->Query(30332,'test1'); // 第一个参数是查询内容,第二个参数是索引的名称(sphinx.conf里面配置)
$order_column = 'CreateDate DESC';//排序规则
}
然后用代码获取
配置数据连接配置
这里就讲tp 使用sphinxQL
'database_config2'=>[
'type' => 'mysql',
// 服务器地址
'hostport' => '9306',
'hostname' => '127.0.0.1',
'username' => '',
// 密码
'password' => '',
]
$db =Db::connect(config('database_config2'),true);
$res =$db->query("select * from test1 where match ('2018') limit 100,100000");
echo "<pre>";
print_r($res);
echo "</pre>";exit;
这就都查出来了,看到这其实这里,是不是感觉很神奇啊
赶紧下载一个sphinx压缩包动手试试吧,接下来第二期会出linux 搭建sphinx文章
更多文章可以去知乎看我的文章:刚来****写,入行不就请大佬不喜勿喷