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

Commit 3e11c5b

Browse files
committed
Added request explanation
1 parent 5d958ee commit 3e11c5b

File tree

6 files changed

+58
-4
lines changed

6 files changed

+58
-4
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Check out its [features](#features)!
1919
* [Console commands](#console-commands)
2020
* [Search rules](#search-rules)
2121
* [Available filters](#available-filters)
22+
* [Debug](#debug)
2223

2324
## Tutorial
2425

@@ -375,3 +376,14 @@ whereNotExists($field) | whereNotExists('unemployed') | Checks if a value isn't
375376
whereRegexp($field, $value, $flags = 'ALL') | whereRegexp('name.raw', 'A.+') | Filters records according to a given regular expression. [Here](https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-regexp-query.html#regexp-syntax) you can find more about syntax.
376377

377378
In most cases it's better to use raw fields to filter records, i.e. not analyzed fields.
379+
380+
## Debug
381+
382+
To analyze results of a search query you can set the `explain` flag to true and get explanation from received models:
383+
384+
```php
385+
App\MyModel::search('Brazil')
386+
->explain(true)
387+
->first()
388+
->getExplanation();
389+
```

src/Builders/FilterBuilder.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
use Laravel\Scout\Builder;
66

7-
class FilterBuilder extends Builder {
7+
class FilterBuilder extends Builder
8+
{
9+
public $explain = false;
10+
811
public function __construct($model, $callback = null)
912
{
1013
$this->model = $model;
@@ -118,4 +121,11 @@ public function whereRegexp($field, $value, $flags = 'ALL')
118121

119122
return $this;
120123
}
124+
125+
public function explain($bool)
126+
{
127+
$this->explain = $bool;
128+
129+
return $this;
130+
}
121131
}

src/Builders/SearchBuilder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace ScoutElastic\Builders;
44

5-
class SearchBuilder extends FilterBuilder {
5+
class SearchBuilder extends FilterBuilder
6+
{
67
public $rules = [];
78

89
public function __construct($model, $query, $callback = null)

src/ElasticEngine.php

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

186+
if ($builder->explain) {
187+
$payload['explain'] = true;
188+
}
189+
186190
return $this->buildTypePayload(
187191
$builder->model,
188192
$payload
@@ -315,7 +319,14 @@ public function map($results, $model)
315319
$id = $hit['_id'];
316320

317321
if (isset($models[$id])) {
318-
return $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;
319330
}
320331
})->filter();
321332
}

src/Features/HasExplanation.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace ScoutElastic\Features;
4+
5+
trait HasExplanation
6+
{
7+
protected $explanation;
8+
9+
public function getExplanation()
10+
{
11+
return $this->explanation;
12+
}
13+
14+
public function setExplanation(array $explanation)
15+
{
16+
$this->explanation = $explanation;
17+
}
18+
}

src/Searchable.php

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

1011
trait Searchable {
11-
use ScoutSearchable;
12+
use ScoutSearchable,
13+
HasExplanation;
1214

1315
/**
1416
* @return IndexConfigurator

0 commit comments

Comments
 (0)