使用PHP和MySQL使用Sphinx的指南。

时间:2022-02-17 16:21:59

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.

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

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.

注:对于斯芬克斯的其他替代品,我也愿意接受建议。

4 个解决方案

#1


28  

Here is a very good Sphinx Tutorial from IBM.

这里有一个非常好的来自IBM的Sphinx教程。

#2


5  

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。安装狮身人面像

On Mac with Homebrew:

在Mac和自制程序:

brew install sphinx

On Amazon Linux (CentOS) with 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附带配置模板。寻找sphinx.conf。configs目录中的dist:

On Mac installed with Homebrew:

在Mac上安装Homebrew:

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

On Amazon Linux installed with yum:

在Amazon Linux上安装了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.

我将max_matches设置添加到这个配置中,因为在我完成所有工作之后,我的第一个问题是“为什么我总是只得到20个搜索结果?”使用max_matches,您可以设置搜索结果号的限制。

3. Create index using indexer

3所示。使用索引器创建索引

indexer --all

4. Run Sphinx daemon

4所示。狮身人面像守护进程运行

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

5. Install PHP Sphinx extension

5。安装PHP Sphinx扩展

On Mac with Homebrew:

在Mac和自制程序:

brew install homebrew/php/php56-sphinx

On Amazon Linux with 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:

要维持一个最新的指数,你可以使用两个指数:

  1. Main index, which is not updated often (once per week, month, etc)
  2. 主索引,不经常更新(每周一次,每月一次)
  3. And delta index, which updates often (every hour, 5 min, etc)
  4. 和delta指数,它经常更新(每小时,5分钟,等等)

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

每次重新索引delta index时,它都会与主索引合并

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:

我发现有用的链接:

#3


2  

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作为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论坛非常好,如果您需要帮助,应该提供给您。理查德。

#4


0  

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

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

#1


28  

Here is a very good Sphinx Tutorial from IBM.

这里有一个非常好的来自IBM的Sphinx教程。

#2


5  

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。安装狮身人面像

On Mac with Homebrew:

在Mac和自制程序:

brew install sphinx

On Amazon Linux (CentOS) with 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附带配置模板。寻找sphinx.conf。configs目录中的dist:

On Mac installed with Homebrew:

在Mac上安装Homebrew:

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

On Amazon Linux installed with yum:

在Amazon Linux上安装了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.

我将max_matches设置添加到这个配置中,因为在我完成所有工作之后,我的第一个问题是“为什么我总是只得到20个搜索结果?”使用max_matches,您可以设置搜索结果号的限制。

3. Create index using indexer

3所示。使用索引器创建索引

indexer --all

4. Run Sphinx daemon

4所示。狮身人面像守护进程运行

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

5. Install PHP Sphinx extension

5。安装PHP Sphinx扩展

On Mac with Homebrew:

在Mac和自制程序:

brew install homebrew/php/php56-sphinx

On Amazon Linux with 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:

要维持一个最新的指数,你可以使用两个指数:

  1. Main index, which is not updated often (once per week, month, etc)
  2. 主索引,不经常更新(每周一次,每月一次)
  3. And delta index, which updates often (every hour, 5 min, etc)
  4. 和delta指数,它经常更新(每小时,5分钟,等等)

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

每次重新索引delta index时,它都会与主索引合并

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:

我发现有用的链接:

#3


2  

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作为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论坛非常好,如果您需要帮助,应该提供给您。理查德。

#4


0  

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

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