Current Location: Home> Latest Articles> 【Practical Guide to Building Efficient and Accurate Search with PHP and Manticore Search】

【Practical Guide to Building Efficient and Accurate Search with PHP and Manticore Search】

M66 2025-06-07

Introduction

With the explosive growth of information, users’ demands for search experience are continuously increasing. Manticore Search, as a high-performance open-source full-text search engine, combined with PHP’s development capabilities, allows building responsive and accurate search systems. This article walks through the development process step-by-step, from environment setup, index creation to using query expansions for improving search accuracy.

Environment Setup: PHP and Manticore Search

Before starting development, ensure Manticore Search server is installed and the related PHP extension is enabled. Add the following line to the PHP configuration file php.ini to load the extension:

<span class="fun">extension=manticore.so</span>

Restart the PHP service after configuration.

Creating Search Index and Adding Data

Before searching, a basic index must be created. For example, create an index named articles with fields for full-text search such as title and content. Below is a sample code snippet to create the index and add data:

// Create index
$index = 'articles';
$query = 'CREATE TABLE ' . $index . ' (title text, content text)';
$result = $conn->query($query);

// Insert data
$query = 'INSERT INTO ' . $index . ' (title, content) VALUES ("Lorem Ipsum", "Lorem ipsum dolor sit amet, consectetur adipiscing elit.")';
$result = $conn->query($query);

Performing Search Queries

After adding data, search queries can be executed. Manticore supports MATCH syntax for full-text matching. Here’s a basic example:

// Perform search query
$index = 'articles';
$query = 'SELECT * FROM ' . $index . ' WHERE MATCH(\'Lorem\')';
$result = $conn->query($query);

// Output results
if ($result && $result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        echo 'Title: ' . $row['title'] . '<br>';
        echo 'Content: ' . $row['content'] . '<br>';
    }
} else {
    echo 'No relevant results found.';
}

This query returns records containing the keyword "Lorem", quickly locating matching content.

Improving Search Accuracy with Query Expansion

To further enhance search relevance, query expansion can be enabled. For example, a stemmer reduces keywords to their root form, improving match accuracy.

Example of adding a query expansion to the index:

// Add query expansion
$index = 'articles';
$query = 'ALTER TABLE ' . $index . ' ADD QUERY EXPANSION stemmer';
$result = $conn->query($query);

When querying, specify the expansion option:

// Search using stemmer query expansion
$index = 'articles';
$query = 'SELECT * FROM ' . $index . ' WHERE MATCH(\'Lorem\') OPTION query_expansion=stemmer';
$result = $conn->query($query);

This enables automatic stemming during search, broadening and refining keyword matching.

Conclusion

Integrating PHP with Manticore Search offers an effective solution for high-performance search systems. From basic index creation to applying query expansions, developers can flexibly meet diverse search requirements across applications. This approach significantly enhances user experience and practical value in e-commerce, blogging, forums, and other platforms.