Elasticsearch PHP Client Tutorial: Installation, Index Management, CRUD Operations, and ThinkPHP6 Integration
This tutorial walks through installing the Elasticsearch PHP client, creating and managing indices, performing single and bulk document operations, executing queries, updates, deletions, and demonstrates integration with ThinkPHP6 for product search and management.
This guide provides a step‑by‑step tutorial for using the Elasticsearch PHP client, covering installation, index management, CRUD operations, and integration with ThinkPHP6.
1. Installation
https://www.elastic.co/guide/en/elasticsearch/client/php-api/7.x/installation.html2. Install via Composer
composer require elasticsearch/elasticsearch="^7.0"3. Official documentation (authoritative)
https://www.elastic.co/guide/en/elasticsearch/client/php-api/7.x/connceting.html4. Create a client instance
use Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()->setHosts(['192.168.1.128:9200'])->build();5. Index creation example
$index = [
"index" => "lampol",
"body" => [
"mappings" => [
"properties" => [
"name" => ["type" => "keyword"],
"age" => ["type" => "byte"],
"addr" => ["type" => "text", "analyzer" => "ik_max_word"]
]
]
]
];
$response = $client->indices()->create($index);6. Check index existence, get mapping, delete index
# Check if index exists
$response = $client->indices()->exists(['index' => 'lampol']);
# Get mapping
$response = $client->indices()->getMapping(['index' => 'lampol']);
# Delete index
$response = $client->indices()->delete(['index' => 'test1']);7. Insert a single document
$doc = [
"index" => "lampol",
"id" => 1,
"body" => ["name" => "liudehua", "age" => 33, "addr" => "中国广东省深圳市"]
];
$response = $client->index($doc);8. Bulk insert documents
$docs['body'] = [
['index' => ['_index' => 'lampol', '_id' => 2]],
['name' => 'liming', 'age' => 43, 'addr' => '山东省青岛市'],
['index' => ['_index' => 'lampol', '_id' => 3]],
['name' => '郭富城', 'age' => 53, 'addr' => '山东省枣庄市']
];
$response = $client->bulk($docs);9. Document query, update, delete
# Get document with id=3
$id = ['index' => 'lampol', 'id' => 3, '_source' => ['name', 'age' => 33]];
$response = $client->get($id);
# Search by address
$param = [
'index' => 'lampol',
'body' => [
'query' => [
'match' => ['addr' => '深圳']
]
]
];
$response = $client->search($param);
# Update document id=1
$doc = [
'index' => 'lampol',
'id' => 1,
'body' => ['doc' => ['age' => 53]]
];
$response = $client->update($doc);
# Delete document id=3
$id = ['index' => 'lampol', 'id' => 3];
$response = $client->delete($id);10. ThinkPHP6 practical example – product search
use Elasticsearch\ClientBuilder;
class Goods {
public function search(Request $request) {
$client = ClientBuilder::create()->setHosts(['192.168.1.128:9200'])->build();
$keywords = $request->param('keywords', '', 'trim');
$page = $request->param('page');
$size = 20;
if ($page) {
$from = ($page - 1) * 20;
$pre = $page - 1;
$next = $page + 1;
} else {
$pre = 1; $next = 2; $from = 0;
}
$search = [
'index' => 'goods',
'body' => [
'query' => ['match' => ['goods_title' => $keywords]],
'from' => $from,
'size' => $size,
'highlight' => [
'fields' => ['goods_title' => new \stdClass()],
'pre_tags' => ["
"],
'post_tags'=> ['
']
]
]
];
$res = $client->search($search);
return view('list', ['goods' => $res['hits']['hits'], 'keywords' => $keywords, 'pre' => $pre, 'next' => $next]);
}
}11. Highlight configuration reference
https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/php_json_objects.html12. Adding a product to Elasticsearch
public function save(Request $request) {
$client = ClientBuilder::create()->setHosts(['192.168.1.128:9200'])->build();
$goods_title = $request->param('goods_title');
$cat_id = 200;
$id = Db::name('goods')->insertGetId(['cat_id' => $cat_id, 'goods_title' => $goods_title]);
$doc = ['index' => 'goods', 'id' => $id, 'body' => ['id' => $id, 'goods_title' => $goods_title]];
$client->index($doc);
return view('search');
}13. Editing a product in Elasticsearch
public function update(Request $request, $id) {
$goods_title = $request->param('goods_title');
Db::name('goods')->where('id', $id)->update(['goods_title' => $goods_title]);
$client = ClientBuilder::create()->setHosts(['192.168.1.128:9200'])->build();
$doc = [
'index' => 'goods',
'id' => $id,
'body' => ['doc' => ['goods_title' => $goods_title]]
];
$client->update($doc);
return view('search');
}14. Deleting a product from Elasticsearch
public function delete($id) {
Db::name('goods')->where('id', $id)->delete();
$client = ClientBuilder::create()->setHosts(['192.168.1.128:9200'])->build();
$client->delete(['index' => 'goods', 'id' => $id]);
return json(['status' => 'success']);
}For further aggregation queries and advanced features, stay tuned for upcoming articles.
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.