Home > Backend Development > PHP Tutorial > Detailed explanation and usage of Sphinx PHP extension

Detailed explanation and usage of Sphinx PHP extension

王林
Release: 2023-10-03 09:32:01
Original
913 people have browsed it

Sphinx PHP 扩展详解及使用方法

Sphinx PHP extension detailed explanation and usage

Introduction:
Sphinx is an open source full-text search engine, which is widely used in the search function of medium and large websites accomplish. For better integration with the PHP language, Sphinx provides a PHP extension for developer convenience.
This article will introduce in detail the purpose, installation steps, and sample codes of various functions and usages of the Sphinx PHP extension to help readers better understand and use the Sphinx PHP extension.

1. Install the Sphinx PHP extension:

  1. Install the SphinxAPI library:
    First, we need to install the SphinxAPI library. This can be achieved by executing the following command in the terminal:

    $ cd /path/to/sphinx/source
    $ ./configure
    $ make
    $ sudo make install
    Copy after login
  2. Install the Sphinx PHP extension:
    After we have installed the SphinxAPI library, we can start installing the Sphinx PHP extension. You can install it through the PECL command line tool and execute the following command:

    $ pecl install sphinx
    Copy after login

After successful installation, you need to add the following configuration line to the php.ini file:

extension=sphinx.so
Copy after login

Restart web server to make the configuration take effect.

2. Basic usage of Sphinx PHP extension:

  1. Connect to Sphinx server:
    First, we need to use the SphinxClient class to connect to the Sphinx server. This can be achieved through the following code:

    <?php
    $client = new SphinxClient();
    $client->SetServer('localhost', 9312);
    Copy after login
  2. Set search options:
    Setting search options is one of the important steps in using Sphinx for full-text search. Here are some commonly used options:

    <?php
    $client->SetMatchMode(SPH_MATCH_EXTENDED2);
    $client->SetSortMode(SPH_SORT_RELEVANCE);
    $client->SetLimits(0, 10, 1000);
    $client->SetFilter('category', array(1, 2, 3));
    $client->SetFieldWeights(array('title' => 5, 'content' => 1));
    Copy after login
  3. Send a query request:
    Sending a query request is one of the key steps for full-text search using Sphinx. The following is a sample code for sending a query request:

    <?php
    $result = $client->Query('keyword');
    if ($result === false) {
     echo "搜索失败:" . $client->GetLastError();
    } else {
     // 处理搜索结果
     foreach ($result['matches'] as $doc) {
         echo "文档ID:" . $doc['id'] . "
    ";
         echo "文档权重:" . $doc['weight'] . "
    ";
     }
    }
    Copy after login

3. Common functions of Sphinx PHP extension:

  1. Use Sphinx for paging:
    The paging function is very useful when there are many search results. The following is a sample code for pagination using Sphinx:

    <?php
    $client->SetLimits($offset, $limit);
    Copy after login
  2. Sort using Sphinx:
    The sorting function can sort search results according to different needs. The following is a sample code for sorting using Sphinx:

    <?php
    $client->SetSortMode(SPH_SORT_RELEVANCE);
    $client->SetSortMode(SPH_SORT_ATTR_DESC, 'timestamp');
    $client->SetSortMode(SPH_SORT_EXTENDED, '@relevance DESC, @id ASC');
    Copy after login
  3. Using Sphinx for field filtering:
    The field filtering function can further limit the search results. The following is a sample code for field filtering using Sphinx:

    <?php
    $client->SetFilter('category_id', array(1, 2, 3));
    $client->SetFilterRange('price', 100, 300);
    $client->SetFilterFloatRange('rating', 4.0, 5.0);
    $client->SetFilterString('brand', array('Apple', 'Samsung'));
    Copy after login

4. Summary:
This article introduces the use of Sphinx PHP extension in detail, including installation steps, basic usage and common usage Function. By studying this article, readers can better understand and use the Sphinx PHP extension to achieve efficient full-text search functionality. I hope this article can be helpful to everyone.

The above is the detailed content of Detailed explanation and usage of Sphinx PHP extension. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template