详细内容参考 官方文档
安装拓展
#### composer安装
composer require elasticsearch/elasticsearch
基础使用
- 实例化一个客户端 ```php require ‘vendor/autoload.php’;
use Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()->build();
2. 索引一个文档
```php
$params = [
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id',
'body' => ['testField' => 'abc']
];
$response = $client->index($params);
print_r($response);
- 获取一个文档
$params = [
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id'
];
$response = $client->get($params);
print_r($response);
- 搜索一个文档
```php
$params = [
‘index’ => ‘my_index’,
‘type’ => ‘my_type’,
‘body’ => [
] ];'query' => [ 'match' => [ 'testField' => 'abc' ] ]
$response = $client->search($params); print_r($response);
5. 删除一个文档
```php
$params = [
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id'
];
$response = $client->delete($params);
print_r($response);
删除索引
$deleteParams = [ 'index' => 'my_index' ]; $response = $client->indices()->delete($deleteParams); print_r($response);
创建索引 ```php $params = [ ‘index’ => ‘my_index’, ‘body’ => [
'settings' => [ 'number_of_shards' => 2, 'number_of_replicas' => 0 ]
] ];
$response = $client->indices()->create($params); print_r($response); ```