Current Location: Home> Latest Articles> PHP Development Guide: Implementing Personalized Search with Manticore Search

PHP Development Guide: Implementing Personalized Search with Manticore Search

M66 2025-06-22

Integrating Manticore Search with PHP: Build a Smart User Preference Search

In modern web development, delivering a personalized search experience is a key factor in improving user engagement and retention. By integrating a high-performance search engine, developers can filter and return content based on user behavior and preferences. This guide demonstrates how to use PHP with Manticore Search to build a flexible search system based on user-defined preferences.

Step 1: Installing and Configuring Manticore Search

First, ensure you’ve downloaded and installed the latest version of Manticore Search. After installation, open the configuration file and enable support for real-time indexing. Set appropriate paths for logs, PID files, and memory limits. Here is a basic configuration example:

searchd
{
    listen = 127.0.0.1:9306
    binlog_path = /var/lib/manticore
    pid_file = /var/run/manticore/searchd.pid
    log = /var/log/manticore/searchd.log
    query_log = /var/log/manticore/query.log
    search_logs = 1
    rt_mem_limit = 512M
}

index my_index
{
    type = rt
    rt_attr_string = name
    rt_attr_uint = age
}

Once you’ve configured the file, restart Manticore Search to apply the changes.

Step 2: Indexing Data Using PHP

Before implementing the search feature, you need to index your data. The following example shows how to use the SphinxQL PHP driver to connect to Manticore Search and insert records:

<?php
require_once('vendor/autoload.php');

use Foolz\SphinxQL\Drivers\Connection;
use Foolz\SphinxQL\SphinxQL;

$connection = new Connection();
$connection->setParams(['host' => '127.0.0.1', 'port' => 9306]);

$index = 'my_index';
$engine = new SphinxQL($connection);
$engine->setConnection($connection);

$engine->query("TRUNCATE RTINDEX $index")->execute();
$engine->query("REPLACE INTO $index (name, age) VALUES ('Alice', 25), ('Bob', 30), ('Charlie', 35)")->execute();

This code clears any existing index data and inserts some sample users for testing.

Step 3: Building a Preference-Based Search Function

Assuming we want to search users based on an age range defined by user preferences, the following function dynamically constructs and executes a SphinxQL query:

<?php
function buildUserPreferenceQuery($preferences) {
    global $connection;

    $index = 'my_index';
    $engine = new SphinxQL($connection);
    $engine->setConnection($connection);

    $query = $engine->query("SELECT * FROM $index");

    foreach ($preferences as $key => $value) {
        if ($key == 'min_age') {
            $query->where('age', '>=', $value);
        } elseif ($key == 'max_age') {
            $query->where('age', '<=', $value);
        }
    }

    return $query->execute();
}

Now, we can use this function by passing a sample preference array and printing the matching search results:

<?php
$preferences = [
    'min_age' => 25,
    'max_age' => 35
];

$result = buildUserPreferenceQuery($preferences);

foreach ($result as $row) {
    echo "Name: " . $row['name'] . ", Age: " . $row['age'] . "\n";
}

Conclusion

Through this step-by-step guide, we’ve implemented a basic personalized search feature using PHP and Manticore Search. By leveraging real-time indexing and dynamic query building, this setup allows developers to create adaptive search systems tailored to user needs. You can expand this functionality further by incorporating full-text fields, sorting, pagination, or even complex filters for more advanced use cases.