Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ private function sanitizeConfig(): void
'cert_password' => null,
],
'options' => [
'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
'bypass_map_validation' => false, // This skips the safety checks for Elastic Specific queries.
'logging' => false,
'ssl_verification' => true,
Expand Down Expand Up @@ -162,6 +163,8 @@ public function setOptions(): void
{
$this->allowIdSort = $this->config['options']['allow_id_sort'] ?? false;

$this->options()->add('track_total_hits', $this->config['options']['track_total_hits'] ?? null);

$this->options()->add('bypass_map_validation', $this->config['options']['bypass_map_validation'] ?? null);

if (isset($this->config['options']['ssl_verification'])) {
Expand Down
35 changes: 34 additions & 1 deletion src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Query\Builder as BaseBuilder;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Query\Grammars\Grammar;
use Illuminate\Database\Query\Processors\Processor;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -108,6 +110,12 @@ class Builder extends BaseBuilder

protected ?MetaDTO $metaTransfer = null;

public function __construct(...$args)
{
parent::__construct(...$args);
$this->applyConnectionOptions();
}

public function __call($method, $parameters)
{
if (Str::startsWith($method, 'filterWhere')) {
Expand All @@ -117,7 +125,7 @@ public function __call($method, $parameters)
return parent::__call($method, $parameters);
}

public function toDsl(): array
public function toDsl(): array|string
{
$this->applyBeforeQueryCallbacks();

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

private function applyConnectionOptions()
{
$trackTotalHits = $this->connection->options()->get('track_total_hits');
if ($trackTotalHits !== null) {
$this->bodyParameters['track_total_hits'] = $trackTotalHits;
}
}

// ======================================================================
// Inherited Methods
// ======================================================================
Expand Down Expand Up @@ -2414,6 +2430,23 @@ public function withAnalyzer(string $analyzer): self
return $this;
}

public function withTrackTotalHits(bool|int|null $val = true): self
{
if ($val === null) {
return $this->withoutTrackTotalHits();
}
$this->bodyParameters['track_total_hits'] = $val;

return $this;
}

public function withoutTrackTotalHits(): self
{
unset($this->bodyParameters['track_total_hits']);

return $this;
}

// ----------------------------------------------------------------------
// Internal Operations
// ----------------------------------------------------------------------
Expand Down
42 changes: 42 additions & 0 deletions tests/LargeRecordsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

use PDPhilip\Elasticsearch\Tests\Models\Product;

beforeEach(function () {
Product::executeSchema();

});

it('tests track total hits', function () {

Product::buildRecords(11_000);

class ProductWithDefaultTrackTotalHits extends Product
{
protected $connection = 'elasticsearch_with_default_track_total_hits';
}

$products = Product::limit(1)->get();
expect($products->getQueryMeta()->getTotalHits())->toBe(10000);

$products = Product::limit(1)->withTrackTotalHits()->get();
expect($products->getQueryMeta()->getTotalHits())->toBe(11000);

$products = Product::limit(1)->withTrackTotalHits(false)->get();
expect($products->getQueryMeta()->getTotalHits())->toBe(-1);

$products = Product::limit(1)->withTrackTotalHits(300)->get();
expect($products->getQueryMeta()->getTotalHits())->toBe(300);

$products = ProductWithDefaultTrackTotalHits::limit(1)->get();
expect($products->getQueryMeta()->getTotalHits())->toBe(11000);

$products = ProductWithDefaultTrackTotalHits::limit(1)->withoutTrackTotalHits()->get();
expect($products->getQueryMeta()->getTotalHits())->toBe(10000);

$products = ProductWithDefaultTrackTotalHits::limit(1)->withTrackTotalHits(300)->get();
expect($products->getQueryMeta()->getTotalHits())->toBe(300);

});
13 changes: 13 additions & 0 deletions tests/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,17 @@ public static function executeSchema()
$table->date('updated_at');
});
}

public static function buildRecords($limit = 100)
{
$records = [];
while ($limit) {
$records[] = [
'state' => rand(1, 100),
];
$limit--;
}
Product::insert($records);
// Product::withoutRefresh()->insert($records);
}
}
10 changes: 10 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,15 @@ protected function getEnvironmentSetUp($app): void
'logging' => true,
],
]);

$app['config']->set('database.connections.elasticsearch_with_default_track_total_hits', [
'driver' => 'elasticsearch',
'auth_type' => 'http',
'hosts' => ['http://localhost:9200'],
'options' => [
'track_total_hits' => true,
'logging' => true,
],
]);
}
}