php-elasticsearch客户端基本使用
php-elasticsearch客户端基本使用
标签(空格分隔): php,elasticsearch
官方文档:https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/getting-started-php.html#_searching_documents
官方中文文档(已过时):https://www.elastic.co/guide/cn/elasticsearch/php/current/_configuration.html
开始
本页将指导您完成 PHP 客户端的安装过程,向您展示如何实例化客户端,以及如何使用它执行基本的 Elasticsearch 操作。
安装
composer require elasticsearch/elasticsearch
实例化客户端
require 'vendor/autoload.php';
$client = Elastic\Elasticsearch\ClientBuilder::create()->build();
创建索引
use Elasticsearch\ClientBuilder;
include "../vendor/autoload.php";
$hosts = [
"http://192.168.33.10:9200"
];
$client = ClientBuilder::create()->setHosts($hosts)->build();
$params = [
'index' => 'my_index',
];
$response = $client->indices()->create($params);
// response print
array(3) {
["acknowledged"]=>
bool(true)
["shards_acknowledged"]=>
bool(true)
["index"]=>
string(8) "my_index"
}
插入一条数据
use Elasticsearch\ClientBuilder;
include "../vendor/autoload.php";
$hosts = [
"http://192.168.33.10:9200"
];
$client = ClientBuilder::create()->setHosts($hosts)->build();
$response = $client->index([
'index' => 'my_index',
'body' => [
'name' => 'Taylor',
'age' => 32,
'address' => "1101 S Main St APT 203 Milpitas CA 95035",
'sex' => '女',
'hobby' => ["唱歌", "跳舞", "谈恋爱", "走秀"]
]
]);
// response print
array(8) {
["_index"]=>
string(8) "my_index"
["_type"]=>
string(4) "_doc"
["_id"]=>
string(20) "uH5JtIkBC78u-UjcI9s-"
["_version"]=>
int(1)
["result"]=>
string(7) "created"
["_shards"]=>
array(3) {
["total"]=>
int(2)
["successful"]=>
int(1)
["failed"]=>
int(0)
}
["_seq_no"]=>
int(0)
["_primary_term"]=>
int(1)
}
插入多条
use Elasticsearch\ClientBuilder;
include "../vendor/autoload.php";
$hosts = [
"http://192.168.33.10:9200"
];
$client = ClientBuilder::create()->setHosts($hosts)->build();
$params = ['body' => []];
for ($i = 1; $i < 6;$i++) {
$params['body'][] = [
'index' => [
'_index' => 'my_index',
'_id' => $i
]
];
$params['body'][] = [
'name' => "Swift".$i,
'age' => rand(10, 35),
'address' => "1101 S Main St APT 203 Milpitas CA 95035",
'sex' => "女",
'hobby' => ["唱歌", "跳舞", "谈恋爱", "走秀"]
];
}
$response = $client->bulk($params);
// response print 新增5条数据
array(3) {
["took"]=>
int(8)
["errors"]=>
bool(false)
["items"]=>
array(5) {
[0]=>
array(1) {
["index"]=>
array(9) {
["_index"]=>
string(8) "my_index"
["_type"]=>
string(4) "_doc"
["_id"]=>
string(1) "1"
["_version"]=>
int(1)
["result"]=>
string(7) "created"
["_shards"]=>
array(3) {
["total"]=>
int(2)
["successful"]=>
int(1)
["failed"]=>
int(0)
}
["_seq_no"]=>
int(1)
["_primary_term"]=>
int(1)
["status"]=>
int(201)
}
}
[1]=>
array(1) {
["index"]=>
array(9) {
["_index"]=>
string(8) "my_index"
["_type"]=>
string(4) "_doc"
["_id"]=>
string(1) "2"
["_version"]=>
int(1)
["result"]=>
string(7) "created"
["_shards"]=>
array(3) {
["total"]=>
int(2)
["successful"]=>
int(1)
["failed"]=>
int(0)
}
["_seq_no"]=>
int(2)
["_primary_term"]=>
int(1)
["status"]=>
int(201)
}
}
[2]=>
array(1) {
["index"]=>
array(9) {
["_index"]=>
string(8) "my_index"
["_type"]=>
string(4) "_doc"
["_id"]=>
string(1) "3"
["_version"]=>
int(1)
["result"]=>
string(7) "created"
["_shards"]=>
array(3) {
["total"]=>
int(2)
["successful"]=>
int(1)
["failed"]=>
int(0)
}
["_seq_no"]=>
int(3)
["_primary_term"]=>
int(1)
["status"]=>
int(201)
}
}
[3]=>
array(1) {
["index"]=>
array(9) {
["_index"]=>
string(8) "my_index"
["_type"]=>
string(4) "_doc"
["_id"]=>
string(1) "4"
["_version"]=>
int(1)
["result"]=>
string(7) "created"
["_shards"]=>
array(3) {
["total"]=>
int(2)
["successful"]=>
int(1)
["failed"]=>
int(0)
}
["_seq_no"]=>
int(4)
["_primary_term"]=>
int(1)
["status"]=>
int(201)
}
}
[4]=>
array(1) {
["index"]=>
array(9) {
["_index"]=>
string(8) "my_index"
["_type"]=>
string(4) "_doc"
["_id"]=>
string(1) "5"
["_version"]=>
int(1)
["result"]=>
string(7) "created"
["_shards"]=>
array(3) {
["total"]=>
int(2)
["successful"]=>
int(1)
["failed"]=>
int(0)
}
["_seq_no"]=>
int(5)
["_primary_term"]=>
int(1)
["status"]=>
int(201)
}
}
}
}
更新数据
use Elasticsearch\ClientBuilder;
include "../vendor/autoload.php";
$hosts = [
"http://192.168.33.10:9200"
];
$client = ClientBuilder::create()->setHosts($hosts)->build();
$params = [
'index' => 'my_index',
'id' => 'uH5JtIkBC78u-UjcI9s-',
'body' => [
'doc' => [
'address' => '美国'
]
]
];
// Update doc at /my_index/_doc/my_id
$response = $client->update($params);
// response print
array(8) {
["_index"]=>
string(8) "my_index"
["_type"]=>
string(4) "_doc"
["_id"]=>
string(20) "uH5JtIkBC78u-UjcI9s-"
["_version"]=>
int(2)
["result"]=>
string(7) "updated"
["_shards"]=>
array(3) {
["total"]=>
int(2)
["successful"]=>
int(1)
["failed"]=>
int(0)
}
["_seq_no"]=>
int(6)
["_primary_term"]=>
int(1)
}
删除数据
$params = [
'index' => 'my_index',
'id' => 'my_id'
];
// Delete doc at /my_index/_doc_/my_id
$response = $client->delete($params);
删除索引
$params = ['index' => 'my_index'];
$response = $client->indices()->delete($params);
查询所有
$params = [
'index' => 'my_index',
];
$response = $client->search($params);
查询一条
$params = [
'index' => 'my_index',
'id' => 'uH5JtIkBC78u-UjcI9s-',
];
$response = $client->get($params);
match查询
$client = ClientBuilder::create()->setHosts($hosts)->build();
$response = $client->search([
'index' => 'my_index',
'body' => [
'query' => [
'match' => [
'name' => 'taylor'
]
]
]
]);
term查询
$response = $client->search([
'index' => 'my_index',
'body' => [
'query' => [
'term' => [
'name' => 'taylor'
]
]
]
]);
range查询
$response = $client->search([
'index' => 'my_index',
'body' => [
'query' => [
'range' => [
'age' => [
'gte' => 10,
'lte' => 20
]
]
]
]
]);
bool组合查询
$params = [
'index' => 'my_index',
'type' => 'my_type',
'body' => [
'query' => [
'bool' => [
'filter' => [
'term' => [ 'address' => 'abc' ]
],
'should' => [
'match' => [ 'name' => 'xyz' ]
]
]
]
]
];
$results = $client->search($params);