Using Elasticsearch 8.2 with PHP 8: Installation, Indexing, and CRUD Operations
This tutorial walks through installing Elasticsearch 8.2, configuring the PHP client via Composer, creating a MySQL articles table, indexing its rows into Elasticsearch, and performing basic CRUD operations such as retrieving, deleting, and searching documents using PHP code.
Elasticsearch is a Lucene‑based distributed search server that provides a RESTful API and is written in Java. It is widely used for real‑time, reliable, and fast enterprise search and is designed for cloud environments.
Environment
PHP 8.0, Elasticsearch 8.2, and the official elasticsearch‑php 8.2 client library.
Install Elasticsearch
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.2.3.tar.gz
tar zxvf elasticsearch-8.2.3.tar.gz
useradd elasticsearch
password elasticsearch
chown elasticsearch:elasticsearch elasticsearch-8.2.3
cd elasticsearch-8.2.3
./bin/elasticsearch // startInstall PHP extension
Use Composer to add the client library:
{
"require": {
// ...
"elasticsearch/elasticsearch": "~8.2.3"
// ...
}
}Run composer update to install.
Test example – MySQL table and data
# Create articles table
create table articles(
id int not null primary key auto_increment,
title varchar(200) not null comment '标题',
content text comment '内容'
);
# Insert sample rows
insert into articles(title, content) values
('Laravel 测试1','Laravel 测试文章内容1'),
('Laravel 测试2','Laravel 测试文章内容2'),
('Laravel 测试3','Laravel 测试文章内容3');Read data from MySQL
try {
$db = new PDO('mysql:host=127.0.0.1;dbname=test','root','');
$sql = 'select * from articles';
$query = $db->prepare($sql);
$query->execute();
$lists = $query->fetchAll();
print_r($lists);
} catch (Exception $e) {
echo $e->getMessage();
}Instantiate Elasticsearch client
require './vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()->build();In Elasticsearch, an index is analogous to a MySQL table and a document to a row.
Index documents
foreach ($lists as $row) {
$params = [
'body' => [
'id' => $row['id'],
'title' => $row['title'],
'content' => $row['content']
],
'id' => 'article_' . $row['id'],
'index' => 'articles_index',
'type' => 'articles_type'
];
$client->index($params);
}Get a document from the index
$params = [
'index' => 'articles_index',
'type' => 'articles_type',
'id' => 'articles_1'
];
$res = $client->get($params);
print_r($res);Delete a document
$params = [
'index' => 'articles_index',
'type' => 'articles_type',
'id' => 'articles_1'
];
$res = $client->delete($params);
print_r($res);Delete the entire index
$params = [
'index' => 'articles_index'
];
$res = $client->indices()->delete($params);
print_r($res);Create an index with custom settings
$params['index'] = 'articles_index';
$params['body']['settings']['number_of_shards'] = 2;
$params['body']['settings']['number_of_replicas'] = 0;
$client->indices()->create($params);Search documents
$params = [
'index' => 'articles_index',
'type' => 'articles_type',
];
$params['body']['query']['match']['content'] = 'Laravel';
$res = $client->search($params);
print_r($res);The article concludes with a reminder to like and share if the content was helpful.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
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.
