Skip to content

Commit ddc67e8

Browse files
committed
Ensure that results are sorted by _id (could be breaking change) + use logger instead of debugbar_log
By default we are sorting by `_id` after any HasSorting logic to ensure that pagination is correct. You can turn this feature by using `$builder->setSortById(false);` Updated config structure: - `log_measurement` Logs every query to log (default false). You can use `ELASTICSEARCH_LOG_MEASUREMENT` env. - `log_debug` Debug logs every query data to a log (true in local environment). You can use `ELASTICSEARCH_LOG_DEBUG` env. - `service` Enables to change available indices (implement IndicesServiceContract or extend IndicesService) - `prefix` Used prefix for index names - uses APP_NAME and replace '-' to '_', converts name to slug variant. - `hosts` A list of IPS for your elastic search - https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/configuration.html. Use `;` separator. Default localhost:9200.
1 parent 3ffc8ae commit ddc67e8

File tree

5 files changed

+75
-9
lines changed

5 files changed

+75
-9
lines changed

config/lelastico.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
),
1212
'indices' => [],
1313
'log_failure' => true,
14-
'debugbar_log' => app()->isLocal() && function_exists('debugbar'),
14+
'log_debug' => env('ELASTICSEARCH_LOG_DEBUG', 'local' === env('APP_ENV')),
15+
'log_measurement' => env('ELASTICSEARCH_LOG_MEASUREMENT', false),
1516
'service' => IndicesService::class,
1617
];

readme.md

+16
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ Use `ELASTICSEARCH_HOSTS` environment for setting elastic search hosts. [Format]
6060

6161
```
6262
$client = resolve(\Elasticsearch\Client::class);
63+
```
64+
65+
```
66+
$client = $container->make(\Elasticsearch\Client::class);
6367
```
6468

6569
**Mapping types constants**
@@ -150,6 +154,10 @@ Property mappings types using constants like:
150154

151155
**Sorting**
152156

157+
**By default we are sorting by `_id` after any HasSorting logic to ensure that pagination is correct.**
158+
159+
You can turn this feature by using `$builder->setSortById(false);`
160+
153161
To enable sortable behavior add `HasSorting` trait to your instance of `AbstractBuilder` and implement method `allowedSortFields`.
154162

155163
```
@@ -180,6 +188,14 @@ Available directions for sorting are `asc` and `desc` and if not specified the d
180188
181189
`sort[]=goals:asc&sort[]=minutes:desc`
182190
191+
## Configuration
192+
193+
- `log_measurement` Logs every query to log (default false). You can use `ELASTICSEARCH_LOG_MEASUREMENT` env.
194+
- `log_debug` Debug logs every query data to a log (true in local environment). You can use `ELASTICSEARCH_LOG_DEBUG` env.
195+
- `service` Enables to change available indices (implement IndicesServiceContract or extend IndicesService)
196+
- `prefix` Used prefix for index names - uses APP_NAME and replace '-' to '_', converts name to slug variant.
197+
- `hosts` A list of IPS for your elastic search - https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/configuration.html. Use `;` separator. Default localhost:9200.
198+
183199
## TODO
184200
185201
- improve documentation

src/Search/Query/AbstractBuilder.php

+22
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Contracts\Config\Repository;
1010
use Illuminate\Http\Request;
1111
use Illuminate\Pagination\LengthAwarePaginator;
12+
use Lelastico\Constants\SortDirections;
1213
use Lelastico\Indices\AbstractElasticIndex;
1314
use Lelastico\Search\Query\Traits\AddQueries;
1415
use Lelastico\Search\Query\Traits\HasPaginationSettings;
@@ -33,6 +34,8 @@ abstract class AbstractBuilder
3334
*/
3435
public ?Client $client = null;
3536

37+
protected bool $sortById = true;
38+
3639
public function __construct(Request $request, LoggerInterface $logger, Repository $config)
3740
{
3841
$this->request = $request;
@@ -80,6 +83,10 @@ public function paginate(): LengthAwarePaginator
8083
$this->addSort($this->query, $this->request);
8184
}
8285

86+
if (true === $this->sortById) {
87+
$this->query->addSort('_id', SortDirections::ASC);
88+
}
89+
8390
// Build the query and improve it
8491
$query = $this->query->getQuery();
8592

@@ -147,4 +154,19 @@ public function setSelect(array $select): self
147154

148155
return $this;
149156
}
157+
158+
/**
159+
* Ensure that entries are by default sorted by _id to ensure correct pagination.
160+
*/
161+
public function setSortById(bool $sortById): self
162+
{
163+
$this->sortById = $sortById;
164+
165+
return $this;
166+
}
167+
168+
public function isSortingById(): bool
169+
{
170+
return $this->sortById;
171+
}
150172
}

src/Search/Query/Traits/LogQuery.php

+32-5
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,46 @@ trait LogQuery
1111
protected Repository $config;
1212

1313
/**
14-
* Logs the query with debugbar (or any custom solution).
14+
* Logs the query.
1515
*
1616
* @param array $result
1717
* @param array $query
1818
*/
1919
protected function logQuery(array $result, array $query)
2020
{
21-
if (false === $this->config->get('lelastico.debugbar_log')) {
21+
$time = $result['took'] / 1000; // ms
22+
23+
$isDebug = $this->config->get('lelastico.log_debug');
24+
25+
if (false === $isDebug) {
26+
if (true == $this->config->get('lelastico.log_measurement')) {
27+
$this->logMeasurement($time, $query['index'] ?? 'unknown');
28+
}
29+
2230
return;
2331
}
2432

25-
// TODO refactor
26-
add_measure('Elastic search', 0, $result['took'] / 1000);
27-
debugbar()->debug('Elastic search query '.json_encode($query, JSON_PRETTY_PRINT));
33+
// Debug-bar add_measure function
34+
$this->logDebug($time, $query);
35+
}
36+
37+
protected function logMeasurement(float $time, string $index): void
38+
{
39+
$this->logger->info('Elastic search query time', [
40+
'took' => $time,
41+
'index' => $index,
42+
]);
43+
}
44+
45+
protected function logDebug(float $time, array $query): void
46+
{
47+
if (true === function_exists('add_measure')) {
48+
add_measure('Elastic search', 0, $time);
49+
}
50+
51+
$this->logger->debug('Elastic search query', [
52+
'took' => $time,
53+
'query' => $query,
54+
]);
2855
}
2956
}

src/Write/BulkWrite.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ class BulkWrite
4242
* @var callable|null
4343
*/
4444
private $onSent;
45-
45+
4646
/**
47-
* Wait for refresh
48-
*
47+
* Wait for refresh.
48+
*
4949
* @var bool
5050
*/
5151
public $refresh = false;

0 commit comments

Comments
 (0)