-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathElasticsearch.php
146 lines (130 loc) · 3.81 KB
/
Elasticsearch.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace Aternos\Model\Driver\Elasticsearch;
use Aternos\Model\Driver\Driver;
use Aternos\Model\Driver\Features\CRUDAbleInterface;
use Aternos\Model\Driver\Features\SearchableInterface;
use Aternos\Model\ModelInterface;
use Aternos\Model\Search\Search;
use Aternos\Model\Search\SearchResult;
use Elasticsearch\Client;
use Elasticsearch\ClientBuilder;
use Elasticsearch\Common\Exceptions\Missing404Exception;
use Exception;
/**
* Class Elasticsearch
*
* Inherit this class, overwrite the connect function
* and register the new class in the driver factory
* for other credentials or connect specifics
*
* @package Aternos\Model\Driver\Search
*/
class Elasticsearch extends Driver implements CRUDAbleInterface, SearchableInterface
{
public const ID = "elasticsearch";
protected string $id = self::ID;
/**
* @var Client|null
*/
protected ?Client $client = null;
/**
* Connect to the elasticsearch cluster
*/
protected function connect()
{
if (!$this->client) {
$this->client = ClientBuilder::create()->build();
}
}
/**
* Save the model
*
* @param ModelInterface $model
* @return bool
*/
public function save(ModelInterface $model): bool
{
$params = [
"index" => $model::getName(),
"id" => $model->getId(),
"body" => get_object_vars($model)
];
$this->connect();
$this->client->index($params);
return true;
}
/**
* Get the model
*
* @param class-string<ModelInterface> $modelClass
* @param mixed $id
* @return ModelInterface|null
* @throws Exception
*/
public function get(string $modelClass, mixed $id, ?ModelInterface $model = null): ?ModelInterface
{
$params = [
'index' => $modelClass::getName(),
$modelClass::getIdField() => $id
];
$this->connect();
try {
$response = $this->client->getSource($params);
} catch (Missing404Exception $e) {
return null;
}
if (!is_array($response)) {
return null;
}
if ($model) {
return $model->applyData($response);
}
return $modelClass::getModelFromData($response);
}
/**
* Delete the model
*
* @param ModelInterface $model
* @return bool
*/
public function delete(ModelInterface $model): bool
{
$params = [
"index" => $model::getName(),
"id" => $model->getId()
];
$this->client->delete($params);
return true;
}
/**
* @param Search $search
* @return SearchResult
*/
public function search(Search $search): SearchResult
{
/** @var class-string<ModelInterface> $modelClassName */
$modelClassName = $search->getModelClassName();
$params = [
'index' => $modelClassName::getName(),
'body' => $search->getSearchQuery()
];
$this->connect();
$response = $this->client->search($params);
if (!is_array($response) || !isset($response["hits"]) || !is_array($response["hits"]) || !isset($response["hits"]["hits"]) || !is_array($response["hits"]["hits"])) {
return new SearchResult(false);
}
$result = new SearchResult(true);
foreach ($response["hits"]["hits"] as $resultDocument) {
if (!isset($resultDocument["_source"]) || !is_array($resultDocument["_source"])) {
continue;
}
/** @var ModelInterface $model */
$model = new $modelClassName();
foreach ($resultDocument["_source"] as $key => $value) {
$model->{$key} = $value;
}
$result->add($model);
}
return $result;
}
}