导航:首页 > 编程语言 > sphinxphp安装

sphinxphp安装

发布时间:2022-07-05 02:02:18

A. mysql怎么与sphinx建立索引

安装sphinx
yum install sphinx libsphinxclient-devel

配置sphinx代替mysql全文索引(/etc/sphinx/sphinx.conf)

source account_src
{
type = mysql

sql_host = localhost
sql_user = root
sql_pass =
sql_db = test
sql_port = 3306
sql_query_pre = SET NAMES utf8

sql_query = \
SELECT * \
FROM tb_account

#sql_attr_uint = idx_id
sql_field_string = name
sql_attr_uint = age
}

index account_idx
{
source = account_src
path = /var/lib/sphinx/account_src
docinfo = extern
charset_type = utf-8
}

indexer
{
mem_limit = 32M
}

searchd
{
listen = 127.0.0.1:9312
listen = 9306:mysql41
log = /var/log/sphinx/searchd.log
query_log = /var/log/sphinx/query.log
read_timeout = 5
max_children = 30
pid_file = /var/run/sphinx/searchd.pid
max_matches = 1000
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
workers = threads # for RT to work
binlog_path = /var/lib/sphinx
}

创建全量索引
indexer --config /etc/sphinx/sphinx.conf --all

启动后台搜索服务
searchd --config /etc/sphinx/sphinx.conf

可设置crontab定期构建索引
indexer --config /etc/sphinx/sphinx.conf --rotate account_idx

测试(自带search工具已不可用,可参照这里),所以需要源码安装sphinx的php扩展

B. php windows下怎么安装sphinx,安装后又怎么使用

1、下载,我这里下的是“Win64 binaries w/MySQL+PgSQL+libstemmer+id64 support”,下载后文件名:sphinx-2.0.6-release-win64-id64-full.zip;
2、将其解压到D: \ sphinx,并在D:\sphinx下新建目录data(用来存放索引文件)与log(用来存放日志文件);
3、将D:\sphinx\sphinx.conf.in复制到D:\sphinx\bin\sphinx.conf.in,并重命名为sphinx.conf;
4、修改 D:\sphinx\bin\sphinx.conf 如下:
4.1、搜索source src1修改{...}中的内容
# 使用的数据库类型
type = mysql
# 服务器
sql_host = localhost
# 数据库登录名
sql_user = root
# 数据库登录密码
sql_pass = root
# 操作的数据库名称
sql_db = test
# 数据库服务器端口
sql_port = 3306
# 设置编码,如果用的是utf-8编码
sql_query_pre = SET NAMES utf-8
(以上7条前如有#将其删除)
4.2、搜索index test1修改{...}中的内容
# 放索引的目录
path = D:/sphinx/data/
# 编码
charset_type = utf-8
# 指定utf-8编码表
charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
# 简单分词,只有0和1,需要搜索中文必须置1
ngram_len = 1
# 需要分词的字符,搜索中文时必须
ngram_chars = U+3000..U+2FA1F
(以上5条前如有#将其删除)
5、导入测试数据将D:\sphinx\example.sql中语句执行到test数据库中,注意:test数据库创建时需要指定为utf-8格式;
6、打开cmd窗口,进入目录D:\sphinx\bin;
7、建立索引,执行indexer.exe test1,test1即为sphinx.conf中index test1
Sphinx 2.0.6-id64-release (r3473)
Copyright (c) 2001-2012, Andrew Aksyonoff
Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphin

using config file './sphinx.conf'...
indexing index 'test1'...
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 222 bytes
total 0.019 sec, 11252 bytes/sec, 202.74 docs/sec
total 2 reads, 0.000 sec, 0.2 kb/call avg, 0.0 msec/call avg
total 9 writes, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg
成功...
8、搜索'test',执行search.exe test
Sphinx 2.0.6-id64-release (r3473)
Copyright (c) 2001-2012, Andrew Aksyonoff
Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphinxsearch.com)

using config file './sphinx.conf'...
index 'test1': query 'test ': returned 3 matches of 3 total in 0.000 sec

displaying matches:
1. document=1, weight=2421, group_id=1, date_added=Tue Feb 26 10:25:31 2013
id=1
group_id=1
group_id2=5
date_added=2013-02-26 10:25:31
title=test one
content=this is my test document number one. also checking search within
phrases.
2. document=2, weight=1442, group_id=3, date_added=Tue Feb 26 10:25:31 2013
id=2
group_id=3
group_id2=6
date_added=2013-02-26 10:25:31
title= ????
content=this is my test document number two ???????
3. document=4, weight=1442, group_id=2, date_added=Tue Feb 26 10:25:31 2013
id=4
group_id=2
group_id2=8
date_added=2013-02-26 10:25:31
title=doc number four
content=this is to test groups

words:
1. 'test': 3 documents, 4 hits

index 'test1stemmed': search error: failed to open D:/sphinx/data/test1stemmed.s
ph: No such file or directory.
最后面的一句error可忽略;
9、搜索中文,首先将数据库中的内容update含有中文,执行sql语句:
UPDATE documents SET title='中文', content='this is my test document number one. also checking search within phrases.含有中文。' WHERE id=1;
UPDATE documents SET title='中文标题', content='this is my test document number one. also checking search within phrases.含有中文内容。' WHERE id=2;
UPDATE documents SET title='中文标题测试', content='this is my test document number one. also checking search within phrases.含有中文内容。' WHERE id=3;
重新建立索引(执行第7步);
然后执行search.exe 中文
Sphinx 2.0.6-id64-release (r3473)
Copyright (c) 2001-2012, Andrew Aksyonoff
Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphinxsearch.com)

