search
HomeBackend DevelopmentPHP TutorialSphinx writes efficient search function example analysis

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
    }

    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
  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.';
    }
    ?>

    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!

Statement
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
如何使用C#编写布隆过滤器算法如何使用C#编写布隆过滤器算法Sep 21, 2023 am 10:24 AM

如何使用C#编写布隆过滤器算法布隆过滤器(BloomFilter)是一种空间效率非常高的数据结构,可以用于判断一个元素是否属于集合。它的基本思想是通过多个独立的哈希函数将元素映射到一个位数组中,并将对应位数组的位标记为1。当判断一个元素是否属于集合时,只需要判断对应位数组的位是否都为1,如果有任何一位为0,则可以判定元素不在集合中。布隆过滤器具有快速查询和

编写C语言中计算幂函数的方法编写C语言中计算幂函数的方法Feb 19, 2024 pm 01:00 PM

如何在C语言中编写乘方函数乘方(exponentiation)是数学中常用的运算,表示将一个数自乘若干次的操作。在C语言中,我们可以通过编写一个乘方函数来实现这个功能。下面将详细介绍如何在C语言中编写乘方函数,并给出具体的代码示例。确定函数的输入和输出乘方函数的输入通常包含两个参数:底数(base)和指数(exponent),输出为计算得到的结果。因此,我们

如何使用C#编写动态规划算法如何使用C#编写动态规划算法Sep 20, 2023 pm 04:03 PM

如何使用C#编写动态规划算法摘要:动态规划是求解最优化问题的一种常用算法,适用于多种场景。本文将介绍如何使用C#编写动态规划算法,并提供具体的代码示例。一、什么是动态规划算法动态规划(DynamicProgramming,简称DP)是一种用来求解具有重叠子问题和最优子结构性质的问题的算法思想。动态规划将问题分解成若干个子问题来求解,通过记录每个子问题的解,

如何使用C++编写一个简单的酒店预订系统?如何使用C++编写一个简单的酒店预订系统?Nov 03, 2023 am 11:54 AM

酒店预订系统是一种重要的信息管理系统,它可以帮助酒店实现更高效的管理和更良好的服务。如果你想学习如何使用C++来编写一个简单的酒店预订系统,那么本文将为您提供一个基本的框架和详细的实现步骤。酒店预订系统的功能需求在开发酒店预订系统之前,我们需要确定其实现的功能需求。一个基本的酒店预订系统至少需要实现以下几个功能:(1)客房信息管理:包括客房类型、房间号、房

如何使用C++编写一个简单的学生选课系统?如何使用C++编写一个简单的学生选课系统?Nov 02, 2023 am 10:54 AM

如何使用C++编写一个简单的学生选课系统?随着科技的不断发展,计算机编程已经成为了一种必备的技能。而在学习编程的过程中,一个简单的学生选课系统可以帮助我们更好地理解和应用编程语言。在本文中,我们将介绍如何使用C++编写一个简单的学生选课系统。首先,我们需要明确这个选课系统的功能和需求。一个基本的学生选课系统通常包含以下几个部分:学生信息管理、课程信息管理、选

如何用Python编写KNN算法?如何用Python编写KNN算法?Sep 19, 2023 pm 01:18 PM

如何用Python编写KNN算法?KNN(K-NearestNeighbors,K近邻算法)是一种简单而常用的分类算法。它的思想是通过测量不同样本之间的距离,将测试样本分类到最近的K个邻居中。本文将介绍如何使用Python编写并实现KNN算法,并提供具体的代码示例。首先,我们需要准备一些数据。假设我们有一组二维的数据集,每个样本都有两个特征。我们将数据集分

如何通过C++编写一个简单的扫雷游戏?如何通过C++编写一个简单的扫雷游戏?Nov 02, 2023 am 11:24 AM

如何通过C++编写一个简单的扫雷游戏?扫雷游戏是一款经典的益智类游戏,它要求玩家根据已知的雷区布局,在没有踩到地雷的情况下,揭示出所有的方块。在这篇文章中,我们将介绍如何使用C++编写一个简单的扫雷游戏。首先,我们需要定义一个二维数组来表示扫雷游戏的地图。数组中的每个元素可以是一个结构体,用于存储方块的状态,例如是否揭示、是否有雷等信息。另外,我们还需要定义

如何使用C#编写二分查找算法如何使用C#编写二分查找算法Sep 19, 2023 pm 12:42 PM

如何使用C#编写二分查找算法二分查找算法是一种高效的查找算法,它在有序数组中查找特定元素的位置,时间复杂度为O(logN)。在C#中,我们可以通过以下几个步骤来编写二分查找算法。步骤一:准备数据首先,我们需要准备一个已经排好序的数组作为查找的目标数据。假设我们要在数组中查找特定元素的位置。int[]data={1,3,5,7,9,11,13

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor