Skip to content

Commit 5d69ff4

Browse files
committed
'track_total_hits' option in the Connection for default Query Builder
1 parent 73531ac commit 5d69ff4

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/Connection.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ private function sanitizeConfig(): void
123123
'cert_password' => null,
124124
],
125125
'options' => [
126+
'track_total_hits' => null, // null -> skips - max 10k by default; true -> full values; false -> no hit tracking, returns -1; int -> max hit tracking val, ex 20000
126127
'bypass_map_validation' => false, // This skips the safety checks for Elastic Specific queries.
127128
'logging' => false,
128129
'ssl_verification' => true,
@@ -162,6 +163,8 @@ public function setOptions(): void
162163
{
163164
$this->allowIdSort = $this->config['options']['allow_id_sort'] ?? false;
164165

166+
$this->options()->add('track_total_hits', $this->config['options']['track_total_hits'] ?? null);
167+
165168
$this->options()->add('bypass_map_validation', $this->config['options']['bypass_map_validation'] ?? null);
166169

167170
if (isset($this->config['options']['ssl_verification'])) {

src/Query/Builder.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Illuminate\Contracts\Support\Arrayable;
1414
use Illuminate\Database\Query\Builder as BaseBuilder;
1515
use Illuminate\Database\Query\Expression;
16+
use Illuminate\Database\Query\Grammars\Grammar;
17+
use Illuminate\Database\Query\Processors\Processor;
1618
use Illuminate\Support\Arr;
1719
use Illuminate\Support\Collection;
1820
use Illuminate\Support\Str;
@@ -108,6 +110,12 @@ class Builder extends BaseBuilder
108110

109111
protected ?MetaDTO $metaTransfer = null;
110112

113+
public function __construct(...$args)
114+
{
115+
parent::__construct(...$args);
116+
$this->applyConnectionOptions();
117+
}
118+
111119
public function __call($method, $parameters)
112120
{
113121
if (Str::startsWith($method, 'filterWhere')) {
@@ -117,7 +125,7 @@ public function __call($method, $parameters)
117125
return parent::__call($method, $parameters);
118126
}
119127

120-
public function toDsl(): array
128+
public function toDsl(): array|string
121129
{
122130
$this->applyBeforeQueryCallbacks();
123131

@@ -129,6 +137,14 @@ public function toSql(): array|string
129137
return $this->toDsl();
130138
}
131139

140+
private function applyConnectionOptions()
141+
{
142+
$trackTotalHits = $this->connection->options()->get('track_total_hits');
143+
if ($trackTotalHits !== null) {
144+
$this->bodyParameters['track_total_hits'] = $trackTotalHits;
145+
}
146+
}
147+
132148
// ======================================================================
133149
// Inherited Methods
134150
// ======================================================================
@@ -2417,13 +2433,20 @@ public function withAnalyzer(string $analyzer): self
24172433
public function withTrackTotalHits(bool|int|null $val = true): self
24182434
{
24192435
if ($val === null) {
2420-
return $this;
2436+
return $this->withoutTrackTotalHits();
24212437
}
24222438
$this->bodyParameters['track_total_hits'] = $val;
24232439

24242440
return $this;
24252441
}
24262442

2443+
public function withoutTrackTotalHits(): self
2444+
{
2445+
unset($this->bodyParameters['track_total_hits']);
2446+
2447+
return $this;
2448+
}
2449+
24272450
// ----------------------------------------------------------------------
24282451
// Internal Operations
24292452
// ----------------------------------------------------------------------

0 commit comments

Comments
 (0)