Search Engine¶
A search engine is a system that indexes and retrieves data based on queries. In applications, a search engine typically refers to a dedicated search service or library that provides full-text search capabilities beyond what a relational database can offer natively.
Popular search engines used with PHP projects include Elasticsearch, OpenSearch, Meilisearch, Typesense, and Apache Solr. They offer features such as full-text indexing, relevance ranking, faceted search, autocomplete, and typo tolerance.
Search engines usually operate as external services communicating via HTTP APIs. PHP libraries are used as clients to index documents and execute queries against the engine.
<?php
// Indexing a document with Elasticsearch PHP client
use Elastic\Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()
->setHosts(['localhost:9200'])
->build();
$client->index([
'index' => 'products',
'id' => 42,
'body' => [
'title' => 'Widget Pro',
'description' => 'A high-quality widget for professionals.',
'price' => 29.99,
],
]);
// Searching documents
$response = $client->search([
'index' => 'products',
'body' => [
'query' => [
'match' => ['description' => 'widget'],
],
],
]);
foreach ($response['hits']['hits'] as $hit) {
echo $hit['_source']['title'] . PHP_EOL;
}
?>
See also Elasticsearch PHP client, Meilisearch PHP SDK and Typesense PHP client.
Related : Database, Index For SQL, Cache, REST API, Application Programming Interface (API), Storage Systems, robots.txt
Related packages : elasticsearch/elasticsearch, meilisearch/meilisearch-php, typesense/typesense-php