Skip to content

Commit 8595ad2

Browse files
committed
Default limit from model
- Default limit comes from model - removed default property type casting as to be inline with Laravel
1 parent 00b1f14 commit 8595ad2

File tree

7 files changed

+47
-12
lines changed

7 files changed

+47
-12
lines changed

src/Connection.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,11 @@ protected function getDefaultPostProcessor(): Query\Processor
328328
return new Query\Processor;
329329
}
330330

331+
public function getDefaultLimit(): int
332+
{
333+
return $this->defaultQueryLimit;
334+
}
335+
331336
// ----------------------------------------------------------------------
332337
// Connection Setters
333338
// ----------------------------------------------------------------------

src/Eloquent/ElasticsearchModel.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,33 @@ trait ElasticsearchModel
3232

3333
protected ?Relation $parentRelation;
3434

35-
protected int $defaultLimit = 1000;
35+
/**
36+
* The default results limit for the model.
37+
*
38+
* @var int
39+
*/
40+
protected $defaultLimit = 0;
3641

37-
protected array $mappingMap = [];
42+
/**
43+
* The default field mapping that will be used to validate keyword fields
44+
*
45+
* @var array
46+
*/
47+
protected $mappingMap = [];
3848

39-
protected bool $generatesUniqueIds = false;
49+
/**
50+
* Model generates unique ids or Elasticsearch generates them.
51+
*
52+
* @var bool
53+
*/
54+
protected $generatesUniqueIds = false;
4055

41-
protected bool $storeIdInDocument = false;
56+
/**
57+
* Option to store the id in the document.
58+
*
59+
* @var bool
60+
*/
61+
protected $storeIdInDocument = false;
4262

4363
// ----------------------------------------------------------------------
4464
// Meta

src/Eloquent/Model.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ public function __construct(array $attributes = [])
2626
if (empty($this->attributes['id']) && $this->generatesUniqueIds) {
2727
$this->attributes['id'] = $this->newUniqueId();
2828
}
29-
$this->meta = new ModelMeta($this->getTable(), $this->getConnection()->getTablePrefix());
29+
$connection = $this->getConnection();
30+
$this->meta = new ModelMeta($this->getTable(), $connection->getTablePrefix());
31+
if (! $this->defaultLimit) {
32+
$this->defaultLimit = $connection->getDefaultLimit();
33+
}
34+
3035
}
3136

3237
public function getTable()

src/Query/Builder.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,12 +2076,17 @@ public function withSuffix(string $suffix): self
20762076

20772077
public function getLimit(): int
20782078
{
2079-
return $this->getSetLimit() > 0 ? $this->getSetLimit() : $this->connection->defaultQueryLimit;
2079+
return $this->getSetLimit() ?? $this->getDefaultLimit() ?? $this->connection->getDefaultLimit();
20802080
}
20812081

2082-
public function getSetLimit(): int
2082+
public function getSetLimit(): ?int
20832083
{
2084-
return $this->options()->get('limit', $this->limit) ?? 0;
2084+
return $this->options()->get('limit', $this->limit) ?? null;
2085+
}
2086+
2087+
public function getDefaultLimit(): ?int
2088+
{
2089+
return $this->options()->get('default_limit', $this->limit) ?? null;
20852090
}
20862091

20872092
protected function hasProcessedSelect(): bool

src/Query/Grammar.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public function compileSelect($query): array
205205

206206
}
207207
$dsl->setBody(['aggs'], $this->compileNestedTermAggregations($fields, $query, $aggs));
208-
$dsl->setBody(['size'], $query->getSetLimit());
208+
$dsl->setBody(['size'], $query->getSetLimit() ?? 0);
209209
$dsl->unsetBody(['sort']);
210210
} else {
211211
// else nothing to aggregate - just a normal query as all records will be distinct anyway
@@ -1053,7 +1053,7 @@ protected function compileCategorizeTextAggregation(Builder $builder, array $agg
10531053
protected function compileCompositeAggregation(Builder $builder, array $aggregation): array
10541054
{
10551055
$sources = $aggregation['args'];
1056-
$size = $builder->getSetLimit();
1056+
$size = $builder->getSetLimit() ?? 0;
10571057
$afterKey = $builder->afterKey ?? null;
10581058
$options = $aggregation['options'] ?? [];
10591059

tests/Models/Post.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Post extends Model
1616

1717
protected static $unguarded = true;
1818

19-
protected array $mappingMap = [
19+
protected $mappingMap = [
2020
'comments.country' => 'comments.country.keyword',
2121
'comments.likes' => 'comments.likes',
2222
];

tests/Models/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
3333
'member_status' => MemberStatus::class,
3434
];
3535

36-
protected array $mappingMap = [
36+
protected $mappingMap = [
3737
'title' => 'title.keyword',
3838
];
3939

0 commit comments

Comments
 (0)