1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under `AGPL, Commercial` license[s]. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/manganel |
7
|
|
|
* @license AGPL, Commercial |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Peter Maselkowski <[email protected]> |
10
|
|
|
* @link http://maslosoft.com/manganel/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Maslosoft\Manganel; |
14
|
|
|
|
15
|
|
|
use Closure; |
16
|
|
|
use Elasticsearch\Client; |
17
|
|
|
use Elasticsearch\ClientBuilder; |
18
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
19
|
|
|
use Maslosoft\EmbeDi\EmbeDi; |
20
|
|
|
use Maslosoft\Manganel\Decorators\QueryBuilder\ConditionDecorator; |
21
|
|
|
use Maslosoft\Manganel\Decorators\QueryBuilder\ConditionsDecorator; |
22
|
|
|
use Maslosoft\Manganel\Decorators\QueryBuilder\ScrollDecorator; |
23
|
|
|
use Maslosoft\Manganel\Decorators\QueryBuilder\SearchDecorator; |
24
|
|
|
use Maslosoft\Manganel\Meta\ManganelMeta; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Manganel |
28
|
|
|
* |
29
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
30
|
|
|
*/ |
31
|
|
|
class Manganel |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
const DefaultIndexId = 'manganel'; |
35
|
|
|
|
36
|
|
|
public $decorators = [ |
37
|
|
|
SearchCriteria::class => [ |
38
|
|
|
ConditionDecorator::class, |
39
|
|
|
ConditionsDecorator::class, |
40
|
|
|
ScrollDecorator::class, |
41
|
|
|
SearchDecorator::class, |
42
|
|
|
] |
43
|
|
|
]; |
44
|
|
|
public $hosts = [ |
45
|
|
|
'localhost:9200' |
46
|
|
|
]; |
47
|
|
|
public $auth = null; |
48
|
|
|
public $username = ''; |
49
|
|
|
public $password = ''; |
50
|
|
|
public $params = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* TODO Enforce lowercase |
54
|
|
|
*/ |
55
|
|
|
public $index = 'my_index'; |
56
|
|
|
public $indexId = self::DefaultIndexId; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Whether to use refresh option when indexing document. |
60
|
|
|
* NOTE: Due to performance reasons, this should be set to `true` only when |
61
|
|
|
* really necessary - so it can be also callback. |
62
|
|
|
* |
63
|
|
|
* Callback function signature: |
64
|
|
|
* ``` |
65
|
|
|
* function(AnnotatedInterface $model) |
66
|
|
|
* ``` |
67
|
|
|
* @var string|Closure |
68
|
|
|
*/ |
69
|
|
|
public $refresh = false; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* |
73
|
|
|
* @var Client |
74
|
|
|
*/ |
75
|
|
|
private $client = null; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Dependency injection container |
79
|
|
|
* @var EmbeDi |
80
|
|
|
*/ |
81
|
|
|
private $di = null; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Instances of manganel |
85
|
|
|
* @var Manganel[] |
86
|
|
|
*/ |
87
|
|
|
private static $mnl = []; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Hash map of class name to id. This is to reduce overhead of Mangan::fromModel() |
91
|
|
|
* @var string[] |
92
|
|
|
*/ |
93
|
|
|
private static $classToId = []; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Class constructor |
97
|
|
|
* @codeCoverageIgnore This is implicitly tested |
98
|
|
|
* @param string $indexId |
99
|
|
|
*/ |
100
|
|
|
public function __construct($indexId = self::DefaultIndexId) |
101
|
|
|
{ |
102
|
|
|
if (empty($indexId)) |
103
|
|
|
{ |
104
|
|
|
$indexId = self::DefaultIndexId; |
105
|
|
|
} |
106
|
|
|
$this->indexId = $indexId; |
107
|
|
|
$this->di = new EmbeDi($this->indexId); |
108
|
|
|
$this->di->configure($this); |
109
|
|
|
|
110
|
|
|
if (empty(self::$mnl[$indexId])) |
111
|
|
|
{ |
112
|
|
|
self::$mnl[$indexId] = $this; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @codeCoverageIgnore This is implicitly tested |
118
|
|
|
* @param AnnotatedInterface $model |
119
|
|
|
* @return static |
120
|
|
|
*/ |
121
|
|
|
public static function create(AnnotatedInterface $model) |
122
|
|
|
{ |
123
|
|
|
$key = get_class($model); |
124
|
|
|
if (isset(self::$classToId[$key])) |
125
|
|
|
{ |
126
|
|
|
$indexId = self::$classToId[$key]; |
127
|
|
|
} |
128
|
|
|
else |
129
|
|
|
{ |
130
|
|
|
$indexId = ManganelMeta::create($model)->type()->indexId; |
131
|
|
|
self::$classToId[$key] = $indexId; |
132
|
|
|
} |
133
|
|
|
return static::fly($indexId); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get flyweight instance of Manganel component. |
138
|
|
|
* Only one instance will be created for each `$indexId`. |
139
|
|
|
* |
140
|
|
|
* @codeCoverageIgnore This is implicitly tested |
141
|
|
|
* @new |
142
|
|
|
* @param string $indexId |
143
|
|
|
* @return Manganel |
144
|
|
|
*/ |
145
|
|
|
public static function fly($indexId = self::DefaultIndexId) |
146
|
|
|
{ |
147
|
|
|
if (empty($indexId)) |
148
|
|
|
{ |
149
|
|
|
$indexId = self::DefaultIndexId; |
150
|
|
|
} |
151
|
|
|
if (empty(self::$mnl[$indexId])) |
152
|
|
|
{ |
153
|
|
|
self::$mnl[$indexId] = new static($indexId); |
154
|
|
|
} |
155
|
|
|
return self::$mnl[$indexId]; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @codeCoverageIgnore This is implicitly tested |
160
|
|
|
*/ |
161
|
|
|
public function init() |
162
|
|
|
{ |
163
|
|
|
$this->di->store($this); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Drop current index |
168
|
|
|
* @return bool |
169
|
|
|
*/ |
170
|
1 |
|
public function drop() |
171
|
|
|
{ |
172
|
|
|
$params = [ |
173
|
1 |
|
'index' => strtolower($this->index) |
174
|
|
|
]; |
175
|
1 |
|
$result = $this->getClient()->indices()->delete($params); |
176
|
1 |
|
if (is_array($result) && array_key_exists('acknowledged', $result) && $result['acknowledged']) |
177
|
|
|
{ |
178
|
1 |
|
return true; |
179
|
|
|
} |
180
|
|
|
return false; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @codeCoverageIgnore This is implicitly tested |
185
|
|
|
* @return Client |
186
|
|
|
*/ |
187
|
|
|
public function getClient() |
188
|
|
|
{ |
189
|
|
|
if (null === $this->client) |
190
|
|
|
{ |
191
|
|
|
$this->params['hosts'] = $this->hosts; |
192
|
|
|
$this->params['connectionParams']['auth'] = [ |
193
|
|
|
$this->username, |
194
|
|
|
$this->password, |
195
|
|
|
$this->auth |
196
|
|
|
]; |
197
|
|
|
$cb = ClientBuilder::create(); |
198
|
|
|
$cb->setHosts($this->hosts); |
199
|
|
|
$this->client = $cb->build(); |
200
|
|
|
} |
201
|
|
|
return $this->client; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
} |
205
|
|
|
|