Skip to content

Commit bb2f828

Browse files
committed
Merge branch '5.x-dev' of github.com:pdphilip/laravel-elasticsearch into 5.x-dev
2 parents a1f8bbd + 1b53e00 commit bb2f828

File tree

8 files changed

+42
-41
lines changed

8 files changed

+42
-41
lines changed

src/Query/Builder.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,16 +2107,12 @@ public function withSuffix(string $suffix): self
21072107

21082108
public function getLimit(): int
21092109
{
2110-
return $this->getSetLimit();
2110+
return $this->getSetLimit() ?? $this->getDefaultLimit() ?? $this->connection->getDefaultLimit();
21112111
}
21122112

2113-
public function getSetLimit(): int
2113+
public function getSetLimit(): ?int
21142114
{
2115-
// If a limit was explicitly set we use that over the defaults.
2116-
return $this->limit
2117-
?? $this->options()->get('limit')
2118-
?? $this->getDefaultLimit()
2119-
?? $this->connection->getDefaultLimit();
2115+
return $this->options()->get('limit', $this->limit) ?? null;
21202116
}
21212117

21222118
public function getDefaultLimit(): ?int

src/Query/Grammar.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -548,9 +548,7 @@ public function _buildInnerHits($innerQuery)
548548
$innerHits['from'] = $innerQuery->offset;
549549
}
550550
if ($size = $innerQuery->getSetLimit()) {
551-
// TODO: David should we handle this diffrently?
552-
// Nested queries have a hard limit of 100 for inner_hits size
553-
$innerHits['size'] = min($size, 100);
551+
$innerHits['size'] = $size;
554552
}
555553

556554
return $innerHits;

