6
6
use Erichard \ElasticQueryBuilder \Filter \Filter ;
7
7
use Erichard \ElasticQueryBuilder \QueryBuilder ;
8
8
use Exception ;
9
+ use Illuminate \Contracts \Config \Repository ;
10
+ use Illuminate \Http \Request ;
9
11
use Illuminate \Pagination \LengthAwarePaginator ;
10
12
use Lelastico \Indices \AbstractElasticIndex ;
11
13
use Lelastico \Search \Query \Traits \AddQueries ;
12
14
use Lelastico \Search \Query \Traits \HasPaginationSettings ;
13
15
use Lelastico \Search \Query \Traits \LogQuery ;
14
16
use Lelastico \Search \Query \Traits \ParseResultsFromHits ;
17
+ use Psr \Log \LoggerInterface ;
15
18
16
19
abstract class AbstractBuilder
17
20
{
@@ -20,28 +23,21 @@ abstract class AbstractBuilder
20
23
use AddQueries;
21
24
use ParseResultsFromHits;
22
25
23
- /**
24
- * @var QueryBuilder
25
- */
26
- public $ query ;
27
-
28
- /**
29
- * @var array|null
30
- */
31
- public $ select = null ;
26
+ protected Request $ request ;
32
27
28
+ public QueryBuilder $ query ;
29
+ public ?array $ select = null ;
33
30
/**
34
31
* Custom client (by default is resolved by each index).
35
- *
36
- * @var Client|null
37
32
*/
38
- public $ client = null ;
33
+ public ? Client $ client = null ;
39
34
40
- /**
41
- * AvailabilitySearchBuilder constructor.
42
- */
43
- public function __construct ()
35
+ public function __construct (Request $ request , LoggerInterface $ logger , Repository $ config )
44
36
{
37
+ $ this ->request = $ request ;
38
+ $ this ->logger = $ logger ;
39
+ $ this ->config = $ config ;
40
+
45
41
// Create root filter that will be used as "group"
46
42
$ this ->filter = Filter::bool ();
47
43
@@ -52,13 +48,13 @@ public function __construct()
52
48
abstract protected function createIndex (): AbstractElasticIndex ;
53
49
54
50
/**
55
- * Runs a elastic query and returns laravel 's LengthAwarePaginator.
51
+ * Runs a elastic query and returns Laravel 's LengthAwarePaginator.
56
52
*
57
53
* @return LengthAwarePaginator
58
54
*
59
55
* @throws Exception
60
56
*/
61
- public function paginate ()
57
+ public function paginate (): LengthAwarePaginator
62
58
{
63
59
// Determine the index
64
60
$ index = $ this ->createIndex ();
@@ -110,7 +106,6 @@ public function paginate()
110
106
// Build simple array with _source array values (we do not need elastic related data)
111
107
$ items = $ this ->getResultsFromHits ($ result ['hits ' ]['hits ' ]);
112
108
113
- // Return array
114
109
return new LengthAwarePaginator (
115
110
$ items ,
116
111
// Use aggregated total entries calculation or total hits
@@ -119,11 +114,14 @@ public function paginate()
119
114
: $ result ['hits ' ]['total ' ]['value ' ],
120
115
$ this ->perPage ,
121
116
$ this ->currentPage ,
122
- ['path ' => request ()->fullUrl ()]
117
+ [
118
+ 'path ' => $ this ->request ->url (),
119
+ 'query ' => $ this ->request ->query ->all (),
120
+ ]
123
121
);
124
122
} catch (Exception $ exception ) {
125
- if (config ('lelastico.log_failure ' )) {
126
- logger ('Elastic search failed ' , [
123
+ if ($ this -> config -> get ('lelastico.log_failure ' )) {
124
+ $ this -> logger -> error ('Elastic search failed ' , [
127
125
'error ' => $ exception ->getMessage (),
128
126
'query ' => $ query ,
129
127
]);
@@ -137,7 +135,7 @@ public function paginate()
137
135
*
138
136
* @return AbstractBuilder
139
137
*/
140
- public function setSelect (array $ select )
138
+ public function setSelect (array $ select ): self
141
139
{
142
140
$ this ->select = $ select ;
143
141
0 commit comments