Skip to content

Commit 55909fe

Browse files
committed
Refine document routing API
1 parent 677cab0 commit 55909fe

9 files changed

Lines changed: 170 additions & 160 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ $documents->index('books', [
104104
]);
105105
```
106106

107-
Routing values can be attached by document ID:
107+
Document routing values can be attached by document ID:
108108

109109
```php
110-
use DirectoryTree\OpenSearchAdapter\Documents\Routing;
110+
use DirectoryTree\OpenSearchAdapter\Documents\DocumentRouting;
111111

112-
$routing = (new Routing)->add('1', 'tenant-1');
112+
$routing = DocumentRouting::make('1', 'tenant-1');
113113

114114
$documents->index('books', [
115115
new Document('1', [

src/Documents/DocumentManager.php

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,10 @@ public function __construct(
2828
*
2929
* @throws BulkRequestException
3030
*/
31-
public function index(
32-
string $indexName,
33-
array $documents,
34-
bool $refresh = false,
35-
?Routing $routing = null
36-
): self {
31+
public function index(string $index, array $documents, bool $refresh = false, ?DocumentRouting $routing = null): self
32+
{
3733
$params = [
38-
'index' => $indexName,
34+
'index' => $index,
3935
'refresh' => $refresh ? 'true' : 'false',
4036
'body' => [],
4137
];
@@ -59,27 +55,23 @@ public function index(
5955
/**
6056
* Delete the given documents from OpenSearch.
6157
*
62-
* @param array<int, string> $documentIds
58+
* @param array<int, string> $ids
6359
*
6460
* @throws BulkRequestException
6561
*/
66-
public function delete(
67-
string $indexName,
68-
array $documentIds,
69-
bool $refresh = false,
70-
?Routing $routing = null
71-
): self {
62+
public function delete(string $index, array $ids, bool $refresh = false, ?DocumentRouting $routing = null): self
63+
{
7264
$params = [
73-
'index' => $indexName,
65+
'index' => $index,
7466
'refresh' => $refresh ? 'true' : 'false',
7567
'body' => [],
7668
];
7769

78-
foreach ($documentIds as $documentId) {
79-
$delete = ['_id' => $documentId];
70+
foreach ($ids as $id) {
71+
$delete = ['_id' => $id];
8072

81-
if ($routing && $routing->has($documentId)) {
82-
$delete['routing'] = $routing->get($documentId);
73+
if ($routing && $routing->has($id)) {
74+
$delete['routing'] = $routing->get($id);
8375
}
8476

8577
$params['body'][] = compact('delete');
@@ -99,25 +91,24 @@ public function delete(
9991
*
10092
* @param array<string, mixed> $query
10193
*/
102-
public function deleteByQuery(string $indexName, array $query, bool $refresh = false): self
94+
public function deleteByQuery(string $index, array $query, bool $refresh = false): self
10395
{
104-
$params = [
105-
'index' => $indexName,
96+
$this->client->deleteByQuery([
97+
'index' => $index,
10698
'refresh' => $refresh ? 'true' : 'false',
10799
'body' => compact('query'),
108-
];
109-
110-
$this->client->deleteByQuery($params);
100+
]);
111101

112102
return $this;
113103
}
114104

115105
/**
116106
* Search an index using the given search request.
117107
*/
118-
public function search(string $indexName, SearchRequest $request): SearchResponse
108+
public function search(string $index, SearchRequest $request): SearchResponse
119109
{
120-
$params = array_merge($request->toArray(), ['index' => $indexName]);
110+
$params = array_merge($request->toArray(), ['index' => $index]);
111+
121112
$response = $this->client->search($params);
122113

123114
return new SearchResponse($response);

src/Documents/DocumentRouting.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace DirectoryTree\OpenSearchAdapter\Documents;
4+
5+
/**
6+
* @see https://docs.opensearch.org/latest/api-reference/document-apis/bulk/
7+
*/
8+
class DocumentRouting
9+
{
10+
/**
11+
* The routing values keyed by document ID.
12+
*
13+
* @var array<string, string>
14+
*/
15+
protected array $routes = [];
16+
17+
/**
18+
* Create a new routing instance for the given document ID.
19+
*/
20+
public static function make(string $id, string $value): self
21+
{
22+
return (new self)->add($id, $value);
23+
}
24+
25+
/**
26+
* Add a routing value for the given document ID.
27+
*/
28+
public function add(string $id, string $value): self
29+
{
30+
$this->routes[$id] = $value;
31+
32+
return $this;
33+
}
34+
35+
/**
36+
* Determine if routing exists for the given document ID.
37+
*/
38+
public function has(string $id): bool
39+
{
40+
return isset($this->routes[$id]);
41+
}
42+
43+
/**
44+
* Get the routing value for the given document ID.
45+
*/
46+
public function get(string $id): ?string
47+
{
48+
return $this->routes[$id] ?? null;
49+
}
50+
51+
/**
52+
* Get the document routing values.
53+
*
54+
* @return array<string, string>
55+
*/
56+
public function toArray(): array
57+
{
58+
return $this->routes;
59+
}
60+
}

src/Documents/Routing.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/Indices/IndexManager.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ public function __construct(Client $client)
2929
/**
3030
* Open the given index.
3131
*/
32-
public function open(string $indexName): self
32+
public function open(string $index): self
3333
{
3434
$this->indices->open([
35-
'index' => $indexName,
35+
'index' => $index,
3636
]);
3737

3838
return $this;
@@ -41,10 +41,10 @@ public function open(string $indexName): self
4141
/**
4242
* Close the given index.
4343
*/
44-
public function close(string $indexName): self
44+
public function close(string $index): self
4545
{
4646
$this->indices->close([
47-
'index' => $indexName,
47+
'index' => $index,
4848
]);
4949

5050
return $this;
@@ -53,10 +53,10 @@ public function close(string $indexName): self
5353
/**
5454
* Determine if the given index exists.
5555
*/
56-
public function exists(string $indexName): bool
56+
public function exists(string $index): bool
5757
{
5858
return $this->indices->exists([
59-
'index' => $indexName,
59+
'index' => $index,
6060
]);
6161
}
6262

@@ -73,10 +73,10 @@ public function create(IndexBlueprint $index): self
7373
/**
7474
* Update the mapping for the given index.
7575
*/
76-
public function putMapping(string $indexName, Mapping $mapping): self
76+
public function putMapping(string $index, Mapping $mapping): self
7777
{
7878
$this->indices->putMapping([
79-
'index' => $indexName,
79+
'index' => $index,
8080
'body' => $mapping->toArray(),
8181
]);
8282

@@ -86,10 +86,10 @@ public function putMapping(string $indexName, Mapping $mapping): self
8686
/**
8787
* Update the settings for the given index.
8888
*/
89-
public function putSettings(string $indexName, Settings $settings): self
89+
public function putSettings(string $index, Settings $settings): self
9090
{
9191
$this->indices->putSettings([
92-
'index' => $indexName,
92+
'index' => $index,
9393
'body' => [
9494
'settings' => $settings->toArray(),
9595
],
@@ -101,10 +101,10 @@ public function putSettings(string $indexName, Settings $settings): self
101101
/**
102102
* Delete the given index.
103103
*/
104-
public function delete(string $indexName): self
104+
public function delete(string $index): self
105105
{
106106
$this->indices->delete([
107-
'index' => $indexName,
107+
'index' => $index,
108108
]);
109109

110110
return $this;
@@ -115,13 +115,13 @@ public function delete(string $indexName): self
115115
*
116116
* @return array<string, Alias>
117117
*/
118-
public function getAliases(string $indexName): array
118+
public function getAliases(string $index): array
119119
{
120120
$response = $this->indices->getAlias([
121-
'index' => $indexName,
121+
'index' => $index,
122122
]);
123123

124-
$aliases = $response[$indexName]['aliases'] ?? [];
124+
$aliases = $response[$index]['aliases'] ?? [];
125125

126126
$results = [];
127127

@@ -139,10 +139,10 @@ public function getAliases(string $indexName): array
139139
/**
140140
* Create or update an alias for the given index.
141141
*/
142-
public function putAlias(string $indexName, Alias $alias): self
142+
public function putAlias(string $index, Alias $alias): self
143143
{
144144
$params = [
145-
'index' => $indexName,
145+
'index' => $index,
146146
'name' => $alias->name(),
147147
];
148148

@@ -158,10 +158,10 @@ public function putAlias(string $indexName, Alias $alias): self
158158
/**
159159
* Delete the given alias from the index.
160160
*/
161-
public function deleteAlias(string $indexName, string $aliasName): self
161+
public function deleteAlias(string $index, string $aliasName): self
162162
{
163163
$this->indices->deleteAlias([
164-
'index' => $indexName,
164+
'index' => $index,
165165
'name' => $aliasName,
166166
]);
167167

0 commit comments

Comments
 (0)