Current Location: Home> Latest Articles> Guide to Building Accurate Search Functionality with PHP and Manticore Search

Guide to Building Accurate Search Functionality with PHP and Manticore Search

M66 2025-08-04

Introduction

In modern web applications, search functionality is a crucial component for improving user experience. To ensure that search results are both fast and accurate, adopting a high-performance full-text search engine is essential. Manticore Search is a powerful search engine that can be easily integrated into applications using its PHP client.

This article will walk you through how to build a precise search feature using PHP and Manticore Search, supported by code examples for better understanding.

Installing Manticore Search

Before using Manticore Search, you need to install it first. You can install it by running the following command in the terminal:

sudo apt-get install manticoresearch

Creating Index and Adding Documents

Prior to searching, you need to create an index and add documents. The index is responsible for storing and retrieving data, while documents contain the actual data. The example below demonstrates how to create an index named books with documents containing book titles, authors, and content:

index books
{
    type = plain
    path = /var/lib/manticore/data/books
    source = src1
    ...
}

Then, use the PHP code below to add documents to the index:

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

$client = new ManticoreClient();

$doc = array(
    'id' => 1,
    'title' => 'PHP for Beginners',
    'author' => 'John Doe',
    'content' => 'This book is a guide for beginners'
);

$client->addDocument('books', $doc);
?>

Performing Search Queries

Once the index is ready, you can perform search queries to retrieve matching documents. The following example demonstrates a keyword-based search operation:

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

$client = new ManticoreClient();

$query = 'beginners guide';

$results = $client->search('books', $query);

foreach ($results['matches'] as $match) {
    echo 'Title: ' . $match['attrs']['title'] . '<br>';
    echo 'Author: ' . $match['attrs']['author'] . '<br>';
    echo 'Content: ' . $match['attrs']['content'] . '<br>';
    echo '<hr>';
}
?>

Optimizing Search Queries

To improve search accuracy and performance, you can utilize advanced features of Manticore Search. The code below shows how to specify search fields and limit the number of results returned:

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

$client = new ManticoreClient();

$query = 'beginners guide';

$params = array(
    'fields' => array('title', 'content'),
    'limit' => 10
);

$results = $client->search('books', $query, $params);

foreach ($results['matches'] as $match) {
    echo 'Title: ' . $match['attrs']['title'] . '<br>';
    echo 'Content: ' . $match['attrs']['content'] . '<br>';
    echo '<hr>';
}
?>

By specifying the fields and limiting the number of returned results, you can better control the search outcomes.

Conclusion

Combining PHP with Manticore Search to build a precise search function greatly enhances both the efficiency and accuracy of search results. This article provides a complete workflow from installation, index setup, to search querying and optimization with practical code examples to help developers get started quickly. For real-world projects, exploring additional advanced features can further improve the search experience.