Current Location: Home> Latest Articles> Efficient Product Search Implementation Using PHP and Coreseek

Efficient Product Search Implementation Using PHP and Coreseek

M66 2025-07-10

The Importance of Product Search in E-commerce

In today's e-commerce environment, product search is a critical feature that allows users to quickly find their desired products. By using keyword queries, users can efficiently browse and filter products, greatly improving their shopping experience. Therefore, building a fast and accurate search system is essential.

Introduction to Coreseek and Its Advantages

Coreseek is a full-text search engine based on Sphinx, optimized for Chinese language environments. It supports Chinese word segmentation and offers fast response times and high accuracy. Coreseek imports data from MySQL databases to build indexes and provides simple query interfaces, making it easy to integrate with PHP applications.

Installing and Configuring Coreseek

First, download the Coreseek package and extract it to your chosen directory. Navigate to the configuration folder and rename sphinx.conf.dist to sphinx.conf. Open this file and adjust the index paths and searchable fields according to your needs. Save the changes, then in the terminal, navigate to the Coreseek installation directory and run:

./bin/searchd

This command starts the search service. Ensure the service runs without errors.

Preparing Product Data and Importing Indexes

Make sure MySQL is installed. Create a database and a table to store product data, for example:

CREATE DATABASE IF NOT EXISTS products;

CREATE TABLE products.product (
  id INT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(100) NOT NULL,
  description TEXT,
  price DECIMAL(10, 2),
  category_id INT
);

Insert sample product data:

INSERT INTO products.product (name, description, price, category_id)
VALUES
  ('Product1', 'Description for product 1', 19.99, 1),
  ('Product2', 'Description for product 2', 29.99, 2),
  ('Product3', 'Description for product 3', 39.99, 1),
  ('Product4', 'Description for product 4', 49.99, 2);

After data preparation, import the data into Coreseek’s index with:

./bin/indexer --all --config /path/to/sphinx.conf

This will index the product data from the database.

Implementing Product Search with PHP

Create a file named search.php and add the following core search code:

<?php
require('path/to/sphinxapi.php');

// Get the search keyword
$keyword = $_GET['q'];

// Instantiate Sphinx client
$sphinx = new SphinxClient();

// Set Sphinx server connection
$sphinx->setServer('localhost', 9312);

// Perform the search
$result = $sphinx->query($keyword, 'product_index');

// Handle search results
if ($result) {
    if ($result['total'] > 0) {
        echo "Found {$result['total']} results:<br>";
        foreach ($result['matches'] as $match) {
            $product = getProductById($match['id']); // Fetch product info by ID
            echo "Product Name: {$product['name']}<br>";
            echo "Description: {$product['description']}<br>";
            echo "Price: {$product['price']}<br><br>";
        }
    } else {
        echo "No results found.";
    }
} else {
    echo $sphinx->GetLastError();
}

// Fetch product details from the database by ID
function getProductById($id) {
    $conn = new mysqli('localhost', 'username', 'password', 'products');
    $sql = "SELECT * FROM product WHERE id = $id";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        return $result->fetch_assoc();
    } else {
        return null;
    }
}
?>

This code demonstrates how to query Coreseek’s index and display detailed product information from the database. Adjust database connection parameters as needed.

Testing the Search Functionality

Open your browser and visit:
http://yourdomain.com/search.php?q=keyword
Replace yourdomain.com with your server's address and keyword with your search term. If configured properly and indexes are up to date, matching results will be displayed.

Conclusion

By combining PHP with Coreseek, you can build a responsive and accurate product search system. Effective database design and index management ensure fast and precise search results, improving user experience on e-commerce platforms. This guide aims to help you implement an efficient product search feature quickly and reliably.