Current Location: Home> Latest Articles> Enhancing Search Relevance with PHP and Manticore Search Integration

Enhancing Search Relevance with PHP and Manticore Search Integration

M66 2025-07-18

Integrating PHP with Manticore Search

In modern web applications, search functionality plays a crucial role. With the rapid growth of data, users demand more accurate and relevant search results. To meet these expectations, developers often integrate a high-performance full-text search engine. Manticore Search is an open-source solution designed for fast and accurate search. This article demonstrates how to integrate Manticore Search with PHP to create a robust search experience.

What is Manticore Search

Manticore Search is a fork of the Sphinx project, offering enhanced features. It supports full-text search, fuzzy matching, custom scoring, JSON storage, and more. Ideal for e-commerce, content platforms, and enterprise search, it comes with rich APIs that make integration with PHP straightforward and efficient.

Installing Manticore Search

You can download the latest version of Manticore Search from the official website and follow the installation guide. After installation, ensure the service is running properly on your server.

Creating an Index in PHP

The first step in building a search feature is creating an index, which stores structured data. Here’s an example of how to create an index using PHP:

<?php
$index = new ManticoreSearchIndex($client, 'products');
$index->create([
    'product_id' => ['type' => 'integer', 'indexed' => true, 'docvalues' => true],
    'name' => ['type' => 'text', 'indexed' => true, 'tokenized' => true],
    'price' => ['type' => 'float'],
]);
?>

This code creates a 'products' index with fields for product ID, name, and price. You can customize it further to suit your data model.

Adding Documents to the Index

Documents are the searchable data entries in the index. The following PHP code shows how to add a sample product document:

<?php
$document = new ManticoreSearchDocument($index);
$document->setId(1);
$document->setFields([
    'product_id' => 1,
    'name' => 'iPhone 11',
    'price' => 999.99,
]);
$index->addDocuments([$document]);
?>

You can add as many documents as needed, making sure each one follows the field structure defined in the index.

Executing a Search

With documents added, you can now perform search queries. Here's a simple example of how to search for products using a keyword:

<?php
$search = new ManticoreSearchSearch($client, ['index' => 'products']);
$result = $search->search('iPhone');
foreach ($result['hits'] as $hit) {
    echo $hit['product_id'] . ': ' . $hit['name'] . ' - ' . $hit['price'] . "\n";
}
?>

This code returns all documents that contain the keyword “iPhone” and displays the results. You can further customize your search by applying filters, sorting, or pagination.

Improving Search Result Relevance

To make your search results more relevant, Manticore Search allows you to adjust field weights and use advanced ranking expressions. Here's an example of how to configure relevance scoring:

<?php
$index->update([
    'base' => 'products',
    'expr' => 'rank_expr',
    'ranker' => 'expr',
]);
?>

With this configuration, you can prioritize certain fields over others in the ranking formula. Additionally, Manticore supports Boolean logic, phrase search, similarity queries, and more—allowing you to create fine-tuned and intelligent search experiences.

Conclusion

By integrating PHP with Manticore Search, developers can build powerful, scalable search features that deliver highly relevant results. This guide has covered the basics of installation, index creation, document management, search execution, and relevance optimization. Tailor these techniques to your specific project needs to significantly enhance your application’s search performance.