<?php
namespace app\lib;
use Elasticsearch\ClientBuilder;
class Es
{
private $url = "127.0.0.1";
private $port = "9200";
private $client = null;
public function __construct()
{
$params = [
$this->url.":".$this->port
];
$this->client = ClientBuilder::create()->setHosts($params)->build();
}
//创建索引
public function createIndex(string $name = "",int $number_of_shards = 5,int $number_of_replicas = 0){
$params["index"] = $name;
$params["body"]["settings"]["number_of_shards"] = $number_of_shards;//是数据分片数,默认为5,有时候设置为3
$params["body"]["settings"]["number_of_replicas"] = $number_of_replicas; //是数据备份数,如果只有一台机器,设置为0
$result = $this->client->indices()->create($params);
return $result; //返回值 Array ( [acknowledged] => 1 [shards_acknowledged] => 1 [index] => person )
}
//删除索引
public function deleteIndex(string $index = ""){
$params["index"] = $index;
$result = $this->client->indices()->delete($params);
return $result; //返回值 Array ( [acknowledged] => 1 )
}
//创建文档
public function addDocument(string $index = "" ,string $type = "" ,string $id = "" ,array $body = []){
$params = [
"index" => $index,
"type"=> $type,
"id" => $id ,
"body" => $body
];
$result = $this->client->index($params);
return $result;
/*Array
(
[_index] => person
[_type] => person_type
[_id] => 1
[_version] => 1
[result] => created
[_shards] => Array
(
[total] => 1
[successful] => 1
[failed] => 0
)
[_seq_no] => 0
[_primary_term] => 1
)*/
}
//更新文档
public function editDocument(string $index = "" ,string $type = "" ,string $id = "" ,array $doc = []){
$params = [
"index" => $index,
"type"=> $type,
"id" => $id ,
"body" => $doc
];
$result = $this->client->update($params);
return $result;
}
//删除文档
public function delDocument(string $index = "" ,string $type = "" ,string $id = "" ){
$params = [
"index" => $index,
"type"=> $type,
"id" => $id ,
];
$result = $this->client->delete($params);
return $result;
}
//获取文档
public function getDocument(string $index = "" ,string $type = "" ,string $id = ""){
$params = [
"index" => $index,
"type"=> $type,
"id" => $id ,
];
$result = $this->client->get($params);
return $result;
}
//查询文档
public function searchDocument(string $index = "" ,string $type = "" ,int $from = 0 ,int $size = 10,array $order = ['order' => 'id']){
$params = [
"index" => $index,
"type"=> $type,
"from" => $from ,
"size" => $size ,
//"sort" => ['_score' => $order]
];
//相当于sql语句: select * from hp_product where prodcut_name like '茶' limit 0,100 order by id desc;
$params['body']['query']['match']['title'] = '张';
$result = $this->client->search($params);
print_r($result);
//相当于sql语句: select * from hp_product where product_name like '茶' and product_id = 20 limit 200,10;
// $params['body']['query']['bool']['must'] = array(
// array('match' => array('product_name' => '茶')),
// array('match' => array('product_id' => 20))
// );
// $params['size'] = 10;
// $params['from'] = 200;
//
//
// 当于sql语句:select * from hp_product where product_name like '茶' or product_id = 20 limit 200,10;
// $params['body']['query']['bool']['should'] = array(
// array('match' => array('product_name' => '茶')),
// array('match' => array('product_id' => 20))
// );
//$params['size'] = 10;
//$params['from'] = 200;
//
//
// 当于sql语句: select * from hp_product where product_name like '茶' and product_id != 20 limit 200,10;
// $params['body']['query']['bool']['must_not'] = array(
// array('match' => array('product_name' => '茶')),
// array('match' => array('product_id' => 20))
// );
//$params['size'] = 10;
//$params['from'] = 200;
//
//
//当于sql语句:select * from hp_product where id>=20 and id<30 limit 200,10;
// $params['body']['query']['range'] = array(
// 'id' => array('gte' => 20,'lt' => 30);
// );
//$params['size'] = 10;
//$params['from'] = 200;
}
}