2018-07-10 18:01:57 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use Elasticsearch\ClientBuilder;
|
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
class ElasticSearchClient
|
|
|
|
{
|
2018-07-10 18:05:42 +02:00
|
|
|
private $__settings = false;
|
2018-07-10 18:01:57 +02:00
|
|
|
private $__client = false;
|
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
private function __getSetSettings()
|
|
|
|
{
|
2018-07-10 18:06:56 +02:00
|
|
|
$settings = array(
|
|
|
|
'enabled' => false,
|
|
|
|
'connection_string' => 'http://localhost',
|
|
|
|
);
|
2018-07-10 18:01:57 +02:00
|
|
|
|
2018-07-10 18:06:56 +02:00
|
|
|
foreach ($settings as $key => $setting) {
|
2018-07-10 18:05:42 +02:00
|
|
|
$temp = Configure::read('Plugin.ElasticSearch_' . $key);
|
2018-07-19 11:48:22 +02:00
|
|
|
if ($temp) {
|
|
|
|
$settings[$key] = $temp;
|
|
|
|
}
|
2018-07-10 18:06:56 +02:00
|
|
|
}
|
|
|
|
return $settings;
|
|
|
|
}
|
2018-07-10 18:01:57 +02:00
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
public function initTool()
|
|
|
|
{
|
2018-07-10 18:01:57 +02:00
|
|
|
$settings = $this->__getSetSettings();
|
|
|
|
$hosts = explode(",", $settings["connection_string"]);
|
2018-07-10 18:05:42 +02:00
|
|
|
$client = ClientBuilder::create()
|
2018-07-10 18:01:57 +02:00
|
|
|
->setHosts($hosts)
|
2018-07-19 11:48:22 +02:00
|
|
|
->build();
|
2018-07-10 18:01:57 +02:00
|
|
|
$this->__client = $client;
|
|
|
|
$this->__settings = $settings;
|
2018-07-10 18:06:56 +02:00
|
|
|
return $client;
|
|
|
|
}
|
2018-07-10 18:01:57 +02:00
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
public function pushDocument($index, $document_type, $document)
|
|
|
|
{
|
2018-07-10 18:01:57 +02:00
|
|
|
// Format timestamp
|
|
|
|
$time = strftime("%Y-%m-%d %H:%M:%S", strtotime($document["Log"]["created"]));
|
|
|
|
$document["Log"]["created"] = $time;
|
2021-02-10 10:19:34 +01:00
|
|
|
if (empty($document["Log"]["ip"])) {
|
|
|
|
$document["Log"]["ip"] = null;
|
|
|
|
}
|
2018-07-10 18:01:57 +02:00
|
|
|
$params = array(
|
|
|
|
'index' => $index,
|
|
|
|
'type' => $document_type,
|
|
|
|
'body' => $document
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->__client->index($params);
|
2018-07-10 18:06:56 +02:00
|
|
|
}
|
2018-07-10 18:01:57 +02:00
|
|
|
}
|