Skip to content

Commit 289b20b

Browse files
committed
Use container depedency + fix request path building
1 parent d0de29b commit 289b20b

File tree

4 files changed

+37
-24
lines changed

4 files changed

+37
-24
lines changed

composer.json

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
"require": {
1111
"php": ">=7.4",
1212
"illuminate/support": ">=5.5",
13+
"illuminate/http": ">=5.5",
14+
"illuminate/pagination": ">=5.5",
15+
"illuminate/console": ">=5.5",
1316
"elasticsearch/elasticsearch": "^7",
1417
"erichard/elasticsearch-query-builder": "dev-collapse-and-improvments#9551a1e51d4059121072130716a9a31357283a08"
1518
},

readme.md

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ Focus of this library is to make it easier to manage elastic indices (wit mappin
1313
* Adds ability to create a query builder with query filters for each index.
1414
* Adds ability to build a query builder from request data (reusable component).
1515

16+
## Requirements
17+
18+
- Composer
19+
- PHP 7.4+
20+
1621
## Installation
1722

1823
**1. Add custom repository to composer.json**

src/Search/Query/AbstractBuilder.php

+21-23
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
use Erichard\ElasticQueryBuilder\Filter\Filter;
77
use Erichard\ElasticQueryBuilder\QueryBuilder;
88
use Exception;
9+
use Illuminate\Contracts\Config\Repository;
10+
use Illuminate\Http\Request;
911
use Illuminate\Pagination\LengthAwarePaginator;
1012
use Lelastico\Indices\AbstractElasticIndex;
1113
use Lelastico\Search\Query\Traits\AddQueries;
1214
use Lelastico\Search\Query\Traits\HasPaginationSettings;
1315
use Lelastico\Search\Query\Traits\LogQuery;
1416
use Lelastico\Search\Query\Traits\ParseResultsFromHits;
17+
use Psr\Log\LoggerInterface;
1518

1619
abstract class AbstractBuilder
1720
{
@@ -20,28 +23,21 @@ abstract class AbstractBuilder
2023
use AddQueries;
2124
use ParseResultsFromHits;
2225

23-
/**
24-
* @var QueryBuilder
25-
*/
26-
public $query;
27-
28-
/**
29-
* @var array|null
30-
*/
31-
public $select = null;
26+
protected Request $request;
3227

28+
public QueryBuilder $query;
29+
public ?array $select = null;
3330
/**
3431
* Custom client (by default is resolved by each index).
35-
*
36-
* @var Client|null
3732
*/
38-
public $client = null;
33+
public ?Client $client = null;
3934

40-
/**
41-
* AvailabilitySearchBuilder constructor.
42-
*/
43-
public function __construct()
35+
public function __construct(Request $request, LoggerInterface $logger, Repository $config)
4436
{
37+
$this->request = $request;
38+
$this->logger = $logger;
39+
$this->config = $config;
40+
4541
// Create root filter that will be used as "group"
4642
$this->filter = Filter::bool();
4743

@@ -52,13 +48,13 @@ public function __construct()
5248
abstract protected function createIndex(): AbstractElasticIndex;
5349

5450
/**
55-
* Runs a elastic query and returns laravel's LengthAwarePaginator.
51+
* Runs a elastic query and returns Laravel's LengthAwarePaginator.
5652
*
5753
* @return LengthAwarePaginator
5854
*
5955
* @throws Exception
6056
*/
61-
public function paginate()
57+
public function paginate(): LengthAwarePaginator
6258
{
6359
// Determine the index
6460
$index = $this->createIndex();
@@ -110,7 +106,6 @@ public function paginate()
110106
// Build simple array with _source array values (we do not need elastic related data)
111107
$items = $this->getResultsFromHits($result['hits']['hits']);
112108

113-
// Return array
114109
return new LengthAwarePaginator(
115110
$items,
116111
// Use aggregated total entries calculation or total hits
@@ -119,11 +114,14 @@ public function paginate()
119114
: $result['hits']['total']['value'],
120115
$this->perPage,
121116
$this->currentPage,
122-
['path' => request()->fullUrl()]
117+
[
118+
'path' => $this->request->url(),
119+
'query' => $this->request->query->all(),
120+
]
123121
);
124122
} 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', [
127125
'error' => $exception->getMessage(),
128126
'query' => $query,
129127
]);
@@ -137,7 +135,7 @@ public function paginate()
137135
*
138136
* @return AbstractBuilder
139137
*/
140-
public function setSelect(array $select)
138+
public function setSelect(array $select): self
141139
{
142140
$this->select = $select;
143141

src/Search/Query/Traits/LogQuery.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
namespace Lelastico\Search\Query\Traits;
44

5+
use Illuminate\Contracts\Config\Repository;
6+
use Psr\Log\LoggerInterface;
7+
58
trait LogQuery
69
{
10+
protected LoggerInterface $logger;
11+
protected Repository $config;
12+
713
/**
814
* Logs the query with debugbar (or any custom solution).
915
*
@@ -12,10 +18,11 @@ trait LogQuery
1218
*/
1319
protected function logQuery(array $result, array $query)
1420
{
15-
if (false === config('lelastico.debugbar_log')) {
21+
if (false === $this->config->get('lelastico.debugbar_log')) {
1622
return;
1723
}
1824

25+
// TODO refactor
1926
add_measure('Elastic search', 0, $result['took'] / 1000);
2027
debugbar()->debug('Elastic search query '.json_encode($query, JSON_PRETTY_PRINT));
2128
}

0 commit comments

Comments
 (0)