using config file './sphinx.conf'...
indexing index 'test1'...
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 349 bytes
total 0.025 sec, 13808 bytes/sec, 158.26 docs/sec
total 2 reads, 0.000 sec, 0.3 kb/call avg, 0.0 msec/call avg
total 9 writes, 0.000 sec, 0.2 kb/call avg, 0.0 msec/call avg

D:\sphinx\bin>search.exe 中文
Sphinx 2.0.6-id64-release (r3473)
Copyright (c) 2001-2012, Andrew Aksyonoff
Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphinxsearch.com)

using config file './sphinx.conf'...
index 'test1': query '中文 ': returned 0 matches of 0 total in 0.000 sec

words:

index 'test1stemmed': search error: failed to open D:/sphinx/data/test1stemmed.s
ph: No such file or directory.
没有搜索到,因为windows命令行中的中文时GBK编码格式,所以没有匹配内容。我们可以使用PHP程序来试试;
10、进入D:\sphinx\api\目录,可以发现sphinx支持php、java、ruby调用,并提供对应的test例子,这里我们使用php来操作,首先将api复制到D:\www\下并重命名为sphinxapi,因为我本机上apache的web目录为D:\www,在D:\www\sphinxapi\下新建search.php,内容为:
<?php
require 'sphinxapi.php';
$s = new SphinxClient();
$s->SetServer('localhost', 9312);
$result = $s->Query('中国');
print_r($result);
echo '<br /><br />';
$result = $s->Query('中文');
print_r($result);
?>
然后回到cmd命令行中,开启sphinx服务,执行searchd.exe(这个必须要执行的)
Sphinx 2.0.6-id64-release (r3473)
Copyright (c) 2001-2012, Andrew Aksyonoff
Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphinxsearch.com)

using config file './sphinx.conf'...
WARNING: compat_sphinxql_magics=1 is deprecated; please update your application
and config
WARNING: preopen_indexes=1 has no effect with seamless_rotate=0
listening on all interfaces, port=9312
listening on all interfaces, port=9306
precaching index 'test1'
precaching index 'test1stemmed'
WARNING: index 'test1stemmed': preload: failed to open D:/sphinx/data/test1stemm
ed.sph: No such file or directory; NOT SERVING
precaching index 'rt'
WARNING: index 'rt': preload: failed to open @CONFDIR@/data/rt.lock: No such fil
e or directory; NOT SERVING
precached 3 indexes in 0.018 sec
成功...
然后在浏览器中执行http://localhost/sphinxapi/search.php,打印出来的数组结果可以很清晰的看见搜索的结果比对。。。
11、至此sphinx在windows下的简单安装与使用就完成了。

C. 使用了 sphinxapi.php 是不是不需要安装sphinx扩展了

类似的这种第三方功模块功能,不外乎就是要么安装php的扩展功能,然后直接调用其功能包含的内置方法,要么就是直接include其api文件,都是一样的道理,最后实例化,调用。

D. 如何调用Sphinx 和api接口

如何调用Sphinx

按上面配置,第5节点对数据库进行了索引,通过Sphinx自带的search(在bin/release目录)就可以在命令行进行搜索:

