Skip to content

Commit 84cf795

Browse files
committed
Tests for track_total_hits
1 parent 5d69ff4 commit 84cf795

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

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)