首页 > php教程 > PHP开发 > 正文

LNMP+sphinx实现大数据秒查

高洛峰
发布: 2016-12-01 13:58:41
原创
1723人浏览过

sphinx是由俄罗斯人andrew aksyonoff开发的一个全文检索引擎。意图为其他应用提供高速、低空间占用、高结果 相关度的全文搜索功能。sphinx可以非常容易的与sql数据库和脚本语言集成。当前系统内置mysql和postgresql 数据库数据源的支持,也支持从标准输入读取特定格式 的xml数据。

Sphinx的特性如下:

a)  高速的建立索引(在当代CPU上,峰值性能可达到10 MB/秒);

b)  高性能的搜索(在2 – 4GB 的文本数据上,平均每次检索响应时间小于0.1秒);

c)  可处理海量数据(目前已知可以处理超过100 GB的文本数据, 在单一CPU的系统上可处理100 M 文档);

d)  提供了优秀的相关度算法,基于短语相似度和统计(BM25)的复合Ranking方法;

e)  支持分布式搜索;

f)  支持短语搜索

g)  提供文档摘要生成

h)  可作为MySQL的存储引擎提供搜索服务;

i)  支持布尔、短语、词语相似度等多种检索模式;

j)  文档支持多个全文检索字段(最大不超过32个);

k)  文档支持多个额外的属性信息(例如:分组信息,时间戳等);

l)  支持断词;

虽然mysql的MYISAM提供全文索引,但是性能却不敢让人恭维,另外数据库毕竟不是很善于做这样的事情,我们需要把这些活让给更适合的程序去做,减少数据库的压力。因此采用Sphinx来做mysql的全文索引工具是一个很好的选择。这个星期主要来学习这个这个工具的使用,下面将学习过程大致的记录一下,做个备忘,也希望能对学习这个工具的其他朋友有所启发。

安装sphinx

wget http://sphinxsearch.com/files/sphinx-2.2.11-release.tar.gz
tar -xf sphinx-2.2.11-release.tar.gz  && cd sphinx-2.2.11-release
./configure  --prefix=/usr/local/spinx --with-mysql
make && make install
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/
libsphinxclient 安装(PHP模块需要)
cd api/libsphinxclient
./configure –prefix=/usr/local/sphinx
make &&  make install
登录后复制

   

2.安装php扩展

wget http://pecl.php.net/get/sphinx-1.3.0.tgz
tar zxf sphinx-1.3.3.tgz && cd sphinx-1.3.3
./configure --with-php-config=/usr/local/php/bin/php-config --with-sphinx=/usr/local/sphinx/
make &&  make install
登录后复制

   

3.创建配置文件

cp /usr/local/sphinx/etc/sphinx-min.conf.dist  /usr/local/sphinx/etc/sphinx.conf
登录后复制

    

#
# Minimal Sphinx configuration sample (clean, simple, functional)
#
 
source src1
{
        type                    = mysql
 
        sql_host                = localhost
        sql_user                = root
        sql_pass                = www.123
        sql_db                  = test
        sql_port                = 3306  # optional, default is 3306
 
        sql_query               = \
                SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content \
                FROM documents
 
        sql_attr_uint           = group_id
        sql_attr_timestamp      = date_added
}
 
 
index test1
{
        source                  = src1
        path                    = /usr/local/spinx/var/data/test1
}
 
 
indexer
{
        mem_limit               = 32M
}
 
 
searchd
{
        listen                  = 9312
        listen                  = 9306:mysql41
        log                     = /usr/local/spinx/var/log/searchd.log
        query_log               = /usr/local/spinx/var/log/query.log
        read_timeout            = 5
        max_children            = 30
        pid_file                = /usr/local/spinx/var/log/searchd.pid
        seamless_rotate         = 1
        preopen_indexes         = 1
        unlink_old              = 1
        workers                 = threads # for RT to work
        binlog_path             = /usr/local/spinx/var/data
}
登录后复制

   

4.创建索引并启动

/usr/local/spinx/bin/indexer  -c /usr/local/spinx/etc/sphinx.conf --all
/usr/local/spinx/bin/searchd  -c /usr/local/spinx/etc/sphinx.conf
登录后复制

   

5.查询验证

cd /root/sphinx-2.2.11-release/api
python test.py  test
DEPRECATED: Do not call this method or, even better, use SphinxQL instead of an API
Query 'test ' retrieved 3 of 3 matches in 0.000 sec
Query stats:
        'test' found 5 times in 3 documents
Matches:
1. doc_id=1, weight=2, group_id=1, date_added=2016-11-30 01:21:20
2. doc_id=2, weight=2, group_id=1, date_added=2016-11-30 01:21:20
3. doc_id=4, weight=1, group_id=2, date_added=2016-11-30 01:21:20
登录后复制
mysql> select * from documents;
+----+----------+-----------+---------------------+-----------------+---------------------------------------------------------------------------+
| id | group_id | group_id2 | date_added          | title           | content                                                                   |
+----+----------+-----------+---------------------+-----------------+---------------------------------------------------------------------------+
|  1 |        1 |         5 | 2016-11-30 01:21:20 | test one        | this is my test document number one. also checking search within phrases. |
|  2 |        1 |         6 | 2016-11-30 01:21:20 | test two        | this is my test document number two                                       |
|  3 |        2 |         7 | 2016-11-30 01:21:20 | another doc     | this is another group                                                     |
|  4 |        2 |         8 | 2016-11-30 01:21:20 | doc number four | this is to test groups                                                    |
+----+----------+-----------+---------------------+-----------------+---------------------------------------------------------------------------+
登录后复制

   

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号