EsBase類
<?php
namespace App\Helpers\Classes;
use Elasticsearch\ClientBuilder;
class EsBaseHelper
{
private $client;
public function __construct($es = null)
{
$host = getenv('ES_SCHEME').'://'.getenv('ES_HOSTS').':'.getenv('ES_PORT');
$hosts = array($host);
$this->client = ClientBuilder::create()->setHosts($hosts)->build();
}
public function create_index($index,$type)
{
$indexParams['index'] = $index;
$indexParams['type'] = $type;
$indexParams['body']['settings']['number_of_shards'] = 5;
$indexParams['body']['settings']['number_of_replicas'] = 1;
//$indexParams = json_encode($indexParams);
$this->client->create($indexParams);
}
public function add_document($params)
{
/* $params = array();
$params['body'] = array(
'testField' => 'dfdsfdsf'
);
$params['index'] = 'my_index';
$params['type'] = 'my_index';
$params['id'] = 'w1231313';*/
$ret = $this->client->index($params);
return $ret;
}
public function delete_index($deleteParams)
{
/* $deleteParams['index'] = 'my_index55';*/
$cc = $this->client->indices()->delete($deleteParams);
return $cc;
}
public function delete_document($deleteParams)
{
/*
$deleteParams = array();
$deleteParams['index'] = 'my_index';
$deleteParams['type'] = 'my_index';
$deleteParams['id'] = 'AU4Kmmj-WOmOrmyOj2qf';*/
$retDelete = $this->client->delete($deleteParams);
return $retDelete;
}
public function update_document($updateParams)
{
/*$updateParams = array();
$updateParams['index'] = 'my_index';
$updateParams['type'] = 'my_index';
$updateParams['id'] = 'my_id';
$updateParams['body']['doc']['asas'] = '111111';*/
$response = $this->client->update($updateParams);
return $response;
}
public function search($searchParams)
{
/*
$searchParams['index'] = 'assets_7_74';
$searchParams['type'] = 'ips';
$searchParams['from'] = 0;
$searchParams['size'] = 100;
$searchParams['body']['query']['match']['ip'] = '63.143.44.13';
*/
$retDoc = $this->client->search($searchParams);
return $retDoc;
}
//只查詢一個
public function get_document($getParams){
/*$getParams = array();
$getParams['index'] = 'assets_42_294';
$getParams['type'] = 'ips';
$getParams['id'] = '123.60.50.199';*/
$retDoc = $this->client->get($getParams);
return $retDoc;
}
//判斷是否存在
public function exists($getParams){
/* $getParams = array(
'index' => 'my_index',
'type' => 'my_index',
'id' => 'w1231313'
);*/
$exists = $this->client->exists($getParams);
return $exists;
}
public function scroll($searchParam){
/*
$searchParams['scroll_id'] = 'cccccc';
$searchParams['scroll'] = '30s';
*/
return $this->client->scroll($searchParam);
}
public function get_scroll_id($searchParam){
/*
$searchParams['index'] = 'assets_7_74';
$searchParams['search_type'] = 'scan';
$searchParams['scroll'] = "30s";
$searchParams['size'] = 5000;
$searchParams['body']['query']['match']['ip'] = '63.143.44.13';
*/
$docs = $this->client->search($searchParam);
//return $docs['_scroll_id'];
return $docs;
}
public function bulk($bulk){
$res = $this->client->bulk($bulk);
return $res;
}
}
.env
.
.
# Es
ES_HOSTS=127.0.0.1
ES_PORT=9200
ES_SCHEME=http
ES_USER=
ES_PASS=
本作品採用《CC 協議》,轉載必須註明作者和本文連結