php 基于web的搜索引擎,php中使用sphinx搜索引擎 Web程序 - 贪吃蛇学院-专业IT技术平台...

本文详细介绍了如何在Ubuntu上安装配置Sphinx搜索引擎,包括设置MySQL源、创建索引并执行分词,以及如何在PHP中利用SphinxClient进行搜索。重点涉及了配置文件编写和PHP扩展的安装过程。
摘要由CSDN通过智能技术生成

sphinx是一个高效的搜索引擎,分词搜索的速度比较快,索引建立存储在硬盘文件,不会干扰数据库,有自己内置的一套数据库。

一. 安装、配置

1.ubuntu安装sphinx

如果没有安装aptitude ,需要先安装 aptitude 因为因为用apt get install 安装下面命令会出现问题.

sudo apt-get install aptitude

sudo aptitude install sphinx3 sphinx3-doc sphinxsearch sphinx-common -y

2.配置

cd /etc/sphinxsearch/

cp sphinx.conf.sample sphinx.conf

修改配置文件如下 #配置源 source sphinx_t0#数据库名_数据表名,每配置一个数据表,都需要写上一个配置源{ type =mysql#数据库类型sql_host =localhost sql_user =root sql_pass = 123123sql_db = sphinx#指定数据库 sql_port = 3306#optional, default is 3306 sql_sock = /tmp/mysql.sock#mysql接口#从数据库之中读取数据的SQL语句设置#在这里尽可能不使用where或groupby,#将where与groupby的内容交给sphinx,由sphinx进行条件过滤与groupby效率会更高#注意:select的字段必须包括一个唯一主键以及要全文检索的字段(可以有多个)、输出的字段。#where中要用到的字段也要select出来#例:#在配置sql语句,可以先写出一句预计要执行的sql语句,然后根据sphinx规定设置#select * from t0 where description like ‘%广州%‘ or name like ‘%s%‘#=> select id,description,name,age from t0sql_query =SELECT id, name, age, description,group_id,date_added FROM t0 sql_attr_uint =age#使用sql_attr设置的字段(搜索条件),只能作为属性,使用SphinxClient::SetFilter()进行过滤;#未被设置的字段,自动作为全文检索的字段,使用SphinxClient::Query("搜索字符串")进行全文搜索#sql_query第一列id需为整数,且被系统使用,无需再设置sql_attr_uintsql_attr_uint =group_id sql_attr_timestamp = date_added#定义不同类型的字段要用不同的属性名,比如上面的sql_attr_timestamp就是时间戳类型#sql_query_info = SELECT * FROM documents WHERE id=$id #命令行查询时,从数据库读取原始数据信息#在执行sql_query前执行的sql命令, 可以有多条 sql_query_pre = SET NAMES utf8#执行sql字符编码}#索引,每一源需要一个索引 index sphinx_t0#索引名字一般与配置源一致{ source = sphinx_t0#source 关联源 path = /usr/local/coreseek/var/data/sphinx_t0#索引文件存放路径,每个索引文件一个 docinfo =extern charset_dictpath = /usr/local/mmseg/etc/#指明分词法读取词典文件的位置,当启用分词法时,为必填项。在使用LibMMSeg作为分词 库时,需要确保词典文件uni.lib在指定的目录下 charset_type = zh_cn.utf-8#字符编码}#索引,控制所有索引indexer { mem_limit = 512M#内存}#sphinx守护进程配置searchd { port = 9312#端口log = /usr/local/coreseek/var/log/searchd.logquery_log = /usr/local/coreseek/var/log/query.logread_timeout = 5#超时 max_children = 30#最大连接数 pid_file = /usr/local/csft/var/log/searchd.pid#pid文件路径 max_matches = 1000#max_matches最大匹配数,也就是查找的数据再多也只返回这里设置的1000条 seamless_rotate = 1preopen_indexes = 0unlink_old = 1}

3.执行命令分词,会在/var/lib/sphinxsearch/data/test1 目录下生成一堆索引文件

sudo indexer -c /etc/sphinxsearch/sphinx.conf test1

test1为上述配置文件的index名字

4.命令行测试搜索

sudo search -c /etc/sphinxsearch/sphinx.conf google

二.在php中使用,安装php、sphinx的依赖库

1.安装 aptitude

apt-get install aptitude

sudo aptitude install libsphinxclient-dev libsphinxclient-0.0.1 -y

2.安装php sphinx的扩展

安装 pecl

sudo apt-get install php-pear php5-dev

在安装sphinx

sudo pecl install sphinx

3.在配置文件php.ini中添加sphinx的扩展,

我的php.ini文件为

sudo vim /etc/php5/fpm/php.ini

获取自己的php.ini文件位置使用

php5-fpm -i|grep ini

添加:

extension=sphinx.so

4.重启php5-fpm,查看php是否加载sphinx模块

sudo /etc/init.d/php5-fpm restart

5.将search程序运行在后台

sudo searchd -c /etc/sphinxsearch/sphinx.conf

默认监听配置文件中的端口:9312

PHP代码:

$list= array(); if(!empty($_POST)){ $sc = new SphinxClient(); //实例化Api

$sc->setServer(‘192.168.169.128‘, 9312); //设置服务端,第一个参数sphinx服务器地址,第二个sphinx监听端口

$res = $sc->query($_POST[‘key‘], ‘test1‘); //执行查询,第一个参数查询的关键字,第二个查询的索引名称,mysql索引名

称(这个也是在配置文件中定义的),多个索引名称以,分开,也可以用*表示所有索引。 //print_r($sc);

echo ‘

‘;     print_r($res);     echo ‘
‘; exit;} ?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值