-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathIndex.php
More file actions
257 lines (232 loc) · 6.69 KB
/
Index.php
File metadata and controls
257 lines (232 loc) · 6.69 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
namespace Flowpack\ElasticSearch\Domain\Model;
/*
* This file is part of the Flowpack.ElasticSearch package.
*
* (c) Contributors of the Flowpack Team - flowpack.org
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Flowpack\ElasticSearch\Exception as ElasticSearchException;
use Flowpack\ElasticSearch\Service\DynamicIndexSettingService;
use Flowpack\ElasticSearch\Transfer\Response;
use Neos\Utility\Arrays;
use Neos\Flow\Annotations as Flow;
/**
* Representation of an Index
*/
class Index
{
/**
* @var array
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-update-settings.html
*/
static protected $updatableSettings = [
'index.number_of_replicas',
'index.auto_expand_replicas',
'index.blocks.read_only',
'index.blocks.read',
'index.blocks.write',
'index.blocks.metadata',
'index.refresh_interval',
'index.index_concurrency',
'index.codec',
'index.codec.bloom.load',
'index.fail_on_merge_failure',
'index.translog.flush_threshold_ops',
'index.translog.flush_threshold_size',
'index.translog.flush_threshold_period',
'index.translog.disable_flush',
'index.cache.filter.max_size',
'index.cache.filter.expire',
'index.gateway.snapshot_interval',
'index.routing.allocation.include',
'index.routing.allocation.exclude',
'index.routing.allocation.require',
'index.routing.allocation.disable_allocation',
'index.routing.allocation.disable_new_allocation',
'index.routing.allocation.disable_replica_allocation',
'index.routing.allocation.enable',
'index.routing.allocation.total_shards_per_node',
'index.recovery.initial_shards',
'index.gc_deletes',
'index.ttl.disable_purge',
'index.translog.fs.type',
'index.compound_format',
'index.compound_on_flush',
'index.warmer.enabled',
];
/**
* @var DynamicIndexSettingService
* @Flow\Inject
*/
protected $dynamicIndexSettingService;
/**
* @var string
*/
protected $name;
/**
* @var string
*/
protected $settingsKey;
/**
* The owner client of this index. Could be set later in order to allow creating pending Index objects
*
* @var Client
*/
protected $client;
/**
* @var array
*/
protected $settings;
/**
* @param string $name
* @param Client $client $client
* @throws ElasticSearchException
*/
public function __construct($name, Client $client = null)
{
$name = trim($name);
if (empty($name) || substr($name, 0, 1) === '_') {
throw new ElasticSearchException('The provided index name "' . $name . '" must not be empty and not start with an underscore.', 1340187948);
} elseif ($name !== strtolower($name)) {
throw new ElasticSearchException('The provided index name "' . $name . '" must be all lowercase.', 1340187956);
}
$this->name = $name;
$this->settingsKey = $name;
$this->client = $client;
}
/**
* Inject the settings
*
* @param array $settings
* @return void
*/
public function injectSettings(array $settings)
{
$this->settings = $settings;
}
/**
* @param string $typeName
* @return AbstractType
*/
public function findType($typeName)
{
return new GenericType($this, $typeName);
}
/**
* @param array <AbstractType> $types
* @return TypeGroup
*/
public function findTypeGroup(array $types)
{
return new TypeGroup($this, $types);
}
/**
* @return boolean
*/
public function exists()
{
$response = $this->request('HEAD');
return $response->getStatusCode() === 200;
}
/**
* @param string $method
* @param string $path
* @param array $arguments
* @param string $content
* @param boolean $prefixIndex
* @param string $header
* @return Response
* @throws ElasticSearchException
*/
public function request($method, $path = null, array $arguments = [], $content = null, $prefixIndex = true, $header = null)
{
if ($this->client === null) {
throw new Exception('The client of the index "' . $this->name . '" is not set, hence no requests can be done.');
}
$path = ($path ? trim($path) : '');
if ($prefixIndex === true) {
$path = '/' . $this->name . $path;
} else {
$path = '/' . $path;
}
return $this->client->request($method, $path, $arguments, $content, $header);
}
/**
* @return void
*/
public function create()
{
$this->request('PUT', null, [], json_encode($this->getSettings()));
}
/**
* @return array|null
*/
protected function getSettings()
{
if ($this->client instanceof Client) {
$path = 'indexes.' . $this->client->getBundle() . '.' . $this->settingsKey;
} else {
$path = 'indexes.default' . '.' . $this->settingsKey;
}
$settings = Arrays::getValueByPath($this->settings, $path);
return $settings !== null ? $this->dynamicIndexSettingService->process($settings, $path, $this->getName()) : $settings;
}
/**
* @return void
*/
public function updateSettings()
{
$settings = $this->getSettings();
$updatableSettings = [];
foreach (static::$updatableSettings as $settingPath) {
$setting = Arrays::getValueByPath($settings, $settingPath);
if ($setting !== null) {
$updatableSettings = Arrays::setValueByPath($updatableSettings, $settingPath, $setting);
}
}
$this->request('PUT', '/_settings', [], json_encode($updatableSettings));
}
/**
* @return void
*/
public function delete()
{
$this->request('DELETE');
}
/**
* Refresh the index
*
* @return void
*/
public function refresh()
{
$this->request('POST', '/_refresh');
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param Client $client
* @return void
*/
public function setClient($client)
{
$this->client = $client;
}
/**
* @param string $settingsKey
* @return void
*/
public function setSettingsKey($settingsKey)
{
$this->settingsKey = $settingsKey;
}
}