在 PHP 和 MySQL 中使用 Sphinx 的指南
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5036666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Guide to using Sphinx with PHP and MySQL
提问by Julio
I'm looking for a complete guide to using Sphinx with PHP and MySQL. I'd like one that's a bit simpler and easygoing than the one provided on the site.
我正在寻找在 PHP 和 MySQL 中使用 Sphinx 的完整指南。我想要一个比网站上提供的更简单和随和的。
I'm looking for a few concepts on how exactly it all works.
我正在寻找一些关于它究竟如何运作的概念。
I have a server with PHP, HTML, other data and a MySQL database. How would I go about setting up Sphinx to power the search and results being returned?
我有一台带有 PHP、HTML、其他数据和 MySQL 数据库的服务器。我将如何设置 Sphinx 以支持搜索和返回的结果?
I'd like to be able to pass my search terms to my PHP script and have it deal with Sphinx and return the data.
我希望能够将我的搜索词传递给我的 PHP 脚本并让它处理 Sphinx 并返回数据。
P.S. I'm also open to suggestion regarding any other alternatives to Sphinx.
PS 我也愿意接受有关 Sphinx 的任何其他替代品的建议。
回答by Ihor Burlachenko
I came across this post but didn't find an answer I wanted to see. So here is my Quick Start Guide:
我遇到了这篇文章,但没有找到我想看到的答案。所以这是我的快速入门指南:
1. Install Sphinx
1.安装Sphinx
On Mac with Homebrew:
在带有 Homebrew 的 Mac 上:
brew install sphinx
On Amazon Linux (CentOS) with yum:
在带有 yum 的 Amazon Linux (CentOS) 上:
yum install sphinx
2. Create Sphinx config
2. 创建 Sphinx 配置
Sphinx comes with config template. Look for sphinx.conf.dist in the configs directory:
Sphinx 带有配置模板。在 configs 目录中查找 sphinx.conf.dist:
On Mac installed with Homebrew:
在安装了 Homebrew 的 Mac 上:
/usr/local/Cellar/sphinx/<sphinx version>/etc
On Amazon Linux installed with yum:
在使用 yum 安装的 Amazon Linux 上:
/etc/sphinx
It is pretty straightforward but might contain too many settings for a newbie. In such case you can use this simple config:
它非常简单,但对于新手来说可能包含太多设置。在这种情况下,您可以使用这个简单的配置:
source TestSource {
type = mysql
sql_host = <host>
sql_user = <user>
sql_pass = <password>
sql_db = <db>
sql_query_range = select min(id), max(id) from TestTable
sql_range_step = 2048
sql_query = select id, some_info from TestTable\
where id >= $start and id <= $end
}
index TestIndex {
source = TestSource
path = /var/lib/sphinx/test-index
min_word_len = 3
min_infix_len = 3
}
searchd {
log = /var/log/sphinx/searchd.log
query_log = /var/log/sphinx/query.log
pid_file = /var/run/searchd.pid
max_matches = 200
listen = localhost:9312
}
I added max_matches setting to this config because my first question after I got everything working was "Why do I always get only 20 search results?". With max_matches you can set the limit for search results number.
我在这个配置中添加了 max_matches 设置,因为我在一切正常后的第一个问题是“为什么我总是只得到 20 个搜索结果?”。使用 max_matches 您可以设置搜索结果数量的限制。
3. Create index using indexer
3.使用索引器创建索引
indexer --all
4. Run Sphinx daemon
4. 运行 Sphinx 守护进程
sudo searchd -c /path/to/config/sphinx.conf
5. Install PHP Sphinx extension
5.安装PHP Sphinx扩展
On Mac with Homebrew:
在带有 Homebrew 的 Mac 上:
brew install homebrew/php/php56-sphinx
On Amazon Linux with yum:
在带有 yum 的 Amazon Linux 上:
yum install libsphinxclient
pecl install sphinx
6. Query your index from PHP
6. 从 PHP 查询您的索引
$index = new SphinxClient();
$index->setServer("127.0.0.1", 9312);
$result = $index->query('some search term', 'TestIndex');
print_r($result);
In case of any errors you can get more information with the following method:
如果出现任何错误,您可以使用以下方法获取更多信息:
$index->getLastError();
7. Keep up to date index
7. 保持最新索引
To maintain an up to date index you can use two indices:
要维护最新的索引,您可以使用两个索引:
- Main index, which is not updated often (once per week, month, etc)
- And delta index, which updates often (every hour, 5 min, etc)
- 主索引,不经常更新(每周一次、每月一次等)
- 以及 delta 索引,它经常更新(每小时、5 分钟等)
Every time delta index is re-indexed it is merged with the main index
每次重新索引 delta 索引时,它都会与主索引合并
Follow this link http://www.sphinxconsultant.com/sphinx-search-delta-indexing/to read more about this approach.
按照此链接http://www.sphinxconsultant.com/sphinx-search-delta-indexing/阅读有关此方法的更多信息。
Links I found useful:
我发现有用的链接:
- http://sphinxsearch.com/docs/current.html
- http://sphinxsearch.com/info/faq/
- http://atlchris.com/1996/working-with-sphinx-search-engine-on-a-lamp-linux-apache-mysql-and-php-stack-server/
- http://www.sphinxconsultant.com/sphinx-search-delta-indexing/
- https://github.com/schmittjoh/php-stubs/tree/master/res/php/sphinx
- http://sphinxsearch.com/docs/current.html
- http://sphinxsearch.com/info/faq/
- http://atlchris.com/1996/working-with-sphinx-search-engine-on-a-lamp-linux-apache-mysql-and-php-stack-server/
- http://www.sphinxconsultant.com/sphinx-search-delta-indexing/
- https://github.com/schmittjoh/php-stubs/tree/master/res/php/sphinx
回答by Richard Housham
I'm not too sure about a good guide but here are my steps.
我不太确定一个好的指南,但这是我的步骤。
a) Download and install it's quite straightforward
a) 下载并安装它非常简单
b) Make your first index - you need a source a location the given config is very good remember you can use a primary source to config all the main areas and then other sources stem off from that. Each source should start with the primary key and I find it works best to do key_id AS id
b) 创建您的第一个索引 - 您需要一个给定配置非常好的位置的源,请记住您可以使用主要源来配置所有主要区域,然后其他源由此衍生。每个源都应该从主键开始,我发现最好做 key_id AS id
c) Test you index using the search
c) 使用搜索测试您的索引
d) Start your search demon for sphinx - searchd this is what php will connect to and how it gets your results.
d) 开始搜索 sphinx - searchd 这是 php 将连接到的内容以及它如何获得结果。
e) Make a function to search all indexes pass in the index you want to search and it will return the ids in an array that have matched your search
e) 制作一个搜索所有索引的函数,传入您要搜索的索引,它将返回与您的搜索匹配的数组中的 id
f) Make a delta and updates.
f) 进行增量和更新。
Job done - the sphinx forum is very nice and should provide you if you need any help. Richard
工作完成 - sphinx 论坛非常好,如果您需要任何帮助,应该会提供给您。理查德
回答by Dong3000
Take a look at Search Engine Extensionsat php.net: http://php.net/manual/en/refs.search.php.
在 php.net 上查看搜索引擎扩展:http: //php.net/manual/en/refs.search.php。
回答by Manticore Search
http://play.manticoresearch.com/is a set of interactive courses that will walk you through different tasks people come across when using Sphinx/Manticore and how they can be solved.
http://play.manticoresearch.com/是一组互动课程,将引导您了解人们在使用 Sphinx/Manticore 时遇到的不同任务以及如何解决这些任务。