Skip to content

Commit

Permalink
Use container depedency + fix request path building
Browse files Browse the repository at this point in the history
  • Loading branch information
pionl committed Dec 31, 2020
1 parent d0de29b commit 289b20b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"require": {
"php": ">=7.4",
"illuminate/support": ">=5.5",
"illuminate/http": ">=5.5",
"illuminate/pagination": ">=5.5",
"illuminate/console": ">=5.5",
"elasticsearch/elasticsearch": "^7",
"erichard/elasticsearch-query-builder": "dev-collapse-and-improvments#9551a1e51d4059121072130716a9a31357283a08"
},
Expand Down
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ Focus of this library is to make it easier to manage elastic indices (wit mappin
* Adds ability to create a query builder with query filters for each index.
* Adds ability to build a query builder from request data (reusable component).

## Requirements

- Composer
- PHP 7.4+

## Installation

**1. Add custom repository to composer.json**
Expand Down
44 changes: 21 additions & 23 deletions src/Search/Query/AbstractBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
use Erichard\ElasticQueryBuilder\Filter\Filter;
use Erichard\ElasticQueryBuilder\QueryBuilder;
use Exception;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Lelastico\Indices\AbstractElasticIndex;
use Lelastico\Search\Query\Traits\AddQueries;
use Lelastico\Search\Query\Traits\HasPaginationSettings;
use Lelastico\Search\Query\Traits\LogQuery;
use Lelastico\Search\Query\Traits\ParseResultsFromHits;
use Psr\Log\LoggerInterface;

abstract class AbstractBuilder
{
Expand All @@ -20,28 +23,21 @@ abstract class AbstractBuilder
use AddQueries;
use ParseResultsFromHits;

/**
* @var QueryBuilder
*/
public $query;

/**
* @var array|null
*/
public $select = null;
protected Request $request;

public QueryBuilder $query;
public ?array $select = null;
/**
* Custom client (by default is resolved by each index).
*
* @var Client|null
*/
public $client = null;
public ?Client $client = null;

/**
* AvailabilitySearchBuilder constructor.
*/
public function __construct()
public function __construct(Request $request, LoggerInterface $logger, Repository $config)
{
$this->request = $request;
$this->logger = $logger;
$this->config = $config;

// Create root filter that will be used as "group"
$this->filter = Filter::bool();

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

/**
* Runs a elastic query and returns laravel's LengthAwarePaginator.
* Runs a elastic query and returns Laravel's LengthAwarePaginator.
*
* @return LengthAwarePaginator
*
* @throws Exception
*/
public function paginate()
public function paginate(): LengthAwarePaginator
{
// Determine the index
$index = $this->createIndex();
Expand Down Expand Up @@ -110,7 +106,6 @@ public function paginate()
// Build simple array with _source array values (we do not need elastic related data)
$items = $this->getResultsFromHits($result['hits']['hits']);

// Return array
return new LengthAwarePaginator(
$items,
// Use aggregated total entries calculation or total hits
Expand All @@ -119,11 +114,14 @@ public function paginate()
: $result['hits']['total']['value'],
$this->perPage,
$this->currentPage,
['path' => request()->fullUrl()]
[
'path' => $this->request->url(),
'query' => $this->request->query->all(),
]
);
} catch (Exception $exception) {
if (config('lelastico.log_failure')) {
logger('Elastic search failed', [
if ($this->config->get('lelastico.log_failure')) {
$this->logger->error('Elastic search failed', [
'error' => $exception->getMessage(),
'query' => $query,
]);
Expand All @@ -137,7 +135,7 @@ public function paginate()
*
* @return AbstractBuilder
*/
public function setSelect(array $select)
public function setSelect(array $select): self
{
$this->select = $select;

Expand Down
9 changes: 8 additions & 1 deletion src/Search/Query/Traits/LogQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

namespace Lelastico\Search\Query\Traits;

use Illuminate\Contracts\Config\Repository;
use Psr\Log\LoggerInterface;

trait LogQuery
{
protected LoggerInterface $logger;
protected Repository $config;

/**
* Logs the query with debugbar (or any custom solution).
*
Expand All @@ -12,10 +18,11 @@ trait LogQuery
*/
protected function logQuery(array $result, array $query)
{
if (false === config('lelastico.debugbar_log')) {
if (false === $this->config->get('lelastico.debugbar_log')) {
return;
}

// TODO refactor
add_measure('Elastic search', 0, $result['took'] / 1000);
debugbar()->debug('Elastic search query '.json_encode($query, JSON_PRETTY_PRINT));
}
Expand Down

0 comments on commit 289b20b

Please sign in to comment.