Skip to content

Commit 5c1c6b7

Browse files
authored
Merge pull request #6 from olekjs/dev-release-v1.5.0
Dev release v1.5.0
2 parents cac0688 + 11a6dd8 commit 5c1c6b7

File tree

5 files changed

+77
-22
lines changed

5 files changed

+77
-22
lines changed

src/Contracts/Collectionable.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Olekjs\Elasticsearch\Contracts;
4+
5+
use Illuminate\Support\Collection;
6+
7+
interface Collectionable
8+
{
9+
public function toCollect(): Collection;
10+
}

src/Dto/SearchResponseDto.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
namespace Olekjs\Elasticsearch\Dto;
44

55
use Illuminate\Contracts\Support\Arrayable;
6+
use Illuminate\Support\Collection;
7+
use Olekjs\Elasticsearch\Contracts\Collectionable;
68
use Olekjs\Elasticsearch\Contracts\ResponseDtoInterface;
79

8-
class SearchResponseDto implements ResponseDtoInterface, Arrayable
10+
class SearchResponseDto implements ResponseDtoInterface, Arrayable, Collectionable
911
{
1012
public function __construct(
1113
private readonly int $took,
1214
private readonly bool $isTimedOut,
1315
private readonly ShardsResponseDto $shards,
14-
private readonly SearchHitsDto $results,
16+
private readonly SearchHitsDto $result,
1517
) {
1618
}
1719

@@ -25,9 +27,9 @@ public function getIsTimedOut(): bool
2527
return $this->isTimedOut;
2628
}
2729

28-
public function getResults(): SearchHitsDto
30+
public function getResult(): SearchHitsDto
2931
{
30-
return $this->results;
32+
return $this->result;
3133
}
3234

3335
public function getShards(): ShardsResponseDto
@@ -41,7 +43,17 @@ public function toArray(): array
4143
'took' => $this->getTook(),
4244
'is_timed_out' => $this->getIsTimedOut(),
4345
'shards' => $this->getShards()->toArray(),
44-
'results' => $this->getResults()->toArray(),
46+
'results' => $this->getResult()->toArray(),
4547
];
4648
}
49+
50+
public function toCollect(): Collection
51+
{
52+
$hits = array_map(
53+
fn(SearchHitDto $searchHitDto) => $searchHitDto->getSource(),
54+
$this->getResult()->getHits()
55+
);
56+
57+
return Collection::make($hits);
58+
}
4759
}

