-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexManager.php
More file actions
183 lines (153 loc) · 3.97 KB
/
Copy pathIndexManager.php
File metadata and controls
183 lines (153 loc) · 3.97 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
<?php
namespace DirectoryTree\OpenSearchAdapter\Indices;
use OpenSearch\Client;
use OpenSearch\Namespaces\IndicesNamespace;
/**
* @see https://docs.opensearch.org/latest/api-reference/index-apis/create-index/
* @see https://docs.opensearch.org/latest/api-reference/index-apis/put-mapping/
* @see https://docs.opensearch.org/latest/api-reference/index-apis/update-settings/
* @see https://docs.opensearch.org/latest/api-reference/index-apis/alias/
*/
class IndexManager implements IndexManagerInterface
{
/**
* The OpenSearch indices namespace.
*/
protected IndicesNamespace $indices;
/**
* Create a new index manager instance.
*/
public function __construct(Client $client)
{
$this->indices = $client->indices();
}
/**
* Open the given index.
*/
public function open(string $index): static
{
$this->indices->open([
'index' => $index,
]);
return $this;
}
/**
* Close the given index.
*/
public function close(string $index): static
{
$this->indices->close([
'index' => $index,
]);
return $this;
}
/**
* Determine if the given index exists.
*/
public function exists(string $index): bool
{
return $this->indices->exists([
'index' => $index,
]);
}
/**
* Create an index from the given blueprint.
*/
public function create(IndexBlueprint $index): static
{
$this->indices->create($index->toArray());
return $this;
}
/**
* Update the mapping for the given index.
*/
public function putMapping(string $index, Mapping $mapping): static
{
$this->indices->putMapping([
'index' => $index,
'body' => $mapping->toArray(),
]);
return $this;
}
/**
* Update the settings for the given index.
*/
public function putSettings(string $index, Settings $settings): static
{
$this->indices->putSettings([
'index' => $index,
'body' => [
'settings' => $settings->toArray(),
],
]);
return $this;
}
/**
* Delete the given index.
*/
public function delete(string $index): static
{
$this->indices->delete([
'index' => $index,
]);
return $this;
}
/**
* Get the aliases for the given index.
*
* @return array<string, Alias>
*/
public function getAliases(string $index): array
{
$response = $this->indices->getAlias([
'index' => $index,
]);
$aliases = $response[$index]['aliases'] ?? [];
$results = [];
foreach ($aliases as $name => $parameters) {
$results[$name] = new Alias(
$name,
$parameters['filter'] ?? null,
$parameters['routing'] ?? null,
$parameters['is_write_index'] ?? null,
);
}
return $results;
}
/**
* Create or update an alias for the given index.
*/
public function putAlias(string $index, Alias $alias): static
{
$params = [
'index' => $index,
'name' => $alias->name(),
];
if ($body = $alias->toArray()) {
$params['body'] = $body;
}
$this->indices->putAlias($params);
return $this;
}
/**
* Delete the given alias from the index.
*/
public function deleteAlias(string $index, string $aliasName): static
{
$this->indices->deleteAlias([
'index' => $index,
'name' => $aliasName,
]);
return $this;
}
/**
* Atomically apply multiple alias actions.
*/
public function updateAliases(AliasActions $actions): static
{
$this->indices->updateAliases([
'body' => $actions->toArray(),
]);
return $this;
}
}