Skip to content

Commit 8f8f4dd

Browse files
authored
Merge pull request #8 from olekjs/dev-release-v1.7.0
v1.7.0 add crud operations to builder
2 parents dbcf56d + d8ca203 commit 8f8f4dd

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

src/Builder/Builder.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@
1010
use Olekjs\Elasticsearch\Contracts\ClientInterface;
1111
use Olekjs\Elasticsearch\Dto\BulkResponseDto;
1212
use Olekjs\Elasticsearch\Dto\FindResponseDto;
13+
use Olekjs\Elasticsearch\Dto\IndexResponseDto;
1314
use Olekjs\Elasticsearch\Dto\PaginateResponseDto;
1415
use Olekjs\Elasticsearch\Dto\SearchResponseDto;
16+
use Olekjs\Elasticsearch\Exceptions\ConflictResponseException;
1517
use Olekjs\Elasticsearch\Exceptions\CoreException;
18+
use Olekjs\Elasticsearch\Exceptions\DeleteResponseException;
1619
use Olekjs\Elasticsearch\Exceptions\FindResponseException;
1720
use Olekjs\Elasticsearch\Exceptions\IndexNotFoundResponseException;
21+
use Olekjs\Elasticsearch\Exceptions\IndexResponseException;
1822
use Olekjs\Elasticsearch\Exceptions\NotFoundResponseException;
1923
use Olekjs\Elasticsearch\Exceptions\SearchResponseException;
24+
use Olekjs\Elasticsearch\Exceptions\UpdateResponseException;
2025

2126
class Builder implements BuilderInterface
2227
{
@@ -325,6 +330,50 @@ public function bulk(BulkOperationInterface $bulk): BulkResponseDto
325330
return $this->client->bulk($bulk);
326331
}
327332

333+
/**
334+
* @throws IndexResponseException
335+
*/
336+
public function create(string|int $id, array $data): IndexResponseDto
337+
{
338+
if (!isset($this->index)) {
339+
throw new LogicException('Index name is required.');
340+
}
341+
342+
return $this->client->create($this->index, $id, $data);
343+
}
344+
345+
/**
346+
* @throws NotFoundResponseException
347+
* @throws UpdateResponseException
348+
* @throws ConflictResponseException
349+
*/
350+
public function update(
351+
string|int $id,
352+
array $data = [],
353+
array $script = [],
354+
?int $primaryTerm = null,
355+
?int $sequenceNumber = null
356+
): IndexResponseDto {
357+
if (!isset($this->index)) {
358+
throw new LogicException('Index name is required.');
359+
}
360+
361+
return $this->client->update($this->index, $id, $data, $script, $primaryTerm, $sequenceNumber);
362+
}
363+
364+
/**
365+
* @throws DeleteResponseException
366+
* @throws NotFoundResponseException
367+
*/
368+
public function delete(string|int $id): IndexResponseDto
369+
{
370+
if (!isset($this->index)) {
371+
throw new LogicException('Index name is required.');
372+
}
373+
374+
return $this->client->delete($this->index, $id);
375+
}
376+
328377
public function getIndex(): string
329378
{
330379
return $this->index;

src/Contracts/BuilderInterface.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66
use Olekjs\Elasticsearch\Builder\Builder;
77
use Olekjs\Elasticsearch\Dto\BulkResponseDto;
88
use Olekjs\Elasticsearch\Dto\FindResponseDto;
9+
use Olekjs\Elasticsearch\Dto\IndexResponseDto;
910
use Olekjs\Elasticsearch\Dto\PaginateResponseDto;
1011
use Olekjs\Elasticsearch\Dto\SearchResponseDto;
12+
use Olekjs\Elasticsearch\Exceptions\ConflictResponseException;
1113
use Olekjs\Elasticsearch\Exceptions\CoreException;
14+
use Olekjs\Elasticsearch\Exceptions\DeleteResponseException;
1215
use Olekjs\Elasticsearch\Exceptions\FindResponseException;
1316
use Olekjs\Elasticsearch\Exceptions\IndexNotFoundResponseException;
17+
use Olekjs\Elasticsearch\Exceptions\IndexResponseException;
1418
use Olekjs\Elasticsearch\Exceptions\NotFoundResponseException;
1519
use Olekjs\Elasticsearch\Exceptions\SearchResponseException;
20+
use Olekjs\Elasticsearch\Exceptions\UpdateResponseException;
1621

1722
interface BuilderInterface
1823
{
@@ -99,6 +104,30 @@ public function paginate(int $page = 1, int $perPage = 25): PaginateResponseDto;
99104
*/
100105
public function bulk(BulkOperationInterface $bulk): BulkResponseDto;
101106

107+
/**
108+
* @throws IndexResponseException
109+
*/
110+
public function create(string|int $id, array $data): IndexResponseDto;
111+
112+
/**
113+
* @throws NotFoundResponseException
114+
* @throws UpdateResponseException
115+
* @throws ConflictResponseException
116+
*/
117+
public function update(
118+
string|int $id,
119+
array $data = [],
120+
array $script = [],
121+
?int $primaryTerm = null,
122+
?int $sequenceNumber = null
123+
): IndexResponseDto;
124+
125+
/**
126+
* @throws DeleteResponseException
127+
* @throws NotFoundResponseException
128+
*/
129+
public function delete(string|int $id): IndexResponseDto;
130+
102131
public function getIndex(): string;
103132

104133
public function getBody(): array;

tests/Unit/BuilderTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Olekjs\Elasticsearch\Client;
99
use Olekjs\Elasticsearch\Dto\BulkResponseDto;
1010
use Olekjs\Elasticsearch\Dto\FindResponseDto;
11+
use Olekjs\Elasticsearch\Dto\IndexResponseDto;
1112
use Olekjs\Elasticsearch\Dto\PaginateResponseDto;
1213
use Olekjs\Elasticsearch\Dto\SearchHitsDto;
1314
use Olekjs\Elasticsearch\Dto\SearchResponseDto;
@@ -515,4 +516,31 @@ public function testBulkMethod(): void
515516

516517
$this->assertInstanceOf(BulkResponseDto::class, $result);
517518
}
519+
520+
public function testCreateMethod(): void
521+
{
522+
$client = $this->getMockBuilder(Client::class)->getMock();
523+
524+
$result = Builder::query($client)->index('test')->create(1, []);
525+
526+
$this->assertInstanceOf(IndexResponseDto::class, $result);
527+
}
528+
529+
public function testUpdateMethod(): void
530+
{
531+
$client = $this->getMockBuilder(Client::class)->getMock();
532+
533+
$result = Builder::query($client)->index('test')->update(1, ['name' => 'test']);
534+
535+
$this->assertInstanceOf(IndexResponseDto::class, $result);
536+
}
537+
538+
public function testDeleteMethod(): void
539+
{
540+
$client = $this->getMockBuilder(Client::class)->getMock();
541+
542+
$result = Builder::query($client)->index('test')->delete(1);
543+
544+
$this->assertInstanceOf(IndexResponseDto::class, $result);
545+
}
518546
}

0 commit comments

Comments
 (0)