Using Elasticsearch and Sphinx as PHP Search Engines: Installation, Configuration, and Code Examples
This article explains why MySQL LIKE queries struggle with large datasets, introduces Elasticsearch and Sphinx as powerful PHP search engine alternatives, and provides step‑by‑step PHP code examples for setting up indices, adding documents, and performing full‑text searches.
In web development, search functionality is crucial, and traditional MySQL LIKE queries often become slow and inefficient as data volume grows.
To overcome these limitations, two robust PHP search engine extensions are recommended: Elasticsearch and Sphinx.
Elasticsearch is a distributed search and analytics engine that offers real‑time full‑text search using inverted indexes, supports various query types and filters, and is backed by an active open‑source community.
PHP usage example for Elasticsearch:
<code>// 引入自动加载文件
require 'vendor/autoload.php';
// 创建 Elasticsearch 客户端
$client = Elasticsearch\ClientBuilder::create()
->setHosts(['localhost:9200'])
->build();
// 创建一个索引
$params = [
'index' => 'my_index',
'body' => [
'settings' => [
'number_of_shards' => 1,
'number_of_replicas' => 0
]
]
];
$response = $client->indices()->create($params);
// 添加一个文档
$params = [
'index' => 'my_index',
'type' => 'my_type',
'id' => '1',
'body' => [
'title' => 'My Document',
'content' => 'This is a test document'
]
];
$response = $client->index($params);
// 搜索文档
$params = [
'index' => 'my_index',
'type' => 'my_type',
'body' => [
'query' => [
'match' => [
'title' => 'My Document'
]
]
]
];
$response = $client->search($params);
// 输出搜索结果
print_r($response['hits']['hits']);
</code>A recommended course related to Elasticsearch is "Massive‑Scale Data Concurrency Solutions (Theory + Practice)".
Sphinx is another reliable PHP search engine that supports full‑text and distributed search, known for its efficiency and scalability, and provides rich APIs for PHP integration.
The Sphinx workflow consists of four components: Database (data source), Indexer (creates full‑text indexes), Searchd (handles queries), and the APP (client that sends queries and displays results).
PHP usage example for Sphinx:
<code>$host = 'localhost';
$port = 9312;
// 创建一个 Sphinx 客户端对象
$client = new SphinxClient();
// 设置服务器主机和端口
$client->setServer($host, $port);
$query = 'keyword'; // 要搜索的关键字
// 设置查询选项
$client->setMatchMode(SPH_MATCH_EXTENDED2); // 匹配模式
$client->setLimits(0, 20); // 设置返回结果的数量
// 执行查询
$result = $client->query($query, 'index_name'); // index_name 是你要查询的索引名称
// 处理查询结果
if ($result === false) {
echo '查询失败:' . $client->getLastError();
} else {
foreach ($result['matches'] as $id => $match) {
echo '文档ID:' . $id . ',评分:' . $match['weight'];
}
}
</code>The article concludes by encouraging readers to explore these two search engine extensions and find installation guides as needed.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.