(搜索CGArt)
windows上:
search -c d:/sphinx/sphinx.conf CGArt
Linux上:
cd /usr/local/sphinx
./bin/search -c sphinx.conf CGArt
运行后,系统提示一堆信息:
....
....
words:
1. 'cgart': 36 documents, 189 hits
这个表示库中有36条记录符合要求,出现CGArt的有189处。应用程序如果想调用Sphinx,可以从两个方面:

一是通过Sphinx官方提供的API接口(接口有python,Java,Php三种版本)

二是通过安装SphinxSE(具体见1.2部分),然后创建一个中介sphinxSE类型的表,再通过执行特定的SQL语句实现。

通过官方API调用Sphinx(以PHP为例)

在sphinx安装目录有一个API目录,里面有三个PHP文件:test.php,test2.php和sphinxapi.php。 sphinxapi.php是sphinx调用接口封装文件,test.php是一个在命令行下执行的查询例子文件,test2.php是一个生成摘要的 例子文件。

在命令下行运行test.php(Linux上没有API目录,需要从源程序包中复制api目录至/usr/local/sphinx)

Windows上:
D:\sphinx\bin\release>c:\php5.2\php.exe -c c:\php5.2\php.ini ..\..\api\test.php -i cgfinal CGartLinux上(php在/usr/local/php目录,sphinx.conf在/usr/local/sphinx目录):

cd /usr/local/sphinx
/usr/local/php/bin/php api/test.php -i cgfinal CGArtSphinx的API查询接口主要有这些内容(其实对照 一下sphinxapi.php就清楚了):

//创建Sphinx的客户端接口对象
$cl = new SphinxClient ();

//设置连接Sphinx主机名与端口
$cl->SetServer('localhost',3312);

//可选,为每一个全文检索字段设置权重,主要根据你在sql_query中定义的字段的顺序,Sphinx系统以后会调整,可以按字段名称来设定权重
$cl->SetWeights ( array ( 100, 1 ) );

//设定搜索模式,SPH_MATCH_ALL,SPH_MATCH_ANY,SPH_MATCH_BOOLEAN,SPH_MATCH_EXTENDED,SPH_MATCH_PHRASE
$cl->SetMatchMode(SPH_MATCH_ALL);

//设定过滤条件$attribute是属性名,相当于字段名(用SPH_MATCH_EXTENDED时),$value是值,$exclude是布尔型,
当为true时,相当于$attribute!=$value,默认值是false
$cl->SetFilter($attribute, $values, $exclude);

//设定group by
//根据分组方法,匹配的记录集被分流到不同的组,每个组都记录着组的匹配记录数以及根据当前排序方法本组中的最佳匹配记录。
//最后的结果集包含各组的一个最佳匹配记录,和匹配数量以及分组函数值
//结果集分组可以采用任意一个排序语句,包括文档的属性以及sphinx的下面几个内部属性
//@id--匹配文档ID
//@weight, @rank, @relevance--匹配权重
//@group--group by 函数值
//@count--组内记录数量
//$groupsort的默认排序方法是@group desc,就是按分组函数值大小倒序排列
$cl->SetGroupBy($attribute, $func, $groupsort);

//设定order by的内容,第一个参数是排序方法名,值有
// SPH_SORT_RELEVANCE,SPH_SORT_ATTR_DESC,SPH_SORT_ATTR_ASC,SPH_SORT_TIME_SEGMENTS,SPH_SORT_EXTENDED
//$sortby的值如"HITS desc"
$cl->SetSortMode(SPH_SORT_EXTENDED, $sortby);

//set count-distinct attribute for group-by queries,$distinct为字符串
$cl->SetGroupDistinct ( $distinct );

//相当于mysql的limit $offset,$limit
$cl->SetLimits($start,$limit)

//$q是查询的关键字,$index是索引名称,当等于*时表查询所有索引

E. php 中文分词难题 sphinx

1.看一下bin目录是否存在search.exe
如果不存在,就不能用search命令,看你下载到的是什么版本,有些版本是没有这个文件的。
2.Query()的两个参数,第一个是搜索的关键词,第二个是索引。
没有结果的话,请确保你的数据中有那个关键词,还要确保索引存在,并配置正确。

以前弄这个的时候,我写了篇笔记:https://my.oschina.net/agui1989/blog/738392
有兴趣可以去看一下。

F. 在Ubuntu下安装好sphinx 2.0之后怎么测试是否安装成功呢

