将 Sphinx 与 PHP 和 MySQL 结合使用的指南

发布于 2024-10-17 17:20:33 字数 253 浏览 4 评论 0原文

我正在寻找使用 Sphinx 与 PHP 和 MySQL 的完整指南。我想要一个比网站上提供的更简单、更随和的版本。

我正在寻找一些关于它到底如何运作的概念。

我有一个带有 PHP、HTML、其他数据和 MySQL 数据库的服务器。我将如何设置 Sphinx 来支持搜索和返回结果?

我希望能够将搜索词传递给 PHP 脚本并让它处理 Sphinx 并返回数据。

PS 我也愿意接受有关 Sphinx 的任何其他替代方案的建议。

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.

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?

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.

P.S. I'm also open to suggestion regarding any other alternatives to Sphinx.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

失与倦" 2024-10-24 17:20:33

我看到这篇文章,但没有找到我想看到的答案。这是我的快速入门指南:

1。 使用 Homebrew 在 Mac 上安装 Sphinx

brew install sphinx

在 Amazon Linux (CentOS) 上使用 yum 安装:

yum install sphinx

2.创建Sphinx配置

Sphinx附带配置模板。在 configs 目录中查找 sphinx.conf.dist:

在安装了 Homebrew 的 Mac 上:

/usr/local/Cellar/sphinx/<sphinx version>/etc

在安装了 yum 的 Amazon Linux 上:

/etc/sphinx

它非常简单,但对于新手来说可能包含太多设置。在这种情况下,您可以使用这个简单的配置:

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
}

我向此配置添加了 max_matches 设置,因为在一切正常运行后我的第一个问题是“为什么我总是只得到 20 个搜索结果?”。使用 max_matches 您可以设置搜索结果数量的限制。

3.使用索引器创建索引

indexer --all

4.运行Sphinx守护进程

sudo searchd -c /path/to/config/sphinx.conf

5。安装 PHP Sphinx 扩展

在 Mac 上使用 Homebrew:

brew install homebrew/php/php56-sphinx

在 Amazon Linux 上使用 yum:

yum install libsphinxclient
pecl install sphinx

6。从 PHP 查询索引

$index = new SphinxClient();
$index->setServer("127.0.0.1", 9312);

$result = $index->query('some search term', 'TestIndex');

print_r($result);

如果出现任何错误,您可以使用以下方法获取更多信息:

$index->getLastError();

7.保持最新索引

要保持最新索引,您可以使用两个索引:

  1. 主索引,不经常更新(每周、每月一次等)
  2. 和增量索引,经常更新(每小时、 5 分钟等)

每次重新索引增量索引时,它都会与主索引合并

按照此链接 http://www.sphinxconsultant.com/sphinx-search-delta-indexing/ 了解有关此方法的更多信息。

我发现有用的链接:

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

On Mac with Homebrew:

brew install sphinx

On Amazon Linux (CentOS) with yum:

yum install sphinx

2. Create Sphinx config

Sphinx comes with config template. Look for sphinx.conf.dist in the configs directory:

On Mac installed with Homebrew:

/usr/local/Cellar/sphinx/<sphinx version>/etc

On Amazon Linux installed with yum:

/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.

3. Create index using indexer

indexer --all

4. Run Sphinx daemon

sudo searchd -c /path/to/config/sphinx.conf

5. Install PHP Sphinx extension

On Mac with Homebrew:

brew install homebrew/php/php56-sphinx

On Amazon Linux with yum:

yum install libsphinxclient
pecl install sphinx

6. Query your index from 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

To maintain an up to date index you can use two indices:

  1. Main index, which is not updated often (once per week, month, etc)
  2. And delta index, which updates often (every hour, 5 min, etc)

Every time delta index is re-indexed it is merged with the main index

Follow this link http://www.sphinxconsultant.com/sphinx-search-delta-indexing/ to read more about this approach.

Links I found useful:

千年*琉璃梦 2024-10-24 17:20:33

我不太确定什么是好的指南,但这是我的步骤。

a) 下载并安装它非常简单

b) 创建您的第一个索引 - 您需要一个源,一个位置,给定的配置非常好,记住您可以使用主要源来配置所有主要区域,然后其他源从中衍生出来。每个源都应该以主键开头,我发现最好执行 key_id AS id

c) 使用搜索测试索引

d) 启动 sphinx 的搜索恶魔 - searchd 这是 php 将连接到的内容以及它如何获取结果。

e) 创建一个函数来搜索所有索引,传入您要搜索的索引,它将返回与您的搜索匹配的数组中的 id

f) 进行增量并更新。

工作完成 - sphinx 论坛非常好,如果您需要任何帮助,应该可以为您提供。
理查德

I'm not too sure about a good guide but here are my steps.

a) Download and install it's quite straightforward

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

c) Test you index using the search

d) Start your search demon for sphinx - searchd this is what php will connect to and how it gets your results.

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

f) Make a delta and updates.

Job done - the sphinx forum is very nice and should provide you if you need any help.
Richard

倦话 2024-10-24 17:20:33

查看 php.net 上的搜索引擎扩展http: //php.net/manual/en/refs.search.php

Take a look at Search Engine Extensions at php.net: http://php.net/manual/en/refs.search.php.

倒数 2024-10-24 17:20:33

http://play.manticoresearch.com/ 是一组交互式课程,将引导您完成不同的任务人们在使用 Sphinx/Manticore 时遇到的问题以及如何解决这些问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文