Skip to content
This repository was archived by the owner on Nov 4, 2021. It is now read-only.

Commit 4f2f73a

Browse files
committed
- Changed how query explanation works
- Added the profile method to the query builder
1 parent e96b2cb commit 4f2f73a

File tree

5 files changed

+44
-43
lines changed

5 files changed

+44
-43
lines changed

README.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -382,11 +382,22 @@ In most cases it's better to use raw fields to filter records, i.e. not analyzed
382382

383383
## Debug
384384

385-
To analyze results of a search query you can set the `explain` flag to true and get explanation from received models:
385+
There are two methods that can help you to analyze results of a search query:
386+
387+
* [explain](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html)
386388

387-
```php
388-
App\MyModel::search('Brazil')
389-
->explain(true)
390-
->first()
391-
->getExplanation();
392-
```
389+
```php
390+
App\MyModel::search('Brazil')
391+
->first()
392+
->explain();
393+
```
394+
395+
* [profile](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-profile.html)
396+
397+
```php
398+
App\MyModel::search('Brazil')
399+
->first()
400+
->profile();
401+
```
402+
403+
Both methods return raw data from ES.

src/Builders/FilterBuilder.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
class FilterBuilder extends Builder
88
{
9-
public $explain = false;
10-
119
public function __construct($model, $callback = null)
1210
{
1311
$this->model = $model;
@@ -122,10 +120,13 @@ public function whereRegexp($field, $value, $flags = 'ALL')
122120
return $this;
123121
}
124122

125-
public function explain($bool)
123+
public function explain()
126124
{
127-
$this->explain = $bool;
125+
return $this->engine()->explain($this);
126+
}
128127

129-
return $this;
128+
public function profile()
129+
{
130+
return $this->engine()->profile($this);
130131
}
131132
}

src/ElasticEngine.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,10 @@ protected function buildSearchQueryPayload(Builder $builder, $queryPayload, arra
183183
}
184184
}
185185

186-
if ($builder->explain) {
187-
$payload['explain'] = true;
186+
foreach (['explain', 'profile'] as $key) {
187+
if (isset($options[$key])) {
188+
$payload[$key] = $options[$key];
189+
}
188190
}
189191

190192
return $this->buildTypePayload(
@@ -290,6 +292,20 @@ public function paginate(Builder $builder, $perPage, $page)
290292
]);
291293
}
292294

295+
public function explain(Builder $builder)
296+
{
297+
return $this->performSearch($builder, [
298+
'explain' => true
299+
]);
300+
}
301+
302+
public function profile(Builder $builder)
303+
{
304+
return $this->performSearch($builder, [
305+
'profile' => true
306+
]);
307+
}
308+
293309
public function searchRaw(Model $model, $query)
294310
{
295311
$payload = $this->buildTypePayload($model, $query);
@@ -319,14 +335,7 @@ public function map($results, $model)
319335
$id = $hit['_id'];
320336

321337
if (isset($models[$id])) {
322-
/** @var Searchable $model */
323-
$model = $models[$id];
324-
325-
if (isset($hit['_explanation'])) {
326-
$model->setExplanation($hit['_explanation']);
327-
}
328-
329-
return $model;
338+
return $models[$id];
330339
}
331340
})->filter();
332341
}

src/Features/HasExplanation.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/Searchable.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
use ScoutElastic\Builders\FilterBuilder;
77
use ScoutElastic\Builders\SearchBuilder;
88
use \Exception;
9-
use ScoutElastic\Features\HasExplanation;
109

1110
trait Searchable {
12-
use ScoutSearchable,
13-
HasExplanation;
11+
use ScoutSearchable;
1412

1513
/**
1614
* @return IndexConfigurator

0 commit comments

Comments
 (0)