With the rapid growth of information, users increasingly demand accuracy in search results. Traditional search engines often fall short in meeting multidimensional and precise search needs, making it essential to build a flexible and efficient search filtering system.
Manticore Search is an open-source, high-performance full-text search engine that supports rapid scalability and complex queries. You can download and install Manticore Search from official sources to prepare for development.
An index is the foundation of full-text search. The following example shows how to create an index with title, content, and category fields via PHP using the Manticore Search extension:
<?php
require 'vendor/autoload.php';
use ManticoresearchClient;
use ManticoresearchIndex;
use ManticoresearchExceptionsRuntimeException;
use ManticoresearchQueryBoolQuery;
use ManticoresearchQueryQuery;
try {
$client = new Client(['host' => 'localhost', 'port' => 9308]);
$index = new Index($client);
$index->create([
'index' => 'myindex',
'type' => 'mytype',
'fields' => [
'title' => ['type' => 'text'],
'content' => ['type' => 'text'],
'category' => ['type' => 'text']
]
]);
echo "Index created successfully.";
} catch (RuntimeException $e) {
echo $e->getMessage();
}
?>
This code creates an index named "myindex" with necessary text fields, which can be adjusted based on your needs.
Once the index is ready, use the following example to perform keyword full-text search across title, content, and category fields:
<?php
require 'vendor/autoload.php';
use ManticoresearchClient;
use ManticoresearchQueryBoolQuery;
use ManticoresearchQueryQuery;
$client = new Client(['host' => 'localhost', 'port' => 9308]);
$boolQuery = new BoolQuery();
$query = new Query($client);
$boolQuery->addShould($query->match('title', 'keyword'));
$boolQuery->addShould($query->match('content', 'keyword'));
$boolQuery->addShould($query->match('category', 'keyword'));
$query->bool($boolQuery);
$result = $query->search('myindex', 'mytype');
print_r($result);
?>
This executes a search for the keyword "keyword" across multiple fields, meeting diverse query needs.
Adding filter conditions refines search results further. The example below shows how to filter by a specific category on top of full-text search:
<?php
require 'vendor/autoload.php';
use ManticoresearchClient;
use ManticoresearchQueryBoolQuery;
use ManticoresearchQueryQuery;
$client = new Client(['host' => 'localhost', 'port' => 9308]);
$boolQuery = new BoolQuery();
$query = new Query($client);
$boolQuery->addMust($query->match('title', 'keyword'));
$boolQuery->addFilter($query->term('category', 'news'));
$query->bool($boolQuery);
$result = $query->search('myindex', 'mytype');
print_r($result);
?>
By adding filters, the search scope is narrowed, improving the relevance and accuracy of the results.
This article introduced how to use PHP combined with Manticore Search to build an intelligent search filtering feature. It covered key steps including environment setup, index creation, full-text search, and filtering. Hopefully, it provides valuable insights for your search engine development and helps you create smarter search experiences that better meet user needs.