Skip to content

Commit e2124c8

Browse files
committed
Merge branch '5.x' into pr/79
2 parents 71de46e + 84cf795 commit e2124c8

File tree

5 files changed

+102
-1
lines changed

5 files changed

+102
-1
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: 34 additions & 1 deletion
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
// ======================================================================
@@ -2414,6 +2430,23 @@ public function withAnalyzer(string $analyzer): self
24142430
return $this;
24152431
}
24162432

2433+
public function withTrackTotalHits(bool|int|null $val = true): self
2434+
{
2435+
if ($val === null) {
2436+
return $this->withoutTrackTotalHits();
2437+
}
2438+
$this->bodyParameters['track_total_hits'] = $val;
2439+
2440+
return $this;
2441+
}
2442+
2443+
public function withoutTrackTotalHits(): self
2444+
{
2445+
unset($this->bodyParameters['track_total_hits']);
2446+
2447+
return $this;
2448+
}
2449+
24172450
// ----------------------------------------------------------------------
24182451
// Internal Operations
24192452
// ----------------------------------------------------------------------

tests/LargeRecordsTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PDPhilip\Elasticsearch\Tests\Models\Product;
6+
7+
beforeEach(function () {
8+
Product::executeSchema();
9+
10+
});
11+
12+
it('tests track total hits', function () {
13+
14+
Product::buildRecords(11_000);
15+
16+
class ProductWithDefaultTrackTotalHits extends Product
17+
{
18+
protected $connection = 'elasticsearch_with_default_track_total_hits';
19+
}
20+
21+
$products = Product::limit(1)->get();
22+
expect($products->getQueryMeta()->getTotalHits())->toBe(10000);
23+
24+
$products = Product::limit(1)->withTrackTotalHits()->get();
25+
expect($products->getQueryMeta()->getTotalHits())->toBe(11000);
26+
27+
$products = Product::limit(1)->withTrackTotalHits(false)->get();
28+
expect($products->getQueryMeta()->getTotalHits())->toBe(-1);
29+
30+
$products = Product::limit(1)->withTrackTotalHits(300)->get();
31+
expect($products->getQueryMeta()->getTotalHits())->toBe(300);
32+
33+
$products = ProductWithDefaultTrackTotalHits::limit(1)->get();
34+
expect($products->getQueryMeta()->getTotalHits())->toBe(11000);
35+
36+
$products = ProductWithDefaultTrackTotalHits::limit(1)->withoutTrackTotalHits()->get();
37+
expect($products->getQueryMeta()->getTotalHits())->toBe(10000);
38+
39+
$products = ProductWithDefaultTrackTotalHits::limit(1)->withTrackTotalHits(300)->get();
40+
expect($products->getQueryMeta()->getTotalHits())->toBe(300);
41+
42+
});

tests/Models/Product.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,17 @@ public static function executeSchema()
2929
$table->date('updated_at');
3030
});
3131
}
32+
33+
public static function buildRecords($limit = 100)
34+
{
35+
$records = [];
36+
while ($limit) {
37+
$records[] = [
38+
'state' => rand(1, 100),
39+
];
40+
$limit--;
41+
}
42+
Product::insert($records);
43+
// Product::withoutRefresh()->insert($records);
44+
}
3245
}

tests/TestCase.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,15 @@ protected function getEnvironmentSetUp($app): void
5656
'logging' => true,
5757
],
5858
]);
59+
60+
$app['config']->set('database.connections.elasticsearch_with_default_track_total_hits', [
61+
'driver' => 'elasticsearch',
62+
'auth_type' => 'http',
63+
'hosts' => ['http://localhost:9200'],
64+
'options' => [
65+
'track_total_hits' => true,
66+
'logging' => true,
67+
],
68+
]);
5969
}
6070
}

0 commit comments

Comments
 (0)