src/Relations/Traits/QueriesRelationships.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,8 @@ trait QueriesRelationships
2727
*
2828
* @throws Exception
2929
*/
30-
public function has(
31-
$relation,
32-
$operator = '>=',
33-
$count = 1,
34-
$boolean = 'and',
35-
?Closure $callback = null
36-
): Builder|static {
30+
public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', ?Closure $callback = null): Builder|static
31+
{
3732
if (is_string($relation)) {
3833
if (str_contains($relation, '.')) {
3934
// @phpstan-ignore-next-line
@@ -79,13 +74,8 @@ protected function isAcrossConnections(Relation $relation): bool
7974
*
8075
* @throws Exception
8176
*/
82-
public function addHybridHas(
83-
Relation $relation,
84-
string $operator = '>=',
85-
int $count = 1,
86-
string $boolean = 'and',
87-
?Closure $callback = null
88-
): mixed {
77+
public function addHybridHas(Relation $relation, string $operator = '>=', int $count = 1, string $boolean = 'and', ?Closure $callback = null): mixed
78+
{
8979
$hasQuery = $relation->getQuery();
9080
if ($callback) {
9181
$hasQuery->callScope($callback);
@@ -102,14 +92,22 @@ public function addHybridHas(
10292
$relation instanceof MorphToMany => $relation->getInverse() ?
10393
$this->handleMorphedByMany($hasQuery, $relation) :
10494
$this->handleMorphToMany($hasQuery, $relation),
105-
default => $hasQuery->pluck($this->getHasCompareKey($relation))
95+
default => $this->handleDefaultForeignIdsLookup($hasQuery, $relation)
10696
};
10797

10898
$relatedIds = $this->getConstrainedRelatedIds($relations, $operator, $count);
10999

110100
return $this->whereIn($this->getRelatedConstraintKey($relation), $relatedIds, $boolean, $not);
111101
}
112102

103+
private function handleDefaultForeignIdsLookup($query, $relation)
104+
{
105+
$key = $this->getHasCompareKey($relation);
106+
$query->whereNotNull($key);
107+
108+
return $query->pluck($key);
109+
}
110+
113111
/**
114112
* @param Builder $hasQuery
115113
* @param Relation $relation

tests/GeospatialTest.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,23 @@
4040

4141
it('finds locations within a defined polygon', function () {
4242

43-
$locations = Location::whereGeoBox('location', ['lon' => -0.1450383, 'lat' => 51.5069158], ['lon' => -0.1270247, 'lat' => 51.5013233])->get();
43+
$expectedLon = -0.1392173;
44+
$expectedLat = 51.5045037;
45+
$topLeft = ['lon' => $expectedLon - 0.0005, 'lat' => $expectedLat + 0.0005];
46+
$bottomRight = ['lon' => $expectedLon + 0.0005, 'lat' => $expectedLat - 0.0005];
47+
48+
$locations = Location::whereGeoBox(
49+
'point',
50+
$topLeft,
51+
$bottomRight,
52+
)->get();
4453

4554
expect($locations->count())->toBe(1);
4655

47-
$locations->get()->each(function ($item) {
48-
expect($item->name)->toBe('StJamesPalace');
56+
$locations->each(function ($item) {
57+
expect($item->name)->toBe('St. James\'s Palace');
4958
});
50-
})->todo('need to add more checks around geo bounding box');
59+
});
5160

5261
it('finds locations near a point within max distance', function () {
5362
$locations = Location::whereGeoDistance('point', '200m', ['lat' => 51.5049537, 'lon' => -0.1392173])->get();

tests/ModelTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@
457457
});
458458

459459
expect($names)->toBe(['fork', 'spork', 'spoon']);
460-
})->todo();
460+
});
461461

462462
it('tests chunk across many items', function () {
463463
$users = [];

tests/Models/Group.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Group extends Model
1717

1818
protected static $unguarded = true;
1919

20-
public function users(): BelongsToMany
20+
public function groupUsers(): BelongsToMany
2121
{
2222
return $this->belongsToMany(User::class, 'users', 'groups', 'users', 'id', 'id', 'users');
2323
}

tests/Models/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function clients()
8989
return $this->belongsToMany(Client::class);
9090
}
9191

92-
public function groups()
92+
public function userGroups()
9393
{
9494
return $this->belongsToMany(Group::class, 'groups', 'users', 'groups', 'id', 'id', 'groups');
9595
}

tests/RelationsTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -321,22 +321,22 @@
321321

322322
it('tests belongs to many custom', function () {
323323
$user = User::create(['name' => 'John Doe']);
324-
$group = $user->groups()->create(['name' => 'Admins']);
324+
$group = $user->userGroups()->create(['name' => 'Admins']);
325325

326-
// Refetch
326+
// Re-fetch
327327
$user = User::find($user->id);
328328
$group = Group::find($group->id);
329329

330330
// Check for custom relation attributes
331331
expect($group->getAttributes())->toHaveKey('users')
332332
->and($user->getAttributes())->toHaveKey('groups')
333-
->and($user->groups->pluck('id')->toArray())->toContain($group->id)
334-
->and($group->users->pluck('id')->toArray())->toContain($user->id)
335-
->and($user->groups()->first()->id)->toBe($group->id)
336-
->and($group->users()->first()->id)->toBe($user->id);
333+
->and($user->userGroups->pluck('id')->toArray())->toContain($group->id)
334+
->and($group->groupUsers->pluck('id')->toArray())->toContain($user->id)
335+
->and($user->userGroups()->first()->id)->toBe($group->id)
336+
->and($group->groupUsers()->first()->id)->toBe($user->id);
337337

338338
// Assert they are attached
339-
})->todo();
339+
});
340340

341341
it('tests morph', function () {
342342
$user = User::create(['name' => 'John Doe']);
@@ -854,7 +854,7 @@
854854
$query->where('rating', '<', 5);
855855
})->get();
856856
expect($authors)->toHaveCount(1);
857-
})->todo();
857+
});
858858

859859
it('tests has one has', function () {
860860
$user1 = User::create(['name' => 'John Doe']);
@@ -874,7 +874,7 @@
874874

875875
$users = User::has('role', '!=', 0)->get();
876876
expect($users)->toHaveCount(2);
877-
})->todo();
877+
});
878878

879879
it('tests nested keys', function () {
880880
$client = Client::create([

0 commit comments

Comments
 (0)