File tree Expand file tree Collapse file tree 3 files changed +65
-0
lines changed Expand file tree Collapse file tree 3 files changed +65
-0
lines changed Original file line number Diff line number Diff line change 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+ });
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments