Current Location: Home> Latest Articles> How to Implement Near Real-Time Search with PHP and Elasticsearch

How to Implement Near Real-Time Search with PHP and Elasticsearch

M66 2025-06-21

How to Implement Near Real-Time Search with PHP and Elasticsearch

In modern web applications, search functionality is essential, especially when dealing with large datasets and high-concurrency queries. Traditional database query methods often cannot meet these demands, while Elasticsearch, a powerful distributed search engine, can efficiently handle large-scale data and support near real-time search.

Installing Elasticsearch and PHP Client

First, you need to install Elasticsearch on your server. You can download the latest version from the official website and follow the related documentation for installation and configuration.

Next, use Composer to install the PHP Elasticsearch client library. Run the following command to install it:

composer require elasticsearch/elasticsearch

Connecting to Elasticsearch

In your PHP code, you need to create an instance of the Elasticsearch client and connect it to the Elasticsearch server. Here's an example:

require 'vendor/autoload.php';
$client = ElasticsearchClientBuilder::create()->build();

Creating Index and Mappings

Before performing searches, you need to create an index and define mappings. In Elasticsearch, an index is similar to a table in a traditional database, while mappings define the structure of the data. Here’s an example of how to create an index and define mappings:

$params = [
    'index' => 'my_index',
    'body'  => [
        'mappings' => [
            'properties' => [
                'title' => ['type' => 'text'],
                'content' => ['type' => 'text']
            ]
        ]
    ]
];
$response = $client->indices()->create($params);

The code above creates an index named “my_index” and defines mappings for the “title” and “content” fields as text type. You can customize the fields and data types as needed.

Adding Documents

Next, you need to add data to the index. Here’s an example of how to add a document with “title” and “content” fields to the index:

$params = [
    'index' => 'my_index',
    'body'  => [
        'title'   => 'example title',
        'content' => 'example content'
    ]
];
$response = $client->index($params);

This code adds a document containing the “title” and “content” fields to the “my_index” index. You can add more fields as needed.

Creating a Search Query

Once the data is added, you can create a search query to retrieve the data. Here’s an example of a simple search query:

$params = [
    'index' => 'my_index',
    'body'  => [
        'query' => [
            'match' => [
                'title' => 'example'
            ]
        ]
    ]
];
$response = $client->search($params);
<p>foreach ($response['hits']['hits'] as $hit) {<br>
echo $hit['_source']['title'] . "\n";<br>
}<br>

This code creates a match query for the “title” field and outputs the title of the matched documents.

Conclusion

By combining PHP with Elasticsearch, you can quickly implement an efficient near real-time search functionality. This article covered the complete process, from installing and configuring Elasticsearch, to connecting to Elasticsearch, creating an index, adding documents, and performing search queries. I hope this article helps you better understand how to integrate Elasticsearch into your web applications and deliver a smooth search experience.