sphinx 2.0安装完成的时候,会有提示信息告知你安装结果的。也可以在sphinx 2.0安装好之后,找到一并安装的php、python等多种语言的测试脚本,可以通过这些脚本来检测sphinx是否安装成功。或者直接通过 /usr/bin/indexer --all 命令来建索引从而检查sphinx的运行状况,当然建索引的前提是你的索引配置文件都已经配置ok。

G. sphinx 在 windows下到底怎么安装

一、关于Sphinx

Sphinx 是一个在GPLv2 下发布的一个全文检索引擎,商业授权(例如, 嵌入到其他程序中)需要联系作者(Sphinxsearch.com)以获得商业授权。

一般而言,Sphinx是一个独立的搜索引擎,意图为其他应用提供高速、低空间占用、高结果相关度的全文搜索功能。Sphinx可以非常容易的与SQL数据库和脚本语言集成。

当前系统内置MySQL和PostgreSQL 数据库数据源的支持,也支持从标准输入读取特定格式的XML数据。通过修改源代码,用户可以自行增加新的数据源(例如:其他类型的DBMS的原生支持)。

搜索API支持PHP、Python、Perl、Rudy和Java,并且也可以用作MySQL存储引擎。搜索API非常简单,可以在若干个小时之内移植到新的语言上。

Sphinx特性:

* 高速的建立索引(在当代CPU上,峰值性能可达到10MB/秒);
* 高性能的搜索(在2–4GB的文本数据上,平均每次检索响应时间小于0.1秒);
* 可处理海量数据(目前已知可以处理超过100GB的文本数据,在单一CPU的系统上可处理100M文档);
* 提供了优秀的相关度算法,基于短语相似度和统计(BM25)的复合Ranking方法;
* 支持分布式搜索;
*
提供文件的摘录生成;
*
可作为MySQL的存储引擎提供搜索服务;
*
支持布尔、短语、词语相似度等多种检索模式;
*
文档支持多个全文检索字段(最大不超过32个);
*
文档支持多个额外的属性信息(例如:分组信息,时间戳等);
*
停止词查询;
*
支持单一字节编码和UTF-8编码;
*
原生的MySQL支持(同时支持MyISAM和InnoDB);
*
原生的PostgreSQL支持.

中文手册可以在这里获得(酷勤网备用下载地址:sphinx_doc_zhcn_0.9.pdf)。

二、Sphinx在windows上的安装

1.直接在http://www.sphinxsearch.com/downloads.html找到最新的windows版本,我这里下的是Win32 release binaries with MySQL support,下载后解压在D:\sphinx目录下;

2.在D:\sphinx\下新建一个data目录用来存放索引文件,一个log目录方日志文件,复制D:\sphinx\sphinx.conf.in到D:\sphinx\bin\sphinx.conf(注意修改文件名);

3.修改D:\sphinx\bin\sphinx.conf,我这里列出需要修改的几个:

type = mysql # 数据源,我这里是mysql
sql_host = localhost # 数据库服务器
sql_user = root # 数据库用户名
sql_pass = '' # 数据库密码
sql_db = test # 数据库
sql_port = 3306 # 数据库端口

sql_query_pre = SET NAMES utf8 # 去掉此行前面的注释,如果你的数据库是uft8编码的

index test1
{
# 放索引的目录
path = D:/sphinx/data/
# 编码
charset_type = utf-8
# 指定utf-8的编码表
charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
# 简单分词,只支持0和1,如果要搜索中文,请指定为1
ngram_len = 1
# 需要分词的字符,如果要搜索中文,去掉前面的注释
ngram_chars = U+3000..U+2FA1F
}

# index test1stemmed : test1
# {
# path = @CONFDIR@/data/test1stemmed
# morphology = stem_en
# }

# 如果没有分布式索引,注释掉下面的内容

# index dist1
# {
# 'distributed' index type MUST be specified
# type = distributed

# local index to be searched
# there can be many local indexes configured
# local = test1
# local = test1stemmed

# remote agent
# multiple remote agents may be specified
# syntax is 'hostname:port:index1,[index2[,...]]
# agent = localhost:3313:remote1
# agent = localhost:3314:remote2,remote3

# remote agent connection timeout, milliseconds
# optional, default is 1000 ms, ie. 1 sec
# agent_connect_timeout = 1000

# remote agent query timeout, milliseconds
# optional, default is 3000 ms, ie. 3 sec
# agent_query_timeout = 3000
# }

# 搜索服务需要修改的部分
searchd
{
# 日志
log = D:/sphinx/log/searchd.log

# PID file, searchd process ID file name
pid_file = D:/sphinx/log/searchd.pid

# windows下启动searchd服务一定要注释掉这个
# seamless_rotate = 1
}