src/Utils/SearchResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static function from(Response $response, array $data = []): SearchRespons
2222
failed: data_get($response, '_shards.failed'),
2323
skipped: data_get($response, '_shards.skipped'),
2424
),
25-
results: new SearchHitsDto(
25+
result: new SearchHitsDto(
2626
total: data_get($response, 'hits.total'),
2727
maxScore: data_get($response, 'hits.max_score'),
2828
hits: array_map(

tests/Integration/ClientTest.php

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Olekjs\Elasticsearch\Tests\Integration;
44

5+
use Illuminate\Support\Collection;
56
use Illuminate\Support\Facades\Http;
67
use Olekjs\Elasticsearch\Client;
78
use Olekjs\Elasticsearch\Dto\PaginateResponseDto;
9+
use Olekjs\Elasticsearch\Dto\SearchHitDto;
810
use Olekjs\Elasticsearch\Dto\SearchResponseDto;
911
use Olekjs\Elasticsearch\Exceptions\ConflictResponseException;
1012
use Olekjs\Elasticsearch\Exceptions\DeleteResponseException;
@@ -43,10 +45,10 @@ public function testSearchMethod(): void
4345

4446
$this->assertFalse($result->getIsTimedOut());
4547

46-
$this->assertSame(['value' => 5, 'relation' => 'eq'], $result->getResults()->getTotal());
47-
$this->assertSame(1.0, $result->getResults()->getMaxScore());
48+
$this->assertSame(['value' => 5, 'relation' => 'eq'], $result->getResult()->getTotal());
49+
$this->assertSame(1.0, $result->getResult()->getMaxScore());
4850

49-
foreach ($result->getResults()->getHits() as $hit) {
51+
foreach ($result->getResult()->getHits() as $hit) {
5052
$this->assertSame('hello', $hit->getIndex());
5153
$this->assertSame('183865906814918156', $hit->getId());
5254
$this->assertSame(1.0, $hit->getScore());
@@ -447,9 +449,9 @@ public function testEmptyResponseInSearchMethod(): void
447449

448450
$this->assertFalse($result->getIsTimedOut());
449451

450-
$this->assertSame(['value' => 0, 'relation' => 'eq'], $result->getResults()->getTotal());
451-
$this->assertNull($result->getResults()->getMaxScore());
452-
$this->assertEmpty($result->getResults()->getHits());
452+
$this->assertSame(['value' => 0, 'relation' => 'eq'], $result->getResult()->getTotal());
453+
$this->assertNull($result->getResult()->getMaxScore());
454+
$this->assertEmpty($result->getResult()->getHits());
453455
}
454456

455457
public function testSearchWhereInMethod(): void
@@ -473,10 +475,10 @@ public function testSearchWhereInMethod(): void
473475

474476
$this->assertFalse($result->getIsTimedOut());
475477

476-
$this->assertSame(['value' => 2, 'relation' => 'eq'], $result->getResults()->getTotal());
477-
$this->assertSame(1.0, $result->getResults()->getMaxScore());
478+
$this->assertSame(['value' => 2, 'relation' => 'eq'], $result->getResult()->getTotal());
479+
$this->assertSame(1.0, $result->getResult()->getMaxScore());
478480

479-
foreach ($result->getResults()->getHits() as $hit) {
481+
foreach ($result->getResult()->getHits() as $hit) {
480482
$this->assertSame('hello', $hit->getIndex());
481483
$this->assertSame('183865906814918156', $hit->getId());
482484
$this->assertSame(1.0, $hit->getScore());
@@ -505,10 +507,10 @@ public function testSearchWhereKeywordMethod(): void
505507

506508
$this->assertFalse($result->getIsTimedOut());
507509

508-
$this->assertSame(['value' => 1, 'relation' => 'eq'], $result->getResults()->getTotal());
509-
$this->assertSame(1.6739764, $result->getResults()->getMaxScore());
510+
$this->assertSame(['value' => 1, 'relation' => 'eq'], $result->getResult()->getTotal());
511+
$this->assertSame(1.6739764, $result->getResult()->getMaxScore());
510512

511-
foreach ($result->getResults()->getHits() as $hit) {
513+
foreach ($result->getResult()->getHits() as $hit) {
512514
$this->assertSame('hello', $hit->getIndex());
513515
$this->assertSame('183865906814918156', $hit->getId());
514516
$this->assertSame(1.6739764, $hit->getScore());
@@ -537,10 +539,10 @@ public function testSearchWhereLikeMethod(): void
537539

538540
$this->assertFalse($result->getIsTimedOut());
539541

540-
$this->assertSame(['value' => 1, 'relation' => 'eq'], $result->getResults()->getTotal());
541-
$this->assertSame(1.0, $result->getResults()->getMaxScore());
542+
$this->assertSame(['value' => 1, 'relation' => 'eq'], $result->getResult()->getTotal());
543+
$this->assertSame(1.0, $result->getResult()->getMaxScore());
542544

543-
foreach ($result->getResults()->getHits() as $hit) {
545+
foreach ($result->getResult()->getHits() as $hit) {
544546
$this->assertSame('hello', $hit->getIndex());
545547
$this->assertSame('183865906814918156', $hit->getId());
546548
$this->assertSame(1.0, $hit->getScore());
@@ -657,4 +659,27 @@ public function testPaginateMethod(): void
657659

658660
$this->assertInstanceOf(SearchResponseDto::class, $result->getDocuments());
659661
}
662+
663+
public function testSearchResponseCanBeConvertedToCollection(): void
664+
{
665+
Http::fake(function () {
666+
return Http::response(
667+
file_get_contents('tests/Responses/search_success_response.json')
668+
);
669+
});
670+
671+
$client = new Client();
672+
673+
$result = $client->search('hello', [
674+
'query' => [
675+
'match_all' => (object)[]
676+
]
677+
]);
678+
679+
$this->assertInstanceOf(Collection::class, $result->toCollect());
680+
681+
foreach ($result->toCollect() as $source) {
682+
$this->assertSame(['hello' => 'world'], $source);
683+
}
684+
}
660685
}

tests/Unit/DtosTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
use Olekjs\Elasticsearch\Dto\ShardsResponseDto;
1212
use PHPUnit\Framework\TestCase;
1313
use Illuminate\Contracts\Support\Arrayable;
14+
use Olekjs\Elasticsearch\Contracts\Collectionable;
1415

1516
class DtosTest extends TestCase
1617
{
17-
public function testDtosAreArrayableMethod(): void
18+
public function testDtosAreArrayable(): void
1819
{
1920
$dtos = [
2021
FindResponseDto::class,
@@ -32,4 +33,11 @@ public function testDtosAreArrayableMethod(): void
3233
$this->assertContains(Arrayable::class, $interfaces);
3334
}
3435
}
36+
37+
public function testSearchResponseIsCollectionable(): void
38+
{
39+
$interfaces = class_implements(SearchResponseDto::class);
40+
41+
$this->assertContains(Collectionable::class, $interfaces);
42+
}
3543
}

0 commit comments

Comments
 (0)