Current Location: Home> Latest Articles> How to Build an Efficient E-Commerce Product Search Engine Using PHP and Coreseek

How to Build an Efficient E-Commerce Product Search Engine Using PHP and Coreseek

M66 2025-06-19

1. Introduction to Coreseek

Coreseek is an open-source full-text search engine based on Sphinx, known for its excellent performance and powerful search capabilities. It supports features such as Chinese word segmentation, ranking by weight, and highlighting. Coreseek is written in C++, making it highly efficient, and PHP, being a popular scripting language, integrates seamlessly with Coreseek.

2. Installing Coreseek

First, you need to install Coreseek on your server. You can refer to the official documentation for detailed installation steps. After installation, configure Coreseek, primarily by editing the sphinx.conf configuration file. Below is a simple configuration example:

source product {
    type = mysql
    sql_host = localhost
    sql_user = root
    sql_pass = password
    sql_db = ecommerce
    sql_query = SELECT id, name, description, price FROM products
}

index product_index {
    source = product
    path = /path/to/index
    min_word_len = 2
    min_prefix_len = 2
    enable_star = 1
    charset_type = utf-8
}

searchd {
    listen = 9312
    listen = 9306:mysql41
    log = /path/to/log/searchd.log
    query_log = /path/to/log/query.log
    pid_file = /path/to/log/searchd.pid
}

In the example above, we define a data source named “product,” assuming that product data is stored in the “ecommerce” database and retrieved from the “products” table. We also define an index called “product_index” and specify the storage path for the index file. Finally, we set up a Coreseek search process named searchd.

3. Using Coreseek for Search in PHP

Next, let's use the SphinxClient class in PHP to connect to and use Coreseek. Below is a simple search example:

<?php
require_once('sphinxapi.php');

$cl = new SphinxClient();
$cl->setServer('localhost', 9312);
$cl->setMatchMode(SPH_MATCH_EXTENDED);
$cl->setLimits(0, 20);  // Set the number of search results

$keyword = $_GET['keyword'];  // Get the search keyword input by the user

$res = $cl->Query($keyword, 'product_index');

if ($res && $res['total_found'] > 0) {
    foreach ($res['matches'] as $match) {
        $productId = $match['id'];
        // Fetch product details from the database based on productId and display
    }
} else {
    echo 'No related products found';
}
?>

In the above code example, we first include the Coreseek PHP extension file "sphinxapi.php." Then, we create a SphinxClient object, set the server address and port, and configure the match mode to SPH_MATCH_EXTENDED (which supports advanced query syntax). Finally, we call the Query method to initiate a search and display the results accordingly.

4. Conclusion

Through the example provided in this article, we have demonstrated how to build an e-commerce product search engine using PHP and Coreseek. While this article only covers basic functionality, developers can add more advanced features based on specific requirements. By following this approach, you can create an efficient, accurate, and user-friendly product search engine for your e-commerce platform.