Skip to content

Commit 64a0ecc

Browse files
committed
Meta getTotalHits()
+ toArray bug + Test
1 parent 11376e0 commit 64a0ecc

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/Data/QueryMeta.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ public function getError(): array
147147
return $this->error;
148148
}
149149

150+
public function getTotalHits(): int
151+
{
152+
return $this->hits;
153+
}
154+
150155
public function toArray(): array
151156
{
152157
$return = [
@@ -156,6 +161,7 @@ public function toArray(): array
156161
'timed_out' => $this->timed_out,
157162
'took' => $this->took,
158163
'total' => $this->total,
164+
'hits' => $this->hits,
159165
];
160166
if ($this->maxScore) {
161167
$return['max_score'] = $this->maxScore;

src/Eloquent/ElasticCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function getQueryMeta(): QueryMeta
5353

5454
public function getQueryMetaAsArray(): array
5555
{
56-
return $this->meta->asArray();
56+
return $this->meta->toArray();
5757
}
5858

5959
public function getDsl(): array

tests/ModelTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,3 +606,20 @@
606606
'id',
607607
'_id',
608608
]);
609+
610+
it('tests meta total hits', function () {
611+
$users = [];
612+
for ($i = 0; $i < 8500; $i++) {
613+
$users[] = [
614+
'name' => "User {$i}",
615+
'age' => rand(1, 100),
616+
'order' => $i,
617+
'title' => rand(0, 1) ? 'admin' : 'user',
618+
];
619+
}
620+
User::insert($users);
621+
622+
$query = User::wherePhrasePrefix('name', 'User')->limit(10)->get();
623+
expect($query->getQueryMeta()->getTotalHits())->toBe(8500)
624+
->and(count($query))->toBe(10);
625+
});

0 commit comments

Comments
 (0)