4.导入测试数据

C:\Program Files\MySQL\MySQL Server 5.0\bin>mysql -uroot test<d:/sphinx/example.sql

5.建立索引

D:\sphinx\bin>indexer.exe –all
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff

using config file ‘./sphinx.conf’…
indexing index ‘test1′…
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 193 bytes
total 0.101 sec, 1916.30 bytes/sec, 39.72 docs/sec

D:\sphinx\bin>

6.搜索’test’试试

D:\sphinx\bin>search.exe test
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff

using config file ‘./sphinx.conf’…
index ‘test1′: query ‘test ‘: returned 3 matches of 3 total in 0.000 sec

displaying matches:
1. document=1, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008
id=1
group_id=1
group_id2=5
date_added=2008-11-26 14:58:59
title=test one
content=this is my test document number one. also checking search within
phrases.
2. document=2, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008
id=2
group_id=1
group_id2=6
date_added=2008-11-26 14:58:59
title=test two
content=this is my test document number two
3. document=4, weight=1, group_id=2, date_added=Wed Nov 26 14:58:59 2008
id=4
group_id=2
group_id2=8
date_added=2008-11-26 14:58:59
title=doc number four
content=this is to test groups

words:
1. ‘test’: 3 documents, 5 hits
D:\sphinx\bin>

都所出来了吧。

6.测试中文搜索

修改test数据库中documents数据表,

UPDATE `test`.`documents` SET `title` = ‘测试中文’, `content` = ‘this is my test document number two,应该搜的到吧’ WHERE `documents`.`id` = 2;

重建索引:

D:\sphinx\bin>indexer.exe –all

搜索’中文’试试:

D:\sphinx\bin>search.exe 中文
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff

using config file ‘./sphinx.conf’…
index ‘test1′: query ‘中文 ‘: returned 0 matches of 0 total in 0.000 sec

words:
D:\sphinx\bin>

貌似没有搜到,这是因为windows命令行中的编码是gbk,当然搜不出来。我们可以用程序试试,在D:\sphinx\api下新建一个foo.php的文件,注意utf-8编码

<?php
require ’sphinxapi.php’;
$s = new SphinxClient();
$s->SetServer(’localhost’,3312);
$result = $s->Query(’中文’);
var_mp($result);
?>

启动Sphinx searchd服务

D:\sphinx\bin>searchd.exe
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff

WARNING: forcing –console mode on Windows
using config file ‘./sphinx.conf’…
creating server socket on 0.0.0.0:3312
accepting connections

执行PHP查询:

php d:/sphinx/api/foo.php

H. php中如何使用sphinx搜索引擎

一. 安装、配置

1.ubuntu安装sphinx

如果没有安装aptitude ,需要先安装 aptitude

2.配置

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. 安装 aptitudeapt-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

6.在thinkphp中调用搜索

搜索完毕,返回结果(默认返回20条,修改返回条数用添加$s->SetLimits(0, 1000, 1000);),搜索速度相当快,索引80w条数据的title和description两个字段耗时不到10s,该搜索引擎支持增量索引,多种模式的搜索,网上的资料也非常多

I. 怎么查看php安装sphinx是否成功

如何调用Sphinx 按上面配置,第5节点对数据库进行了索引,通过Sphinx自带的search(在bin/release目录)就可以在命令行进行搜索: (搜索CGArt) windows上: search -c d:/sphinx/sphinx.conf CGArt Linux上: cd /usr/local/sphinx ./bin/sear...

阅读全文

与sphinxphp安装相关的资料

热点内容
编译程序输入一个字符串 浏览:404
圆命令画法 浏览:305
如果给电脑e盘文件加密 浏览:801
javaswing项目 浏览:774
androidsdksetup 浏览:1003
pdf怎么设置中文 浏览:126
安卓手机用什么软件看伦敦金 浏览:964
魅族文件夹无名称 浏览:789
苏黎世无人机算法 浏览:872
核桃编程和小码王的融资 浏览:684
微积分教材pdf 浏览:725
写python给微信好友发消息 浏览:336
蚊帐自营米加密 浏览:420
学校推荐核桃编程 浏览:804
湖南农信app怎么导明细 浏览:473
福特abs编程 浏览:509
如何自学安卓手机 浏览:439
以太坊源码共识机制 浏览:912
单片机探测器 浏览:872
demo编程大赛作品怎么运行 浏览:52