1、连接es
use Elasticsearch\ClientBuilder;
private $client;
public function __construct()
{
$this->client = ClientBuilder::create()->setHosts(['localhost:9200'])->build();
}
2、创建索引(相当mysql数据库)
index为索引名字 分片讲解
//创建索引
//现在我们开始添加一个新的索引和一些自定义设置:
public function create_index()
{
$params = [
'index' => 'myindex', #index的名字不能是大写和下划线开头
'body' => [
'settings' => [
'number_of_shards' => 1, //分片
'number_of_replicas' => 0 //副本
],
]
];
$client= $this->client->indices()->create($params);
dump($client);exit;
}
3、设置索引字段
//设置字段
public function add_filed()
{
$params = [
'index' => "myindex",
'body' => [
'_source' => [
'enabled' => true
],
'properties' =>
[
"product_name"=>[
"type"=>'text',
],
'prodcut_id'=>[
"type"=>"integer"
]
]
]
];
$client = $this->client->indices()->putMapping($params);
dump($client);exit;
}
4、获取已设置索引字段
//获取字段
public function get_filed()
{
$post = "myindex";
$params['index'] = $post['index'];
$res = $this->client->indices()->getMapping($params);
$data = isset($res[$post['index']]['mappings']['properties']) ? $res[$post['index']]['mappings']['properties'] : [];
$return=[];
$i=0;
foreach ($data as $key=>$value){
++$i;
$return[]=[
"name"=>$key,
"type"=>$value['type'],
"analyzer"=>isset($value['analyzer'])?$value['analyzer']:""
];
}
dump($return);
}
5、删除索引
//删除索引
public function delete_index()
{
$deleteParams['index'] = 'myindex';
$this->client->indices()->delete($deleteParams);
}
6、修改索引配置
//修改索引配置
public function update_index()
{
$post = "myindex";
$params = [
'index' => $post['index'], #index的名字不能是大写和下划线开头
'body' => [
'settings' => [
'number_of_shards' => 1,
'number_of_replicas' => 0
]
]
];
unset($params['body']['settings']['number_of_shards']);
$this->client->indices()->putSettings($params);
}
7、插入文档
//插入索引数据
public function add_document()
{
$params = array();
$params['body'] = array(
'product_name' => '要插入的商品名称',
'prodcut_id' => 5
);
$params['index'] = 'myindex'; //索引名称
$params['type'] = '_doc'; //类型名称
$params['id'] = '12345678'; //不指定id,系统会自动生成唯一id
$ret = $this->client->index($params);
}
8、修改文档
//更改文档
public function update_document()
{
$updateParams = array();
$updateParams['index'] = 'myindex';
$updateParams['type'] = '_doc';
$updateParams['id'] = 'my_id';
$updateParams['body']['doc']['product_name'] = '新商品名';
$response = $this->client->update($updateParams);
}
9、删除文档
//删除文档
public function delete_document()
{
$deleteParams = array();
$deleteParams['index'] = 'myindex';
$deleteParams['type'] = '_doc';
$deleteParams['id'] = '123';
$retDelete = $this->client->delete($deleteParams);
}
10、获取文档
//获取文档
public function get_document()
{
$getParams = array();
$getParams['index'] = 'myindex';
$getParams['type'] = '_doc';
$getParams['id'] = '12344';
$retDoc = $this->client->get($getParams);
print_r($retDoc);
}
11、文档查询
//查询
public function search()
{
$searchParams['index'] = 'myindex';
$searchParams['type'] = '_doc';
$searchParams['from'] = 0;
$searchParams['size'] = 100;
$searchParams['sort'] = array(
'_score' => array(
'order' => 'id'
)
);
//相当于sql语句: select * from hp_product where prodcut_name like '茶' limit 0,100 order by id desc;
$searchParams['body']['query']['match']['product_name'] = '茶';
$retDoc = $this->client->search($searchParams);
echo '<pre>';
print_r($retDoc);
//相当于sql语句: select * from hp_product where product_name like '茶' and product_id = 20 limit 200,10;
// $searchParams['body']['query']['bool']['must'] = array(
// array('match' => array('product_name' => '茶')),
// array('match' => array('product_id' => 20))
// );
// $searchParams['size'] = 10;
// $searchParams['from'] = 200;
//
//
// 当于sql语句:select * from hp_product where product_name like '茶' or product_id = 20 limit 200,10;
// $searchParams['body']['query']['bool']['should'] = array(
// array('match' => array('product_name' => '茶')),
// array('match' => array('product_id' => 20))
// );
//$searchParams['size'] = 10;
//$searchParams['from'] = 200;
//
//
// 当于sql语句: select * from hp_product where product_name like '茶' and product_id != 20 limit 200,10;
// $searchParams['body']['query']['bool']['must_not'] = array(
// array('match' => array('product_name' => '茶')),
// array('match' => array('product_id' => 20))
// );
//$searchParams['size'] = 10;
//$searchParams['from'] = 200;
//
//
//当于sql语句:select * from hp_product where id>=20 and id<30 limit 200,10;
// $searchParams['body']['query']['range'] = array(
// 'id' => array('gte' => 20,'lt' => 30);
// );
//$searchParams['size'] = 10;
//$searchParams['from'] = 200;
}