Sphinx writes efficient search function example analysis

WBOY
Release: 2023-10-03 09:16:02
Original
769 people have browsed it

Sphinx 编写高效的搜索功能实例解析

Sphinx is an open source full-text search engine that can efficiently handle large-scale text search needs. This article will use an example to analyze how to use Sphinx to write efficient search functions and provide specific code examples.

  1. Installing Sphinx
    First, we need to install Sphinx on the server. You can download the latest version of the installer from the Sphinx official website (https://sphinxsearch.com/) and install it according to the instructions.
  2. Create index
    Before we start writing the search function, we first need to create an index to store the text data that needs to be searched. Suppose our application needs to search for information about some products. Each product contains attributes such as title, description, and price. We can create an index called "products".

    First, create a MySQL database connected to Sphinx and import product data. Suppose we have a database called "products" and contains a table called "product_info" that contains fields such as the product's title, description, and price.

    Next, we need to create a configuration file suitable for the "products" index. Create a file named "products.conf" and add the following content:

    source products
    {
        type = mysql
        sql_host = localhost
        sql_user = username
        sql_pass = password
        sql_db = products
        sql_port = 3306
    
        sql_query = SELECT id, title, description, price FROM product_info
        sql_attr_uint = price
    }
    
    index products
    {
        source = products
        path = /path/to/index/products
        charset_type = utf-8
    }
    Copy after login

    This specifies the connection information and query statement for the MySQL database. "sql_attr_uint = price" means that the price field is an attribute of unsigned integer type.

    Finally, use the following command to create the index:

    $ indexer --config /path/to/products.conf --all
    Copy after login
  3. Writing the search function
    Now, we can start writing the search function of Sphinx. The following is a simple PHP example:

    <?php
    require('sphinxapi.php');
    
    $sphinx = new SphinxClient();
    $sphinx->setServer('localhost', 9312);
    
    $keyword = $_GET['keyword'];
    
    $sphinx->setMatchMode(SPH_MATCH_ANY);
    $sphinx->setSortMode(SPH_SORT_RELEVANCE);
    
    $result = $sphinx->query($keyword, 'products');
    
    if ($result['total'] > 0) {
        foreach ($result['matches'] as $match) {
            echo 'ID: ' . $match['id'] . '<br>';
            echo 'Title: ' . $match['attrs']['title'] . '<br>';
            echo 'Description: ' . $match['attrs']['description'] . '<br>';
            echo 'Price: ' . $match['attrs']['price'] . '<br><br>';
        }
    } else {
        echo 'No results found.';
    }
    ?>
    Copy after login

    This code first creates a SphinxClient object and specifies the address and port number of the Sphinx server. Then, receive the keywords entered by the user and set the matching mode and sorting mode.

    Finally, call the query() method to perform the search, and loop through the results to print out the matching product information.

  4. Run the test
    Save the above code as a PHP file (such as search.php) and deploy it to the web server. You can then search by accessing search.php?keyword=keyword.

    For example, visiting http://yourdomain.com/search.php?keyword=mobile phone will search for products containing the keyword "mobile phone" and display the results on the page.

Through the above steps, we have implemented an efficient search function based on Sphinx. Sphinx provides a wealth of configuration options and query syntax to optimize search performance according to actual needs. We hope that the code examples provided in this article can help readers understand and use Sphinx to write efficient search functions.

The above is the detailed content of Sphinx writes efficient search function example analysis. 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